2022-07-28 19:31:18 +08:00
|
|
|
import { markRaw, nextTick, ref } from 'vue'
|
2020-07-31 14:53:25 +08:00
|
|
|
import { mount } from '@vue/test-utils'
|
2022-04-19 17:37:56 +08:00
|
|
|
import { afterEach, describe, expect, test, vi } from 'vitest'
|
|
|
|
import { debugWarn } from '@element-plus/utils'
|
2021-12-04 11:20:06 +08:00
|
|
|
import { Checked, CircleClose } from '@element-plus/icons-vue'
|
2022-05-05 22:04:32 +08:00
|
|
|
import { ElFormItem } from '@element-plus/components/form'
|
2021-12-10 18:30:15 +08:00
|
|
|
import Switch from '../src/switch.vue'
|
2022-06-22 00:16:51 +08:00
|
|
|
import type { VueWrapper } from '@vue/test-utils'
|
|
|
|
import type { SwitchInstance } from '../src/switch'
|
2021-04-26 11:53:09 +08:00
|
|
|
|
2022-04-19 17:37:56 +08:00
|
|
|
vi.mock('@element-plus/utils/error', () => ({
|
|
|
|
debugWarn: vi.fn(),
|
|
|
|
}))
|
|
|
|
|
2020-07-31 14:53:25 +08:00
|
|
|
describe('Switch.vue', () => {
|
2022-04-19 17:37:56 +08:00
|
|
|
afterEach(() => {
|
|
|
|
vi.clearAllMocks()
|
|
|
|
})
|
|
|
|
|
2020-07-31 14:53:25 +08:00
|
|
|
test('create', () => {
|
2022-07-28 19:31:18 +08:00
|
|
|
const props = {
|
|
|
|
activeText: 'on',
|
|
|
|
inactiveText: 'off',
|
|
|
|
activeColor: '#0f0',
|
|
|
|
inactiveColor: '#f00',
|
|
|
|
width: 100,
|
|
|
|
}
|
|
|
|
const wrapper = mount(() => <Switch {...props} />)
|
2020-07-31 14:53:25 +08:00
|
|
|
const vm = wrapper.vm
|
2022-06-12 22:40:53 +08:00
|
|
|
expect(vm.$el.style.getPropertyValue('--el-switch-on-color')).toEqual(
|
|
|
|
'#0f0'
|
|
|
|
)
|
|
|
|
expect(vm.$el.style.getPropertyValue('--el-switch-off-color')).toEqual(
|
|
|
|
'#f00'
|
|
|
|
)
|
|
|
|
expect(vm.$el.classList.contains('is-checked')).false
|
2020-07-31 14:53:25 +08:00
|
|
|
const coreEl = vm.$el.querySelector('.el-switch__core')
|
|
|
|
expect(coreEl.style.width).toEqual('100px')
|
|
|
|
const leftLabelWrapper = wrapper.find('.el-switch__label--left span')
|
|
|
|
expect(leftLabelWrapper.text()).toEqual('off')
|
|
|
|
})
|
|
|
|
|
2022-01-07 15:43:53 +08:00
|
|
|
test('size', () => {
|
2022-07-28 19:31:18 +08:00
|
|
|
const wrapper = mount(() => <Switch size="large" />)
|
2022-01-07 15:43:53 +08:00
|
|
|
expect(wrapper.find('.el-switch--large').exists()).toBe(true)
|
|
|
|
})
|
|
|
|
|
2022-06-08 09:23:49 +08:00
|
|
|
test('tabindex', () => {
|
2022-07-28 19:31:18 +08:00
|
|
|
const wrapper = mount(() => <Switch tabindex="0" />)
|
2022-06-08 09:23:49 +08:00
|
|
|
expect(wrapper.find('.el-switch__input').attributes().tabindex).toBe('0')
|
|
|
|
})
|
|
|
|
|
2021-10-30 21:23:29 +08:00
|
|
|
test('inline prompt', () => {
|
2022-07-28 19:31:18 +08:00
|
|
|
const props = {
|
|
|
|
inlinePrompt: true,
|
|
|
|
activeText: 'on',
|
|
|
|
inactiveText: 'off',
|
|
|
|
activeColor: '#0f0',
|
|
|
|
inactiveColor: '#f00',
|
|
|
|
width: 100,
|
|
|
|
}
|
|
|
|
const wrapper = mount(() => <Switch {...props} />)
|
2021-10-30 21:23:29 +08:00
|
|
|
const vm = wrapper.vm
|
2022-06-12 22:40:53 +08:00
|
|
|
expect(vm.$el.style.getPropertyValue('--el-switch-on-color')).toEqual(
|
|
|
|
'#0f0'
|
|
|
|
)
|
|
|
|
expect(vm.$el.style.getPropertyValue('--el-switch-off-color')).toEqual(
|
|
|
|
'#f00'
|
|
|
|
)
|
|
|
|
expect(vm.$el.classList.contains('is-checked')).false
|
2021-10-30 21:23:29 +08:00
|
|
|
const coreEl = vm.$el.querySelector('.el-switch__core')
|
|
|
|
expect(coreEl.style.width).toEqual('100px')
|
|
|
|
const leftLabelWrapper = wrapper.find('.el-switch__inner span')
|
2022-02-21 10:16:21 +08:00
|
|
|
expect(leftLabelWrapper.text()).toEqual('on')
|
2021-10-30 21:23:29 +08:00
|
|
|
})
|
|
|
|
|
2020-07-31 14:53:25 +08:00
|
|
|
test('switch with icons', () => {
|
2022-07-28 19:31:18 +08:00
|
|
|
const wrapper = mount(() => (
|
|
|
|
<Switch
|
|
|
|
activeIcon={markRaw(Checked)}
|
|
|
|
inactiveIcon={markRaw(CircleClose)}
|
|
|
|
/>
|
|
|
|
))
|
2020-07-31 14:53:25 +08:00
|
|
|
|
2021-10-27 23:17:13 +08:00
|
|
|
expect(wrapper.findComponent(Checked).exists()).toBe(true)
|
2020-07-31 14:53:25 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('value correctly update', async () => {
|
2022-07-28 19:31:18 +08:00
|
|
|
const value = ref(true)
|
|
|
|
const wrapper = mount(() => (
|
|
|
|
<Switch v-model={value.value} activeColor="#0f0" inactiveColor="#f00" />
|
|
|
|
))
|
2020-07-31 14:53:25 +08:00
|
|
|
const vm = wrapper.vm
|
2022-06-12 22:40:53 +08:00
|
|
|
expect(vm.$el.style.getPropertyValue('--el-switch-on-color')).toEqual(
|
|
|
|
'#0f0'
|
|
|
|
)
|
|
|
|
expect(vm.$el.style.getPropertyValue('--el-switch-off-color')).toEqual(
|
|
|
|
'#f00'
|
|
|
|
)
|
|
|
|
expect(vm.$el.classList.contains('is-checked')).true
|
2020-07-31 14:53:25 +08:00
|
|
|
const coreWrapper = wrapper.find('.el-switch__core')
|
|
|
|
await coreWrapper.trigger('click')
|
2022-06-12 22:40:53 +08:00
|
|
|
expect(vm.$el.classList.contains('is-checked')).false
|
2022-07-28 19:31:18 +08:00
|
|
|
expect(value.value).toEqual(false)
|
2020-07-31 14:53:25 +08:00
|
|
|
await coreWrapper.trigger('click')
|
2022-06-12 22:40:53 +08:00
|
|
|
expect(vm.$el.classList.contains('is-checked')).true
|
2022-07-28 19:31:18 +08:00
|
|
|
expect(value.value).toEqual(true)
|
2020-07-31 14:53:25 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('change event', async () => {
|
2022-08-18 20:31:37 +08:00
|
|
|
const target = ref<string | number | boolean>(1)
|
2022-07-28 19:31:18 +08:00
|
|
|
const value = ref(true)
|
2022-08-18 20:31:37 +08:00
|
|
|
const handleChange = (val: string | number | boolean) => {
|
2022-07-28 19:31:18 +08:00
|
|
|
target.value = val
|
|
|
|
}
|
|
|
|
const wrapper = mount(() => (
|
|
|
|
<Switch v-model={value.value} onUpdate:modelValue={handleChange} />
|
|
|
|
))
|
|
|
|
|
|
|
|
expect(target.value).toEqual(1)
|
2020-07-31 14:53:25 +08:00
|
|
|
const coreWrapper = wrapper.find('.el-switch__core')
|
|
|
|
await coreWrapper.trigger('click')
|
|
|
|
const switchWrapper = wrapper.findComponent(Switch)
|
|
|
|
expect(switchWrapper.emitted()['update:modelValue']).toBeTruthy()
|
2022-07-28 19:31:18 +08:00
|
|
|
expect(target.value).toEqual(false)
|
2020-07-31 14:53:25 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('disabled switch should not respond to user click', async () => {
|
2022-07-28 19:31:18 +08:00
|
|
|
const value = ref(true)
|
|
|
|
const wrapper = mount(() => <Switch disabled v-model={value.value} />)
|
2020-07-31 14:53:25 +08:00
|
|
|
|
2022-07-28 19:31:18 +08:00
|
|
|
expect(value.value).toEqual(true)
|
2020-07-31 14:53:25 +08:00
|
|
|
const coreWrapper = wrapper.find('.el-switch__core')
|
|
|
|
await coreWrapper.trigger('click')
|
2022-07-28 19:31:18 +08:00
|
|
|
expect(value.value).toEqual(true)
|
2020-07-31 14:53:25 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('expand switch value', async () => {
|
2022-07-28 19:31:18 +08:00
|
|
|
const value = ref('100')
|
|
|
|
const onValue = ref('100')
|
|
|
|
const offValue = ref('0')
|
|
|
|
const wrapper = mount(() => (
|
|
|
|
<div>
|
|
|
|
<Switch
|
|
|
|
v-model={value.value}
|
|
|
|
active-value={onValue.value}
|
|
|
|
inactive-value={offValue.value}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
))
|
2020-07-31 14:53:25 +08:00
|
|
|
|
|
|
|
const coreWrapper = wrapper.find('.el-switch__core')
|
|
|
|
await coreWrapper.trigger('click')
|
2022-07-28 19:31:18 +08:00
|
|
|
expect(value.value).toEqual('0')
|
2020-07-31 14:53:25 +08:00
|
|
|
await coreWrapper.trigger('click')
|
2022-07-28 19:31:18 +08:00
|
|
|
expect(value.value).toEqual('100')
|
2020-07-31 14:53:25 +08:00
|
|
|
})
|
|
|
|
|
2022-06-22 00:16:51 +08:00
|
|
|
test('default switch active-value is false', async () => {
|
2022-07-28 19:31:18 +08:00
|
|
|
const value = ref(false)
|
|
|
|
const onValue = ref(false)
|
|
|
|
const offValue = ref(true)
|
|
|
|
const wrapper = mount(() => (
|
|
|
|
<div>
|
|
|
|
<Switch
|
|
|
|
v-model={value.value}
|
|
|
|
active-value={onValue.value}
|
|
|
|
inactive-value={offValue.value}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
))
|
2022-06-22 00:16:51 +08:00
|
|
|
|
|
|
|
const coreWrapper = wrapper.find('.el-switch__core')
|
|
|
|
await coreWrapper.trigger('click')
|
2022-07-28 19:31:18 +08:00
|
|
|
expect(value.value).toEqual(true)
|
2022-06-22 00:16:51 +08:00
|
|
|
await coreWrapper.trigger('click')
|
2022-07-28 19:31:18 +08:00
|
|
|
expect(value.value).toEqual(false)
|
2022-06-22 00:16:51 +08:00
|
|
|
})
|
|
|
|
|
2020-07-31 14:53:25 +08:00
|
|
|
test('value is the single source of truth', async () => {
|
2022-07-28 19:31:18 +08:00
|
|
|
const wrapper = mount(() => <Switch value={true} />)
|
|
|
|
|
2020-07-31 14:53:25 +08:00
|
|
|
const vm = wrapper.vm
|
|
|
|
const coreWrapper = wrapper.find('.el-switch__core')
|
2022-06-22 00:16:51 +08:00
|
|
|
const switchWrapper: VueWrapper<SwitchInstance> =
|
|
|
|
wrapper.findComponent(Switch)
|
2020-07-31 14:53:25 +08:00
|
|
|
const switchVm = switchWrapper.vm
|
|
|
|
const inputEl = vm.$el.querySelector('input')
|
|
|
|
|
2022-08-18 20:31:37 +08:00
|
|
|
expect(switchVm.$.exposed?.checked.value).toBe(true)
|
2020-07-31 14:53:25 +08:00
|
|
|
expect(switchWrapper.classes('is-checked')).toEqual(true)
|
|
|
|
expect(inputEl.checked).toEqual(true)
|
|
|
|
await coreWrapper.trigger('click')
|
2022-08-18 20:31:37 +08:00
|
|
|
expect(switchVm.$.exposed?.checked.value).toBe(true)
|
2020-07-31 14:53:25 +08:00
|
|
|
expect(switchWrapper.classes('is-checked')).toEqual(true)
|
|
|
|
expect(inputEl.checked).toEqual(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('model-value is the single source of truth', async () => {
|
2022-07-28 19:31:18 +08:00
|
|
|
const wrapper = mount(() => <Switch model-value={true} />)
|
|
|
|
|
2020-07-31 14:53:25 +08:00
|
|
|
const vm = wrapper.vm
|
|
|
|
const coreWrapper = wrapper.find('.el-switch__core')
|
2022-06-22 00:16:51 +08:00
|
|
|
const switchWrapper: VueWrapper<SwitchInstance> =
|
|
|
|
wrapper.findComponent(Switch)
|
2020-07-31 14:53:25 +08:00
|
|
|
const switchVm = switchWrapper.vm
|
|
|
|
const inputEl = vm.$el.querySelector('input')
|
|
|
|
|
2022-08-18 20:31:37 +08:00
|
|
|
expect(switchVm.$.exposed?.checked.value).toBe(true)
|
2020-07-31 14:53:25 +08:00
|
|
|
expect(switchWrapper.classes('is-checked')).toEqual(true)
|
|
|
|
expect(inputEl.checked).toEqual(true)
|
|
|
|
await coreWrapper.trigger('click')
|
2022-08-18 20:31:37 +08:00
|
|
|
expect(switchVm.$.exposed?.checked.value).toBe(true)
|
2020-07-31 14:53:25 +08:00
|
|
|
expect(switchWrapper.classes('is-checked')).toEqual(true)
|
|
|
|
expect(inputEl.checked).toEqual(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('sets checkbox value', async () => {
|
2022-07-28 19:31:18 +08:00
|
|
|
const value = ref(false)
|
|
|
|
const wrapper = mount(() => (
|
|
|
|
<div>
|
|
|
|
<Switch v-model={value.value} />
|
|
|
|
</div>
|
|
|
|
))
|
2020-07-31 14:53:25 +08:00
|
|
|
const vm = wrapper.vm
|
|
|
|
const inputEl = vm.$el.querySelector('input')
|
|
|
|
|
2022-07-28 19:31:18 +08:00
|
|
|
value.value = true
|
2020-07-31 14:53:25 +08:00
|
|
|
await vm.$nextTick()
|
|
|
|
expect(inputEl.checked).toEqual(true)
|
2022-07-28 19:31:18 +08:00
|
|
|
value.value = false
|
2020-07-31 14:53:25 +08:00
|
|
|
await vm.$nextTick()
|
|
|
|
expect(inputEl.checked).toEqual(false)
|
|
|
|
})
|
2021-04-26 11:53:09 +08:00
|
|
|
|
|
|
|
test('beforeChange function return promise', async () => {
|
2022-07-28 19:31:18 +08:00
|
|
|
const value = ref(true)
|
|
|
|
const loading = ref(false)
|
|
|
|
const asyncResult = ref('error')
|
|
|
|
const beforeChange = () => {
|
|
|
|
loading.value = true
|
2022-08-18 20:31:37 +08:00
|
|
|
return new Promise<boolean>((resolve, reject) => {
|
2022-07-28 19:31:18 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
loading.value = false
|
|
|
|
return asyncResult.value == 'success'
|
|
|
|
? resolve(true)
|
|
|
|
: reject(new Error('Error'))
|
|
|
|
}, 1000)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
const wrapper = mount(() => (
|
|
|
|
<div>
|
|
|
|
<Switch
|
|
|
|
v-model={value.value}
|
|
|
|
loading={loading.value}
|
|
|
|
beforeChange={beforeChange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
))
|
2021-04-26 11:53:09 +08:00
|
|
|
|
|
|
|
const coreWrapper = wrapper.find('.el-switch__core')
|
|
|
|
|
2022-04-19 12:46:57 +08:00
|
|
|
vi.useFakeTimers()
|
|
|
|
|
|
|
|
await coreWrapper.trigger('click')
|
|
|
|
vi.runAllTimers()
|
2021-04-26 11:53:09 +08:00
|
|
|
await nextTick()
|
2022-07-28 19:31:18 +08:00
|
|
|
expect(value.value).toEqual(true)
|
2022-04-19 17:37:56 +08:00
|
|
|
expect(debugWarn).toHaveBeenCalledTimes(0)
|
2021-04-26 11:53:09 +08:00
|
|
|
|
2022-07-28 19:31:18 +08:00
|
|
|
asyncResult.value = 'success'
|
2021-04-26 11:53:09 +08:00
|
|
|
|
2022-04-19 12:46:57 +08:00
|
|
|
await coreWrapper.trigger('click')
|
|
|
|
vi.runAllTimers()
|
2021-04-26 11:53:09 +08:00
|
|
|
await nextTick()
|
2022-07-28 19:31:18 +08:00
|
|
|
expect(value.value).toEqual(false)
|
2022-04-19 17:37:56 +08:00
|
|
|
expect(debugWarn).toHaveBeenCalledTimes(1)
|
2021-04-26 11:53:09 +08:00
|
|
|
|
2022-04-19 12:46:57 +08:00
|
|
|
await coreWrapper.trigger('click')
|
|
|
|
vi.runAllTimers()
|
2021-04-26 11:53:09 +08:00
|
|
|
await nextTick()
|
2022-07-28 19:31:18 +08:00
|
|
|
expect(value.value).toEqual(true)
|
2022-04-19 17:37:56 +08:00
|
|
|
expect(debugWarn).toHaveBeenCalledTimes(1)
|
2021-04-26 11:53:09 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('beforeChange function return boolean', async () => {
|
2022-07-28 19:31:18 +08:00
|
|
|
const value = ref(true)
|
|
|
|
const result = ref(false)
|
|
|
|
const beforeChange = () => {
|
|
|
|
// do something ...
|
|
|
|
return result.value
|
|
|
|
}
|
|
|
|
const wrapper = mount(() => (
|
|
|
|
<div>
|
|
|
|
<Switch v-model={value.value} beforeChange={beforeChange} />
|
|
|
|
</div>
|
|
|
|
))
|
2021-04-26 11:53:09 +08:00
|
|
|
|
|
|
|
const coreWrapper = wrapper.find('.el-switch__core')
|
|
|
|
|
|
|
|
await coreWrapper.trigger('click')
|
2022-07-28 19:31:18 +08:00
|
|
|
expect(value.value).toEqual(true)
|
2021-04-26 11:53:09 +08:00
|
|
|
|
2022-07-28 19:31:18 +08:00
|
|
|
result.value = true
|
2021-04-26 11:53:09 +08:00
|
|
|
|
|
|
|
await coreWrapper.trigger('click')
|
2022-07-28 19:31:18 +08:00
|
|
|
expect(value.value).toEqual(false)
|
2021-04-26 11:53:09 +08:00
|
|
|
|
|
|
|
await coreWrapper.trigger('click')
|
2022-07-28 19:31:18 +08:00
|
|
|
expect(value.value).toEqual(true)
|
2021-04-26 11:53:09 +08:00
|
|
|
})
|
2022-05-05 22:04:32 +08:00
|
|
|
|
|
|
|
describe('form item accessibility integration', () => {
|
|
|
|
test('automatic id attachment', async () => {
|
2022-07-28 19:31:18 +08:00
|
|
|
const wrapper = mount(() => (
|
|
|
|
<ElFormItem label="Foobar" data-test-ref="item">
|
|
|
|
<Switch />
|
|
|
|
</ElFormItem>
|
|
|
|
))
|
2022-05-05 22:04:32 +08:00
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
const formItem = wrapper.find('[data-test-ref="item"]')
|
|
|
|
const formItemLabel = formItem.find('.el-form-item__label')
|
|
|
|
const switchInput = wrapper.find('.el-switch__input')
|
|
|
|
expect(formItem.attributes().role).toBeFalsy()
|
|
|
|
expect(formItemLabel.attributes().for).toBe(switchInput.attributes().id)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('specified id attachment', async () => {
|
2022-07-28 19:31:18 +08:00
|
|
|
const wrapper = mount(() => (
|
|
|
|
<ElFormItem label="Foobar" data-test-ref="item">
|
|
|
|
<Switch id="foobar" />
|
|
|
|
</ElFormItem>
|
|
|
|
))
|
2022-05-05 22:04:32 +08:00
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
const formItem = wrapper.find('[data-test-ref="item"]')
|
|
|
|
const formItemLabel = formItem.find('.el-form-item__label')
|
|
|
|
const switchInput = wrapper.find('.el-switch__input')
|
|
|
|
expect(formItem.attributes().role).toBeFalsy()
|
|
|
|
expect(switchInput.attributes().id).toBe('foobar')
|
|
|
|
expect(formItemLabel.attributes().for).toBe(switchInput.attributes().id)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('form item role is group when multiple inputs', async () => {
|
2022-07-28 19:31:18 +08:00
|
|
|
const wrapper = mount(() => (
|
|
|
|
<ElFormItem label="Foobar" data-test-ref="item">
|
|
|
|
<Switch />
|
|
|
|
<Switch />
|
|
|
|
</ElFormItem>
|
|
|
|
))
|
2022-05-05 22:04:32 +08:00
|
|
|
|
|
|
|
await nextTick()
|
|
|
|
const formItem = wrapper.find('[data-test-ref="item"]')
|
|
|
|
expect(formItem.attributes().role).toBe('group')
|
|
|
|
})
|
|
|
|
})
|
2020-07-31 14:53:25 +08:00
|
|
|
})
|