collection.test.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { h } from 'vue'
  2. import { mount } from '@vue/test-utils'
  3. import { afterEach, describe, expect, it } from 'vitest'
  4. import TestCollection, {
  5. CollectionChildComponent as ChildComponent,
  6. } from '../test-helper'
  7. import type { ComponentPublicInstance } from 'vue'
  8. import type { ElCollectionInjectionContext } from '../src/tokens'
  9. const { ElCollection } = TestCollection
  10. const AXIOM = 'rem is the best girl'
  11. describe('<ElCollection />', () => {
  12. const createComponent = (props = {}) =>
  13. mount(ElCollection as any, {
  14. props,
  15. slots: {
  16. default: () =>
  17. h(ChildComponent, null, {
  18. default: () => AXIOM,
  19. }),
  20. },
  21. })
  22. let wrapper: ReturnType<typeof createComponent>
  23. afterEach(() => {
  24. wrapper.unmount()
  25. })
  26. describe('render', () => {
  27. it('should be able to render correctly', () => {
  28. wrapper = createComponent()
  29. expect(wrapper.text()).toContain(AXIOM)
  30. })
  31. })
  32. describe('provides', () => {
  33. it('should be able to provide valid data', async () => {
  34. wrapper = createComponent()
  35. const childComponent = wrapper.findComponent(ChildComponent as any)
  36. const vm =
  37. childComponent.vm as ComponentPublicInstance<ElCollectionInjectionContext>
  38. expect([...vm.itemMap.values()]).toHaveLength(0)
  39. expect(vm.getItems()).toHaveLength(0)
  40. expect(vm.collectionRef).toBe(childComponent.element)
  41. })
  42. })
  43. })