apiWatch.spec.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. import {
  2. watch,
  3. watchEffect,
  4. reactive,
  5. computed,
  6. nextTick,
  7. ref,
  8. defineComponent,
  9. getCurrentInstance,
  10. ComponentInternalInstance,
  11. ComponentPublicInstance
  12. } from '../src/index'
  13. import {
  14. render,
  15. nodeOps,
  16. serializeInner,
  17. TestElement,
  18. h
  19. } from '@vue/runtime-test'
  20. import {
  21. ITERATE_KEY,
  22. DebuggerEvent,
  23. TrackOpTypes,
  24. TriggerOpTypes,
  25. triggerRef,
  26. shallowRef,
  27. Ref
  28. } from '@vue/reactivity'
  29. // reference: https://vue-composition-api-rfc.netlify.com/api.html#watch
  30. describe('api: watch', () => {
  31. it('effect', async () => {
  32. const state = reactive({ count: 0 })
  33. let dummy
  34. watchEffect(() => {
  35. dummy = state.count
  36. })
  37. expect(dummy).toBe(0)
  38. state.count++
  39. await nextTick()
  40. expect(dummy).toBe(1)
  41. })
  42. it('watching single source: getter', async () => {
  43. const state = reactive({ count: 0 })
  44. let dummy
  45. watch(
  46. () => state.count,
  47. (count, prevCount) => {
  48. dummy = [count, prevCount]
  49. // assert types
  50. count + 1
  51. if (prevCount) {
  52. prevCount + 1
  53. }
  54. }
  55. )
  56. state.count++
  57. await nextTick()
  58. expect(dummy).toMatchObject([1, 0])
  59. })
  60. it('watching single source: ref', async () => {
  61. const count = ref(0)
  62. let dummy
  63. watch(count, (count, prevCount) => {
  64. dummy = [count, prevCount]
  65. // assert types
  66. count + 1
  67. if (prevCount) {
  68. prevCount + 1
  69. }
  70. })
  71. count.value++
  72. await nextTick()
  73. expect(dummy).toMatchObject([1, 0])
  74. })
  75. it('watching single source: array', async () => {
  76. const array = reactive([] as number[])
  77. const spy = jest.fn()
  78. watch(array, spy)
  79. array.push(1)
  80. await nextTick()
  81. expect(spy).toBeCalledTimes(1)
  82. expect(spy).toBeCalledWith([1], expect.anything(), expect.anything())
  83. })
  84. it('should not fire if watched getter result did not change', async () => {
  85. const spy = jest.fn()
  86. const n = ref(0)
  87. watch(() => n.value % 2, spy)
  88. n.value++
  89. await nextTick()
  90. expect(spy).toBeCalledTimes(1)
  91. n.value += 2
  92. await nextTick()
  93. // should not be called again because getter result did not change
  94. expect(spy).toBeCalledTimes(1)
  95. })
  96. it('watching single source: computed ref', async () => {
  97. const count = ref(0)
  98. const plus = computed(() => count.value + 1)
  99. let dummy
  100. watch(plus, (count, prevCount) => {
  101. dummy = [count, prevCount]
  102. // assert types
  103. count + 1
  104. if (prevCount) {
  105. prevCount + 1
  106. }
  107. })
  108. count.value++
  109. await nextTick()
  110. expect(dummy).toMatchObject([2, 1])
  111. })
  112. it('watching primitive with deep: true', async () => {
  113. const count = ref(0)
  114. let dummy
  115. watch(
  116. count,
  117. (c, prevCount) => {
  118. dummy = [c, prevCount]
  119. },
  120. {
  121. deep: true
  122. }
  123. )
  124. count.value++
  125. await nextTick()
  126. expect(dummy).toMatchObject([1, 0])
  127. })
  128. it('directly watching reactive object (with automatic deep: true)', async () => {
  129. const src = reactive({
  130. count: 0
  131. })
  132. let dummy
  133. watch(src, ({ count }) => {
  134. dummy = count
  135. })
  136. src.count++
  137. await nextTick()
  138. expect(dummy).toBe(1)
  139. })
  140. it('watching multiple sources', async () => {
  141. const state = reactive({ count: 1 })
  142. const count = ref(1)
  143. const plus = computed(() => count.value + 1)
  144. let dummy
  145. watch([() => state.count, count, plus], (vals, oldVals) => {
  146. dummy = [vals, oldVals]
  147. // assert types
  148. vals.concat(1)
  149. oldVals.concat(1)
  150. })
  151. state.count++
  152. count.value++
  153. await nextTick()
  154. expect(dummy).toMatchObject([[2, 2, 3], [1, 1, 2]])
  155. })
  156. it('watching multiple sources: readonly array', async () => {
  157. const state = reactive({ count: 1 })
  158. const status = ref(false)
  159. let dummy
  160. watch([() => state.count, status] as const, (vals, oldVals) => {
  161. dummy = [vals, oldVals]
  162. const [count] = vals
  163. const [, oldStatus] = oldVals
  164. // assert types
  165. count + 1
  166. oldStatus === true
  167. })
  168. state.count++
  169. status.value = true
  170. await nextTick()
  171. expect(dummy).toMatchObject([[2, true], [1, false]])
  172. })
  173. it('watching multiple sources: reactive object (with automatic deep: true)', async () => {
  174. const src = reactive({ count: 0 })
  175. let dummy
  176. watch([src], ([state]) => {
  177. dummy = state
  178. // assert types
  179. state.count === 1
  180. })
  181. src.count++
  182. await nextTick()
  183. expect(dummy).toMatchObject({ count: 1 })
  184. })
  185. it('warn invalid watch source', () => {
  186. // @ts-ignore
  187. watch(1, () => {})
  188. expect(`Invalid watch source`).toHaveBeenWarned()
  189. })
  190. it('warn invalid watch source: multiple sources', () => {
  191. watch([1], () => {})
  192. expect(`Invalid watch source`).toHaveBeenWarned()
  193. })
  194. it('stopping the watcher (effect)', async () => {
  195. const state = reactive({ count: 0 })
  196. let dummy
  197. const stop = watchEffect(() => {
  198. dummy = state.count
  199. })
  200. expect(dummy).toBe(0)
  201. stop()
  202. state.count++
  203. await nextTick()
  204. // should not update
  205. expect(dummy).toBe(0)
  206. })
  207. it('stopping the watcher (with source)', async () => {
  208. const state = reactive({ count: 0 })
  209. let dummy
  210. const stop = watch(
  211. () => state.count,
  212. count => {
  213. dummy = count
  214. }
  215. )
  216. state.count++
  217. await nextTick()
  218. expect(dummy).toBe(1)
  219. stop()
  220. state.count++
  221. await nextTick()
  222. // should not update
  223. expect(dummy).toBe(1)
  224. })
  225. it('cleanup registration (effect)', async () => {
  226. const state = reactive({ count: 0 })
  227. const cleanup = jest.fn()
  228. let dummy
  229. const stop = watchEffect(onCleanup => {
  230. onCleanup(cleanup)
  231. dummy = state.count
  232. })
  233. expect(dummy).toBe(0)
  234. state.count++
  235. await nextTick()
  236. expect(cleanup).toHaveBeenCalledTimes(1)
  237. expect(dummy).toBe(1)
  238. stop()
  239. expect(cleanup).toHaveBeenCalledTimes(2)
  240. })
  241. it('cleanup registration (with source)', async () => {
  242. const count = ref(0)
  243. const cleanup = jest.fn()
  244. let dummy
  245. const stop = watch(count, (count, prevCount, onCleanup) => {
  246. onCleanup(cleanup)
  247. dummy = count
  248. })
  249. count.value++
  250. await nextTick()
  251. expect(cleanup).toHaveBeenCalledTimes(0)
  252. expect(dummy).toBe(1)
  253. count.value++
  254. await nextTick()
  255. expect(cleanup).toHaveBeenCalledTimes(1)
  256. expect(dummy).toBe(2)
  257. stop()
  258. expect(cleanup).toHaveBeenCalledTimes(2)
  259. })
  260. it('flush timing: pre (default)', async () => {
  261. const count = ref(0)
  262. const count2 = ref(0)
  263. let callCount = 0
  264. let result1
  265. let result2
  266. const assertion = jest.fn((count, count2Value) => {
  267. callCount++
  268. // on mount, the watcher callback should be called before DOM render
  269. // on update, should be called before the count is updated
  270. const expectedDOM = callCount === 1 ? `` : `${count - 1}`
  271. result1 = serializeInner(root) === expectedDOM
  272. // in a pre-flush callback, all state should have been updated
  273. const expectedState = callCount - 1
  274. result2 = count === expectedState && count2Value === expectedState
  275. })
  276. const Comp = {
  277. setup() {
  278. watchEffect(() => {
  279. assertion(count.value, count2.value)
  280. })
  281. return () => count.value
  282. }
  283. }
  284. const root = nodeOps.createElement('div')
  285. render(h(Comp), root)
  286. expect(assertion).toHaveBeenCalledTimes(1)
  287. expect(result1).toBe(true)
  288. expect(result2).toBe(true)
  289. count.value++
  290. count2.value++
  291. await nextTick()
  292. // two mutations should result in 1 callback execution
  293. expect(assertion).toHaveBeenCalledTimes(2)
  294. expect(result1).toBe(true)
  295. expect(result2).toBe(true)
  296. })
  297. it('flush timing: post', async () => {
  298. const count = ref(0)
  299. let result
  300. const assertion = jest.fn(count => {
  301. result = serializeInner(root) === `${count}`
  302. })
  303. const Comp = {
  304. setup() {
  305. watchEffect(
  306. () => {
  307. assertion(count.value)
  308. },
  309. { flush: 'post' }
  310. )
  311. return () => count.value
  312. }
  313. }
  314. const root = nodeOps.createElement('div')
  315. render(h(Comp), root)
  316. expect(assertion).toHaveBeenCalledTimes(1)
  317. expect(result).toBe(true)
  318. count.value++
  319. await nextTick()
  320. expect(assertion).toHaveBeenCalledTimes(2)
  321. expect(result).toBe(true)
  322. })
  323. it('flush timing: sync', async () => {
  324. const count = ref(0)
  325. const count2 = ref(0)
  326. let callCount = 0
  327. let result1
  328. let result2
  329. const assertion = jest.fn(count => {
  330. callCount++
  331. // on mount, the watcher callback should be called before DOM render
  332. // on update, should be called before the count is updated
  333. const expectedDOM = callCount === 1 ? `` : `${count - 1}`
  334. result1 = serializeInner(root) === expectedDOM
  335. // in a sync callback, state mutation on the next line should not have
  336. // executed yet on the 2nd call, but will be on the 3rd call.
  337. const expectedState = callCount < 3 ? 0 : 1
  338. result2 = count2.value === expectedState
  339. })
  340. const Comp = {
  341. setup() {
  342. watchEffect(
  343. () => {
  344. assertion(count.value)
  345. },
  346. {
  347. flush: 'sync'
  348. }
  349. )
  350. return () => count.value
  351. }
  352. }
  353. const root = nodeOps.createElement('div')
  354. render(h(Comp), root)
  355. expect(assertion).toHaveBeenCalledTimes(1)
  356. expect(result1).toBe(true)
  357. expect(result2).toBe(true)
  358. count.value++
  359. count2.value++
  360. await nextTick()
  361. expect(assertion).toHaveBeenCalledTimes(3)
  362. expect(result1).toBe(true)
  363. expect(result2).toBe(true)
  364. })
  365. it('should not fire on component unmount w/ flush: post', async () => {
  366. const toggle = ref(true)
  367. const cb = jest.fn()
  368. const Comp = {
  369. setup() {
  370. watch(toggle, cb, { flush: 'post' })
  371. },
  372. render() {}
  373. }
  374. const App = {
  375. render() {
  376. return toggle.value ? h(Comp) : null
  377. }
  378. }
  379. render(h(App), nodeOps.createElement('div'))
  380. expect(cb).not.toHaveBeenCalled()
  381. toggle.value = false
  382. await nextTick()
  383. expect(cb).not.toHaveBeenCalled()
  384. })
  385. it('should fire on component unmount w/ flush: pre', async () => {
  386. const toggle = ref(true)
  387. const cb = jest.fn()
  388. const Comp = {
  389. setup() {
  390. watch(toggle, cb, { flush: 'pre' })
  391. },
  392. render() {}
  393. }
  394. const App = {
  395. render() {
  396. return toggle.value ? h(Comp) : null
  397. }
  398. }
  399. render(h(App), nodeOps.createElement('div'))
  400. expect(cb).not.toHaveBeenCalled()
  401. toggle.value = false
  402. await nextTick()
  403. expect(cb).toHaveBeenCalledTimes(1)
  404. })
  405. // #1763
  406. it('flush: pre watcher watching props should fire before child update', async () => {
  407. const a = ref(0)
  408. const b = ref(0)
  409. const c = ref(0)
  410. const calls: string[] = []
  411. const Comp = {
  412. props: ['a', 'b'],
  413. setup(props: any) {
  414. watch(
  415. () => props.a + props.b,
  416. () => {
  417. calls.push('watcher 1')
  418. c.value++
  419. },
  420. { flush: 'pre' }
  421. )
  422. // #1777 chained pre-watcher
  423. watch(
  424. c,
  425. () => {
  426. calls.push('watcher 2')
  427. },
  428. { flush: 'pre' }
  429. )
  430. return () => {
  431. c.value
  432. calls.push('render')
  433. }
  434. }
  435. }
  436. const App = {
  437. render() {
  438. return h(Comp, { a: a.value, b: b.value })
  439. }
  440. }
  441. render(h(App), nodeOps.createElement('div'))
  442. expect(calls).toEqual(['render'])
  443. // both props are updated
  444. // should trigger pre-flush watcher first and only once
  445. // then trigger child render
  446. a.value++
  447. b.value++
  448. await nextTick()
  449. expect(calls).toEqual(['render', 'watcher 1', 'watcher 2', 'render'])
  450. })
  451. // #1852
  452. it('flush: post watcher should fire after template refs updated', async () => {
  453. const toggle = ref(false)
  454. let dom: TestElement | null = null
  455. const App = {
  456. setup() {
  457. const domRef = ref<TestElement | null>(null)
  458. watch(
  459. toggle,
  460. () => {
  461. dom = domRef.value
  462. },
  463. { flush: 'post' }
  464. )
  465. return () => {
  466. return toggle.value ? h('p', { ref: domRef }) : null
  467. }
  468. }
  469. }
  470. render(h(App), nodeOps.createElement('div'))
  471. expect(dom).toBe(null)
  472. toggle.value = true
  473. await nextTick()
  474. expect(dom!.tag).toBe('p')
  475. })
  476. it('deep', async () => {
  477. const state = reactive({
  478. nested: {
  479. count: ref(0)
  480. },
  481. array: [1, 2, 3],
  482. map: new Map([['a', 1], ['b', 2]]),
  483. set: new Set([1, 2, 3])
  484. })
  485. let dummy
  486. watch(
  487. () => state,
  488. state => {
  489. dummy = [
  490. state.nested.count,
  491. state.array[0],
  492. state.map.get('a'),
  493. state.set.has(1)
  494. ]
  495. },
  496. { deep: true }
  497. )
  498. state.nested.count++
  499. await nextTick()
  500. expect(dummy).toEqual([1, 1, 1, true])
  501. // nested array mutation
  502. state.array[0] = 2
  503. await nextTick()
  504. expect(dummy).toEqual([1, 2, 1, true])
  505. // nested map mutation
  506. state.map.set('a', 2)
  507. await nextTick()
  508. expect(dummy).toEqual([1, 2, 2, true])
  509. // nested set mutation
  510. state.set.delete(1)
  511. await nextTick()
  512. expect(dummy).toEqual([1, 2, 2, false])
  513. })
  514. it('watching deep ref', async () => {
  515. const count = ref(0)
  516. const double = computed(() => count.value * 2)
  517. const state = reactive([count, double])
  518. let dummy
  519. watch(
  520. () => state,
  521. state => {
  522. dummy = [state[0].value, state[1].value]
  523. },
  524. { deep: true }
  525. )
  526. count.value++
  527. await nextTick()
  528. expect(dummy).toEqual([1, 2])
  529. })
  530. it('immediate', async () => {
  531. const count = ref(0)
  532. const cb = jest.fn()
  533. watch(count, cb, { immediate: true })
  534. expect(cb).toHaveBeenCalledTimes(1)
  535. count.value++
  536. await nextTick()
  537. expect(cb).toHaveBeenCalledTimes(2)
  538. })
  539. it('immediate: triggers when initial value is null', async () => {
  540. const state = ref(null)
  541. const spy = jest.fn()
  542. watch(() => state.value, spy, { immediate: true })
  543. expect(spy).toHaveBeenCalled()
  544. })
  545. it('immediate: triggers when initial value is undefined', async () => {
  546. const state = ref()
  547. const spy = jest.fn()
  548. watch(() => state.value, spy, { immediate: true })
  549. expect(spy).toHaveBeenCalled()
  550. state.value = 3
  551. await nextTick()
  552. expect(spy).toHaveBeenCalledTimes(2)
  553. // testing if undefined can trigger the watcher
  554. state.value = undefined
  555. await nextTick()
  556. expect(spy).toHaveBeenCalledTimes(3)
  557. // it shouldn't trigger if the same value is set
  558. state.value = undefined
  559. await nextTick()
  560. expect(spy).toHaveBeenCalledTimes(3)
  561. })
  562. it('warn immediate option when using effect', async () => {
  563. const count = ref(0)
  564. let dummy
  565. watchEffect(
  566. () => {
  567. dummy = count.value
  568. },
  569. // @ts-ignore
  570. { immediate: false }
  571. )
  572. expect(dummy).toBe(0)
  573. expect(`"immediate" option is only respected`).toHaveBeenWarned()
  574. count.value++
  575. await nextTick()
  576. expect(dummy).toBe(1)
  577. })
  578. it('warn and not respect deep option when using effect', async () => {
  579. const arr = ref([1, [2]])
  580. const spy = jest.fn()
  581. watchEffect(
  582. () => {
  583. spy()
  584. return arr
  585. },
  586. // @ts-ignore
  587. { deep: true }
  588. )
  589. expect(spy).toHaveBeenCalledTimes(1)
  590. ;(arr.value[1] as Array<number>)[0] = 3
  591. await nextTick()
  592. expect(spy).toHaveBeenCalledTimes(1)
  593. expect(`"deep" option is only respected`).toHaveBeenWarned()
  594. })
  595. it('onTrack', async () => {
  596. const events: DebuggerEvent[] = []
  597. let dummy
  598. const onTrack = jest.fn((e: DebuggerEvent) => {
  599. events.push(e)
  600. })
  601. const obj = reactive({ foo: 1, bar: 2 })
  602. watchEffect(
  603. () => {
  604. dummy = [obj.foo, 'bar' in obj, Object.keys(obj)]
  605. },
  606. { onTrack }
  607. )
  608. await nextTick()
  609. expect(dummy).toEqual([1, true, ['foo', 'bar']])
  610. expect(onTrack).toHaveBeenCalledTimes(3)
  611. expect(events).toMatchObject([
  612. {
  613. target: obj,
  614. type: TrackOpTypes.GET,
  615. key: 'foo'
  616. },
  617. {
  618. target: obj,
  619. type: TrackOpTypes.HAS,
  620. key: 'bar'
  621. },
  622. {
  623. target: obj,
  624. type: TrackOpTypes.ITERATE,
  625. key: ITERATE_KEY
  626. }
  627. ])
  628. })
  629. it('onTrigger', async () => {
  630. const events: DebuggerEvent[] = []
  631. let dummy
  632. const onTrigger = jest.fn((e: DebuggerEvent) => {
  633. events.push(e)
  634. })
  635. const obj = reactive({ foo: 1 })
  636. watchEffect(
  637. () => {
  638. dummy = obj.foo
  639. },
  640. { onTrigger }
  641. )
  642. await nextTick()
  643. expect(dummy).toBe(1)
  644. obj.foo++
  645. await nextTick()
  646. expect(dummy).toBe(2)
  647. expect(onTrigger).toHaveBeenCalledTimes(1)
  648. expect(events[0]).toMatchObject({
  649. type: TriggerOpTypes.SET,
  650. key: 'foo',
  651. oldValue: 1,
  652. newValue: 2
  653. })
  654. // @ts-ignore
  655. delete obj.foo
  656. await nextTick()
  657. expect(dummy).toBeUndefined()
  658. expect(onTrigger).toHaveBeenCalledTimes(2)
  659. expect(events[1]).toMatchObject({
  660. type: TriggerOpTypes.DELETE,
  661. key: 'foo',
  662. oldValue: 2
  663. })
  664. })
  665. it('should work sync', () => {
  666. const v = ref(1)
  667. let calls = 0
  668. watch(
  669. v,
  670. () => {
  671. ++calls
  672. },
  673. {
  674. flush: 'sync'
  675. }
  676. )
  677. expect(calls).toBe(0)
  678. v.value++
  679. expect(calls).toBe(1)
  680. })
  681. test('should force trigger on triggerRef when watching a shallow ref', async () => {
  682. const v = shallowRef({ a: 1 })
  683. let sideEffect = 0
  684. watch(v, obj => {
  685. sideEffect = obj.a
  686. })
  687. v.value = v.value
  688. await nextTick()
  689. // should not trigger
  690. expect(sideEffect).toBe(0)
  691. v.value.a++
  692. await nextTick()
  693. // should not trigger
  694. expect(sideEffect).toBe(0)
  695. triggerRef(v)
  696. await nextTick()
  697. // should trigger now
  698. expect(sideEffect).toBe(2)
  699. })
  700. // #2125
  701. test('watchEffect should not recursively trigger itself', async () => {
  702. const spy = jest.fn()
  703. const price = ref(10)
  704. const history = ref<number[]>([])
  705. watchEffect(() => {
  706. history.value.push(price.value)
  707. spy()
  708. })
  709. await nextTick()
  710. expect(spy).toHaveBeenCalledTimes(1)
  711. })
  712. // #2231
  713. test('computed refs should not trigger watch if value has no change', async () => {
  714. const spy = jest.fn()
  715. const source = ref(0)
  716. const price = computed(() => source.value === 0)
  717. watch(price, spy)
  718. source.value++
  719. await nextTick()
  720. source.value++
  721. await nextTick()
  722. expect(spy).toHaveBeenCalledTimes(1)
  723. })
  724. // https://github.com/vuejs/vue-next/issues/2381
  725. test('$watch should always register its effects with itw own instance', async () => {
  726. let instance: ComponentInternalInstance | null
  727. let _show: Ref<boolean>
  728. const Child = defineComponent({
  729. render: () => h('div'),
  730. mounted() {
  731. instance = getCurrentInstance()
  732. },
  733. unmounted() {}
  734. })
  735. const Comp = defineComponent({
  736. setup() {
  737. const comp = ref<ComponentPublicInstance | undefined>()
  738. const show = ref(true)
  739. _show = show
  740. return { comp, show }
  741. },
  742. render() {
  743. return this.show
  744. ? h(Child, {
  745. ref: vm => void (this.comp = vm as ComponentPublicInstance)
  746. })
  747. : null
  748. },
  749. mounted() {
  750. // this call runs while Comp is currentInstance, but
  751. // the effect for this `$watch` should nontheless be registered with Child
  752. this.comp!.$watch(() => this.show, () => void 0)
  753. }
  754. })
  755. render(h(Comp), nodeOps.createElement('div'))
  756. expect(instance!).toBeDefined()
  757. expect(instance!.effects).toBeInstanceOf(Array)
  758. expect(instance!.effects!.length).toBe(1)
  759. _show!.value = false
  760. await nextTick()
  761. await nextTick()
  762. expect(instance!.effects![0].active).toBe(false)
  763. })
  764. })