skeleton.test.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { nextTick } from 'vue'
  2. import { mount } from '@vue/test-utils'
  3. import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
  4. import Skeleton from '../src/skeleton.vue'
  5. import type { SkeletonInstance } from '../src/skeleton'
  6. const AXIOM = 'AXIOM is the best girl'
  7. describe('Skeleton.vue', () => {
  8. beforeEach(() => {
  9. vi.useFakeTimers()
  10. })
  11. afterEach(() => {
  12. vi.restoreAllMocks()
  13. })
  14. it('render test', () => {
  15. const wrapper = mount(<Skeleton />)
  16. expect(wrapper.findAll('.el-skeleton__p')).toHaveLength(4)
  17. expect(wrapper.classes()).toMatchInlineSnapshot(`
  18. [
  19. "el-skeleton",
  20. ]
  21. `)
  22. })
  23. it('should render with animation', () => {
  24. const wrapper = mount(<Skeleton animated={true} />)
  25. expect(wrapper.classes()).toMatchInlineSnapshot(`
  26. [
  27. "el-skeleton",
  28. "is-animated",
  29. ]
  30. `)
  31. })
  32. it('should render x times', async () => {
  33. const wrapper = mount(<Skeleton />)
  34. expect(wrapper.findAll('.el-skeleton__p')).toHaveLength(4)
  35. await wrapper.setProps({
  36. count: 2,
  37. })
  38. expect(wrapper.findAll('.el-skeleton__p')).toHaveLength(8)
  39. })
  40. it('should render x rows', () => {
  41. const wrapper = mount(<Skeleton rows={4} />)
  42. expect(wrapper.findAll('.el-skeleton__p')).toHaveLength(5)
  43. })
  44. it('should render default slots', () => {
  45. const wrapper = mount(<Skeleton loading={false}>{AXIOM}</Skeleton>)
  46. expect(wrapper.text()).toBe(AXIOM)
  47. })
  48. it('should render templates', () => {
  49. const wrapper = mount(
  50. <Skeleton
  51. v-slots={{
  52. template: () => AXIOM,
  53. }}
  54. />
  55. )
  56. expect(wrapper.text()).toBe(AXIOM)
  57. })
  58. it('should throttle rendering', async () => {
  59. const wrapper = mount(<Skeleton throttle={500} />)
  60. expect((wrapper.vm as SkeletonInstance).uiLoading).toBe(false)
  61. vi.runAllTimers()
  62. await nextTick()
  63. expect((wrapper.vm as SkeletonInstance).uiLoading).toBe(true)
  64. })
  65. })