refInfor.spec.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import Vue from '@vue/compat'
  2. import { nextTick } from '../../runtime-core/src/scheduler'
  3. import {
  4. DeprecationTypes,
  5. deprecationData,
  6. toggleDeprecationWarning
  7. } from '../../runtime-core/src/compat/compatConfig'
  8. beforeEach(() => {
  9. toggleDeprecationWarning(true)
  10. Vue.configureCompat({
  11. MODE: 2,
  12. GLOBAL_MOUNT: 'suppress-warning'
  13. })
  14. })
  15. afterEach(() => {
  16. toggleDeprecationWarning(false)
  17. Vue.configureCompat({ MODE: 3 })
  18. })
  19. test('V_FOR_REF', async () => {
  20. const vm = new Vue({
  21. data() {
  22. return {
  23. ok: true,
  24. list: [1, 2, 3]
  25. }
  26. },
  27. template: `
  28. <template v-if="ok">
  29. <li v-for="i in list" ref="list">{{ i }}</li>
  30. </template>
  31. `
  32. }).$mount() as any
  33. const mapRefs = () => vm.$refs.list.map((el: HTMLElement) => el.textContent)
  34. expect(mapRefs()).toMatchObject(['1', '2', '3'])
  35. expect(deprecationData[DeprecationTypes.V_FOR_REF].message).toHaveBeenWarned()
  36. vm.list.push(4)
  37. await nextTick()
  38. expect(mapRefs()).toMatchObject(['1', '2', '3', '4'])
  39. vm.list.shift()
  40. await nextTick()
  41. expect(mapRefs()).toMatchObject(['2', '3', '4'])
  42. vm.ok = !vm.ok
  43. await nextTick()
  44. expect(mapRefs()).toMatchObject([])
  45. vm.ok = !vm.ok
  46. await nextTick()
  47. expect(mapRefs()).toMatchObject(['2', '3', '4'])
  48. })