Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit 08946a4

Browse files
committed
test(switch): add tests for v-model + @change event
1 parent 3bf03b9 commit 08946a4

File tree

4 files changed

+59
-12
lines changed

4 files changed

+59
-12
lines changed

packages/chakra-ui-core/src/CSwitch/CSwitch.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @see Source https://github.com/chakra-ui/chakra-ui-vue/blob/master/packages/chakra-ui-core/src/CSwitch/CSwitch.js
99
*/
1010

11-
import { forwardProps, extractListeners } from '../utils'
11+
import { forwardProps } from '../utils'
1212

1313
import CBox from '../CBox'
1414
import CVisuallyHidden from '../CVisuallyHidden'
@@ -29,6 +29,15 @@ const switchSizes = {
2929
}
3030
}
3131

32+
/** Emits events for functional components */
33+
const emitFunctionalEvent = (fn, ...args) => {
34+
if (fn && typeof fn === 'function') {
35+
fn(...args)
36+
} else if (Array.isArray(fn)) {
37+
fn.forEach(handler => typeof handler === 'function' && handler(...args))
38+
}
39+
}
40+
3241
/**
3342
* CSwitch component
3443
*
@@ -101,18 +110,14 @@ const CSwitch = {
101110
}
102111
}
103112

104-
// Events
105-
const nonNativeEvents = {
113+
const eventListeners = {
114+
...listeners,
106115
change: (e) => {
107-
const emitChange = listeners.change
108-
if (emitChange && typeof emitChange === 'function') {
109-
emitChange(!props.isChecked)
110-
}
116+
const newValue = !props.isChecked
117+
emitFunctionalEvent(listeners.change, newValue, e)
111118
}
112119
}
113120

114-
const { native, nonNative } = extractListeners({ listeners }, nonNativeEvents)
115-
116121
return h(CBox, {
117122
...rest,
118123
props: {
@@ -142,8 +147,7 @@ const CSwitch = {
142147
checked: props.isChecked,
143148
disabled: props.isDisabled
144149
},
145-
on: nonNative,
146-
nativeOn: native
150+
on: eventListeners
147151
}),
148152
h(CControlBox, {
149153
attrs: styleProps

packages/chakra-ui-core/src/CSwitch/CSwitch.stories.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,20 @@ storiesOf('UI | Switch', module)
4444
}
4545
}
4646
}))
47+
.add('With v-model + @change', () => ({
48+
components: { CBox, CSwitch },
49+
data: () => ({
50+
enable: false
51+
}),
52+
template: `
53+
<p>
54+
<span>{{enable}}</span>
55+
<c-switch v-model="enable" id="email-alerts" @change="action"/>
56+
</p>`,
57+
methods: {
58+
action: action('@change(event) + v-model'),
59+
handleClick (e) {
60+
console.log('Native event handler', e)
61+
}
62+
}
63+
}))

packages/chakra-ui-core/src/CSwitch/tests/CSwitch.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,29 @@ test('properly handles v-model', async () => {
4949

5050
expect(screen.getByText('enabled')).toBeInTheDocument()
5151
})
52+
53+
/**
54+
* Because the CSwitch is a functional component, it
55+
* handles event emission differently when the
56+
* consumer uses both v-model and the @change
57+
* event listener.
58+
*/
59+
it('should emit a change event when v-model + event listener are provided', async () => {
60+
const onChange = jest.fn()
61+
renderComponent({
62+
data: () => ({
63+
enable: false
64+
}),
65+
template: `
66+
<div>
67+
<span>{{enable ? 'enabled' : 'disabled'}}</span>
68+
<CSwitch v-model="enable" data-testid="switch" @change="handleChange" />
69+
</div>`,
70+
methods: { handleChange: onChange }
71+
})
72+
73+
expect(screen.getByText('disabled')).toBeInTheDocument()
74+
await userEvent.click(screen.getByTestId('switch'))
75+
expect(screen.getByText('enabled')).toBeInTheDocument()
76+
expect(onChange).toHaveBeenCalledTimes(1)
77+
})

packages/chakra-ui-core/src/utils/vdom.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export const extractListeners = (context, nonNative = {}) => {
127127

128128
for (const listener in listeners) {
129129
if (!nonNative[listener]) {
130-
native[listener] = listeners[listener]
130+
nonNative[listener] = listeners[listener]
131131
}
132132
}
133133

0 commit comments

Comments
 (0)