collection-item.test.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { h, nextTick } from 'vue'
  2. import { mount } from '@vue/test-utils'
  3. import { afterEach, describe, expect, it } from 'vitest'
  4. import TestCollection, {
  5. CollectionChildComponent,
  6. CollectionItemChildComponent,
  7. } from '../test-helper'
  8. import type { ComponentPublicInstance } from 'vue'
  9. import type { ElCollectionInjectionContext } from '../src/tokens'
  10. const { ElCollection, ElCollectionItem } = TestCollection
  11. const AXIOM = 'rem is the best girl'
  12. describe('<ElCollectionItem />', () => {
  13. const createComponent = (props = {}, count = 3) =>
  14. mount(ElCollection as any, {
  15. props,
  16. slots: {
  17. default: () => {
  18. return h(
  19. CollectionChildComponent as any,
  20. {},
  21. {
  22. default: () =>
  23. Array.from({ length: count }).map((idx) => {
  24. return h(
  25. ElCollectionItem as any,
  26. {},
  27. {
  28. default: () => [
  29. h(
  30. CollectionItemChildComponent,
  31. {},
  32. { default: () => `${AXIOM} ${idx}` }
  33. ),
  34. ],
  35. }
  36. )
  37. }),
  38. }
  39. )
  40. },
  41. },
  42. })
  43. let wrapper: ReturnType<typeof createComponent>
  44. afterEach(() => {
  45. wrapper.unmount()
  46. })
  47. it('should be able to render correctly', async () => {
  48. wrapper = createComponent()
  49. await nextTick()
  50. expect(wrapper.findAllComponents(ElCollectionItem as any)).toHaveLength(3)
  51. expect(wrapper.findComponent(ElCollectionItem as any).text()).toContain(
  52. AXIOM
  53. )
  54. })
  55. it('register child instance', () => {
  56. wrapper = createComponent()
  57. const childItemComponent = wrapper.findComponent(
  58. CollectionChildComponent as any
  59. )
  60. const childVm =
  61. childItemComponent.vm as ComponentPublicInstance<ElCollectionInjectionContext>
  62. const collectionItems = wrapper.findAllComponents(
  63. CollectionItemChildComponent as any
  64. )
  65. expect(childVm.itemMap.size).toBe(3)
  66. const items = childVm.getItems()
  67. expect(childVm.getItems()).toHaveLength(3)
  68. expect(items[0].ref).toBe(collectionItems.at(0)?.element)
  69. })
  70. })