switch.test.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. import { markRaw, nextTick, ref } from 'vue'
  2. import { mount } from '@vue/test-utils'
  3. import { afterEach, describe, expect, test, vi } from 'vitest'
  4. import { debugWarn } from '@element-plus/utils'
  5. import { Checked, CircleClose } from '@element-plus/icons-vue'
  6. import { ElFormItem } from '@element-plus/components/form'
  7. import Switch from '../src/switch.vue'
  8. import type { VueWrapper } from '@vue/test-utils'
  9. import type { SwitchInstance } from '../src/switch'
  10. vi.mock('@element-plus/utils/error', () => ({
  11. debugWarn: vi.fn(),
  12. }))
  13. describe('Switch.vue', () => {
  14. afterEach(() => {
  15. vi.clearAllMocks()
  16. })
  17. test('create', () => {
  18. const props = {
  19. activeText: 'on',
  20. inactiveText: 'off',
  21. activeColor: '#0f0',
  22. inactiveColor: '#f00',
  23. width: 100,
  24. }
  25. const wrapper = mount(() => <Switch {...props} />)
  26. const vm = wrapper.vm
  27. expect(vm.$el.style.getPropertyValue('--el-switch-on-color')).toEqual(
  28. '#0f0'
  29. )
  30. expect(vm.$el.style.getPropertyValue('--el-switch-off-color')).toEqual(
  31. '#f00'
  32. )
  33. expect(vm.$el.classList.contains('is-checked')).false
  34. const coreEl = vm.$el.querySelector('.el-switch__core')
  35. expect(coreEl.style.width).toEqual('100px')
  36. const leftLabelWrapper = wrapper.find('.el-switch__label--left span')
  37. expect(leftLabelWrapper.text()).toEqual('off')
  38. })
  39. test('size', () => {
  40. const wrapper = mount(() => <Switch size="large" />)
  41. expect(wrapper.find('.el-switch--large').exists()).toBe(true)
  42. })
  43. test('tabindex', () => {
  44. const wrapper = mount(() => <Switch tabindex="0" />)
  45. expect(wrapper.find('.el-switch__input').attributes().tabindex).toBe('0')
  46. })
  47. test('inline prompt', () => {
  48. const props = {
  49. inlinePrompt: true,
  50. activeText: 'on',
  51. inactiveText: 'off',
  52. activeColor: '#0f0',
  53. inactiveColor: '#f00',
  54. width: 100,
  55. }
  56. const wrapper = mount(() => <Switch {...props} />)
  57. const vm = wrapper.vm
  58. expect(vm.$el.style.getPropertyValue('--el-switch-on-color')).toEqual(
  59. '#0f0'
  60. )
  61. expect(vm.$el.style.getPropertyValue('--el-switch-off-color')).toEqual(
  62. '#f00'
  63. )
  64. expect(vm.$el.classList.contains('is-checked')).false
  65. const coreEl = vm.$el.querySelector('.el-switch__core')
  66. expect(coreEl.style.width).toEqual('100px')
  67. const label = wrapper.find('.el-switch__inner span')
  68. expect(label.text()).toEqual('off')
  69. })
  70. test('switch with icons', () => {
  71. const wrapper = mount(() => (
  72. <Switch
  73. activeIcon={markRaw(Checked)}
  74. inactiveIcon={markRaw(CircleClose)}
  75. />
  76. ))
  77. expect(wrapper.findComponent(Checked).exists()).toBe(true)
  78. })
  79. test('value correctly update', async () => {
  80. const value = ref(true)
  81. const wrapper = mount(() => (
  82. <Switch v-model={value.value} activeColor="#0f0" inactiveColor="#f00" />
  83. ))
  84. const vm = wrapper.vm
  85. expect(vm.$el.style.getPropertyValue('--el-switch-on-color')).toEqual(
  86. '#0f0'
  87. )
  88. expect(vm.$el.style.getPropertyValue('--el-switch-off-color')).toEqual(
  89. '#f00'
  90. )
  91. expect(vm.$el.classList.contains('is-checked')).true
  92. const coreWrapper = wrapper.find('.el-switch__core')
  93. await coreWrapper.trigger('click')
  94. expect(vm.$el.classList.contains('is-checked')).false
  95. expect(value.value).toEqual(false)
  96. await coreWrapper.trigger('click')
  97. expect(vm.$el.classList.contains('is-checked')).true
  98. expect(value.value).toEqual(true)
  99. })
  100. test('change event', async () => {
  101. const target = ref<string | number | boolean>(1)
  102. const value = ref(true)
  103. const handleChange = (val: string | number | boolean) => {
  104. target.value = val
  105. }
  106. const wrapper = mount(() => (
  107. <Switch v-model={value.value} onUpdate:modelValue={handleChange} />
  108. ))
  109. expect(target.value).toEqual(1)
  110. const coreWrapper = wrapper.find('.el-switch__core')
  111. await coreWrapper.trigger('click')
  112. const switchWrapper = wrapper.findComponent(Switch)
  113. expect(switchWrapper.emitted()['update:modelValue']).toBeTruthy()
  114. expect(target.value).toEqual(false)
  115. })
  116. test('disabled switch should not respond to user click', async () => {
  117. const value = ref(true)
  118. const wrapper = mount(() => <Switch disabled v-model={value.value} />)
  119. expect(value.value).toEqual(true)
  120. const coreWrapper = wrapper.find('.el-switch__core')
  121. await coreWrapper.trigger('click')
  122. expect(value.value).toEqual(true)
  123. })
  124. test('expand switch value', async () => {
  125. const value = ref('100')
  126. const onValue = ref('100')
  127. const offValue = ref('0')
  128. const wrapper = mount(() => (
  129. <div>
  130. <Switch
  131. v-model={value.value}
  132. active-value={onValue.value}
  133. inactive-value={offValue.value}
  134. />
  135. </div>
  136. ))
  137. const coreWrapper = wrapper.find('.el-switch__core')
  138. await coreWrapper.trigger('click')
  139. expect(value.value).toEqual('0')
  140. await coreWrapper.trigger('click')
  141. expect(value.value).toEqual('100')
  142. })
  143. test('default switch active-value is false', async () => {
  144. const value = ref(false)
  145. const onValue = ref(false)
  146. const offValue = ref(true)
  147. const wrapper = mount(() => (
  148. <div>
  149. <Switch
  150. v-model={value.value}
  151. active-value={onValue.value}
  152. inactive-value={offValue.value}
  153. />
  154. </div>
  155. ))
  156. const coreWrapper = wrapper.find('.el-switch__core')
  157. await coreWrapper.trigger('click')
  158. expect(value.value).toEqual(true)
  159. await coreWrapper.trigger('click')
  160. expect(value.value).toEqual(false)
  161. })
  162. test('value is the single source of truth', async () => {
  163. const wrapper = mount(() => <Switch value={true} />)
  164. const vm = wrapper.vm
  165. const coreWrapper = wrapper.find('.el-switch__core')
  166. const switchWrapper: VueWrapper<SwitchInstance> =
  167. wrapper.findComponent(Switch)
  168. const switchVm = switchWrapper.vm
  169. const inputEl = vm.$el.querySelector('input')
  170. expect(switchVm.$.exposed?.checked.value).toBe(true)
  171. expect(switchWrapper.classes('is-checked')).toEqual(true)
  172. expect(inputEl.checked).toEqual(true)
  173. await coreWrapper.trigger('click')
  174. expect(switchVm.$.exposed?.checked.value).toBe(true)
  175. expect(switchWrapper.classes('is-checked')).toEqual(true)
  176. expect(inputEl.checked).toEqual(true)
  177. })
  178. test('model-value is the single source of truth', async () => {
  179. const wrapper = mount(() => <Switch model-value={true} />)
  180. const vm = wrapper.vm
  181. const coreWrapper = wrapper.find('.el-switch__core')
  182. const switchWrapper: VueWrapper<SwitchInstance> =
  183. wrapper.findComponent(Switch)
  184. const switchVm = switchWrapper.vm
  185. const inputEl = vm.$el.querySelector('input')
  186. expect(switchVm.$.exposed?.checked.value).toBe(true)
  187. expect(switchWrapper.classes('is-checked')).toEqual(true)
  188. expect(inputEl.checked).toEqual(true)
  189. await coreWrapper.trigger('click')
  190. expect(switchVm.$.exposed?.checked.value).toBe(true)
  191. expect(switchWrapper.classes('is-checked')).toEqual(true)
  192. expect(inputEl.checked).toEqual(true)
  193. })
  194. test('sets checkbox value', async () => {
  195. const value = ref(false)
  196. const wrapper = mount(() => (
  197. <div>
  198. <Switch v-model={value.value} />
  199. </div>
  200. ))
  201. const vm = wrapper.vm
  202. const inputEl = vm.$el.querySelector('input')
  203. value.value = true
  204. await vm.$nextTick()
  205. expect(inputEl.checked).toEqual(true)
  206. value.value = false
  207. await vm.$nextTick()
  208. expect(inputEl.checked).toEqual(false)
  209. })
  210. test('beforeChange function return promise', async () => {
  211. const value = ref(true)
  212. const loading = ref(false)
  213. const asyncResult = ref('error')
  214. const beforeChange = () => {
  215. loading.value = true
  216. return new Promise<boolean>((resolve, reject) => {
  217. setTimeout(() => {
  218. loading.value = false
  219. return asyncResult.value == 'success'
  220. ? resolve(true)
  221. : reject(new Error('Error'))
  222. }, 1000)
  223. })
  224. }
  225. const wrapper = mount(() => (
  226. <div>
  227. <Switch
  228. v-model={value.value}
  229. loading={loading.value}
  230. beforeChange={beforeChange}
  231. />
  232. </div>
  233. ))
  234. const coreWrapper = wrapper.find('.el-switch__core')
  235. vi.useFakeTimers()
  236. await coreWrapper.trigger('click')
  237. vi.runAllTimers()
  238. await nextTick()
  239. expect(value.value).toEqual(true)
  240. expect(debugWarn).toHaveBeenCalledTimes(0)
  241. asyncResult.value = 'success'
  242. await coreWrapper.trigger('click')
  243. vi.runAllTimers()
  244. await nextTick()
  245. expect(value.value).toEqual(false)
  246. expect(debugWarn).toHaveBeenCalledTimes(1)
  247. await coreWrapper.trigger('click')
  248. vi.runAllTimers()
  249. await nextTick()
  250. expect(value.value).toEqual(true)
  251. expect(debugWarn).toHaveBeenCalledTimes(1)
  252. })
  253. test('beforeChange function return boolean', async () => {
  254. const value = ref(true)
  255. const result = ref(false)
  256. const beforeChange = () => {
  257. // do something ...
  258. return result.value
  259. }
  260. const wrapper = mount(() => (
  261. <div>
  262. <Switch v-model={value.value} beforeChange={beforeChange} />
  263. </div>
  264. ))
  265. const coreWrapper = wrapper.find('.el-switch__core')
  266. await coreWrapper.trigger('click')
  267. expect(value.value).toEqual(true)
  268. result.value = true
  269. await coreWrapper.trigger('click')
  270. expect(value.value).toEqual(false)
  271. await coreWrapper.trigger('click')
  272. expect(value.value).toEqual(true)
  273. })
  274. describe('form item accessibility integration', () => {
  275. test('automatic id attachment', async () => {
  276. const wrapper = mount(() => (
  277. <ElFormItem label="Foobar" data-test-ref="item">
  278. <Switch />
  279. </ElFormItem>
  280. ))
  281. await nextTick()
  282. const formItem = wrapper.find('[data-test-ref="item"]')
  283. const formItemLabel = formItem.find('.el-form-item__label')
  284. const switchInput = wrapper.find('.el-switch__input')
  285. expect(formItem.attributes().role).toBeFalsy()
  286. expect(formItemLabel.attributes().for).toBe(switchInput.attributes().id)
  287. })
  288. test('specified id attachment', async () => {
  289. const wrapper = mount(() => (
  290. <ElFormItem label="Foobar" data-test-ref="item">
  291. <Switch id="foobar" />
  292. </ElFormItem>
  293. ))
  294. await nextTick()
  295. const formItem = wrapper.find('[data-test-ref="item"]')
  296. const formItemLabel = formItem.find('.el-form-item__label')
  297. const switchInput = wrapper.find('.el-switch__input')
  298. expect(formItem.attributes().role).toBeFalsy()
  299. expect(switchInput.attributes().id).toBe('foobar')
  300. expect(formItemLabel.attributes().for).toBe(switchInput.attributes().id)
  301. })
  302. test('form item role is group when multiple inputs', async () => {
  303. const wrapper = mount(() => (
  304. <ElFormItem label="Foobar" data-test-ref="item">
  305. <Switch />
  306. <Switch />
  307. </ElFormItem>
  308. ))
  309. await nextTick()
  310. const formItem = wrapper.find('[data-test-ref="item"]')
  311. expect(formItem.attributes().role).toBe('group')
  312. })
  313. })
  314. })