countdown.test.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { mount } from '@vue/test-utils'
  2. import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
  3. import dayjs from 'dayjs'
  4. import Countdown from '../src/countdown.vue'
  5. import type { VueWrapper } from '@vue/test-utils'
  6. const TITLE_CLASS = '.el-statistic__head'
  7. const CONTENT_CLASS = '.el-statistic__content'
  8. describe('Countdown.vue', () => {
  9. let wrapper: VueWrapper<any>
  10. beforeEach(() => {
  11. vi.useFakeTimers()
  12. })
  13. afterEach(() => {
  14. vi.useRealTimers()
  15. wrapper.unmount()
  16. })
  17. it('render test', () => {
  18. wrapper = mount(() => (
  19. <Countdown title="test" value={Date.now() + 1000 * 60} />
  20. ))
  21. expect(wrapper.find(TITLE_CLASS).text()).toBe('test')
  22. expect(wrapper.find(CONTENT_CLASS).text()).toBe('00:01:00')
  23. })
  24. describe('format', () => {
  25. it.each([
  26. ['DD HH:mm:ss', '02 02:02:02'],
  27. ['DD [days] HH [hours] mm:ss', '02 days 02 hours 02:02'],
  28. ['HH:mm:ss', '50:02:02'],
  29. ['HH:mm:ss:SSS', '50:02:02:002'],
  30. ])('should work with %s', (format, expected) => {
  31. const value = dayjs()
  32. .add(2, 'd')
  33. .add(2, 'h')
  34. .add(2, 'm')
  35. .add(2, 's')
  36. .add(2, 'ms')
  37. wrapper = mount(() => <Countdown value={value} format={format} />)
  38. expect(wrapper.find(CONTENT_CLASS).text()).toBe(expected)
  39. })
  40. })
  41. it('change event', () => {
  42. const onChange = vi.fn()
  43. wrapper = mount(() => (
  44. <Countdown onChange={onChange} value={Date.now() + 1000 * 60} />
  45. ))
  46. vi.advanceTimersByTime(16)
  47. expect(onChange).toHaveBeenCalledTimes(1)
  48. })
  49. it('finish event', () => {
  50. const onFinish = vi.fn()
  51. wrapper = mount(() => (
  52. <Countdown onFinish={onFinish} value={Date.now() + 1000 * 60} />
  53. ))
  54. vi.advanceTimersByTime(1000 * 30)
  55. expect(onFinish).not.toHaveBeenCalled()
  56. vi.runAllTimers()
  57. expect(onFinish).toHaveBeenCalled()
  58. })
  59. })