effect.spec.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. import {
  2. reactive,
  3. effect,
  4. stop,
  5. toRaw,
  6. TrackOpTypes,
  7. TriggerOpTypes,
  8. DebuggerEvent,
  9. markRaw
  10. } from '../src/index'
  11. import { ITERATE_KEY } from '../src/effect'
  12. describe('reactivity/effect', () => {
  13. it('should run the passed function once (wrapped by a effect)', () => {
  14. const fnSpy = jest.fn(() => {})
  15. effect(fnSpy)
  16. expect(fnSpy).toHaveBeenCalledTimes(1)
  17. })
  18. it('should observe basic properties', () => {
  19. let dummy
  20. const counter = reactive({ num: 0 })
  21. effect(() => (dummy = counter.num))
  22. expect(dummy).toBe(0)
  23. counter.num = 7
  24. expect(dummy).toBe(7)
  25. })
  26. it('should observe multiple properties', () => {
  27. let dummy
  28. const counter = reactive({ num1: 0, num2: 0 })
  29. effect(() => (dummy = counter.num1 + counter.num1 + counter.num2))
  30. expect(dummy).toBe(0)
  31. counter.num1 = counter.num2 = 7
  32. expect(dummy).toBe(21)
  33. })
  34. it('should handle multiple effects', () => {
  35. let dummy1, dummy2
  36. const counter = reactive({ num: 0 })
  37. effect(() => (dummy1 = counter.num))
  38. effect(() => (dummy2 = counter.num))
  39. expect(dummy1).toBe(0)
  40. expect(dummy2).toBe(0)
  41. counter.num++
  42. expect(dummy1).toBe(1)
  43. expect(dummy2).toBe(1)
  44. })
  45. it('should observe nested properties', () => {
  46. let dummy
  47. const counter = reactive({ nested: { num: 0 } })
  48. effect(() => (dummy = counter.nested.num))
  49. expect(dummy).toBe(0)
  50. counter.nested.num = 8
  51. expect(dummy).toBe(8)
  52. })
  53. it('should observe delete operations', () => {
  54. let dummy
  55. const obj = reactive({ prop: 'value' })
  56. effect(() => (dummy = obj.prop))
  57. expect(dummy).toBe('value')
  58. // @ts-ignore
  59. delete obj.prop
  60. expect(dummy).toBe(undefined)
  61. })
  62. it('should observe has operations', () => {
  63. let dummy
  64. const obj = reactive<{ prop: string | number }>({ prop: 'value' })
  65. effect(() => (dummy = 'prop' in obj))
  66. expect(dummy).toBe(true)
  67. // @ts-ignore
  68. delete obj.prop
  69. expect(dummy).toBe(false)
  70. obj.prop = 12
  71. expect(dummy).toBe(true)
  72. })
  73. it('should observe properties on the prototype chain', () => {
  74. let dummy
  75. const counter = reactive({ num: 0 })
  76. const parentCounter = reactive({ num: 2 })
  77. Object.setPrototypeOf(counter, parentCounter)
  78. effect(() => (dummy = counter.num))
  79. expect(dummy).toBe(0)
  80. // @ts-ignore
  81. delete counter.num
  82. expect(dummy).toBe(2)
  83. parentCounter.num = 4
  84. expect(dummy).toBe(4)
  85. counter.num = 3
  86. expect(dummy).toBe(3)
  87. })
  88. it('should observe has operations on the prototype chain', () => {
  89. let dummy
  90. const counter = reactive({ num: 0 })
  91. const parentCounter = reactive({ num: 2 })
  92. Object.setPrototypeOf(counter, parentCounter)
  93. effect(() => (dummy = 'num' in counter))
  94. expect(dummy).toBe(true)
  95. // @ts-ignore
  96. delete counter.num
  97. expect(dummy).toBe(true)
  98. // @ts-ignore
  99. delete parentCounter.num
  100. expect(dummy).toBe(false)
  101. counter.num = 3
  102. expect(dummy).toBe(true)
  103. })
  104. it('should observe inherited property accessors', () => {
  105. let dummy, parentDummy, hiddenValue: any
  106. const obj = reactive<{ prop?: number }>({})
  107. const parent = reactive({
  108. set prop(value) {
  109. hiddenValue = value
  110. },
  111. get prop() {
  112. return hiddenValue
  113. }
  114. })
  115. Object.setPrototypeOf(obj, parent)
  116. effect(() => (dummy = obj.prop))
  117. effect(() => (parentDummy = parent.prop))
  118. expect(dummy).toBe(undefined)
  119. expect(parentDummy).toBe(undefined)
  120. obj.prop = 4
  121. expect(dummy).toBe(4)
  122. // this doesn't work, should it?
  123. // expect(parentDummy).toBe(4)
  124. parent.prop = 2
  125. expect(dummy).toBe(2)
  126. expect(parentDummy).toBe(2)
  127. })
  128. it('should observe function call chains', () => {
  129. let dummy
  130. const counter = reactive({ num: 0 })
  131. effect(() => (dummy = getNum()))
  132. function getNum() {
  133. return counter.num
  134. }
  135. expect(dummy).toBe(0)
  136. counter.num = 2
  137. expect(dummy).toBe(2)
  138. })
  139. it('should observe iteration', () => {
  140. let dummy
  141. const list = reactive(['Hello'])
  142. effect(() => (dummy = list.join(' ')))
  143. expect(dummy).toBe('Hello')
  144. list.push('World!')
  145. expect(dummy).toBe('Hello World!')
  146. list.shift()
  147. expect(dummy).toBe('World!')
  148. })
  149. it('should observe implicit array length changes', () => {
  150. let dummy
  151. const list = reactive(['Hello'])
  152. effect(() => (dummy = list.join(' ')))
  153. expect(dummy).toBe('Hello')
  154. list[1] = 'World!'
  155. expect(dummy).toBe('Hello World!')
  156. list[3] = 'Hello!'
  157. expect(dummy).toBe('Hello World! Hello!')
  158. })
  159. it('should observe sparse array mutations', () => {
  160. let dummy
  161. const list = reactive<string[]>([])
  162. list[1] = 'World!'
  163. effect(() => (dummy = list.join(' ')))
  164. expect(dummy).toBe(' World!')
  165. list[0] = 'Hello'
  166. expect(dummy).toBe('Hello World!')
  167. list.pop()
  168. expect(dummy).toBe('Hello')
  169. })
  170. it('should observe enumeration', () => {
  171. let dummy = 0
  172. const numbers = reactive<Record<string, number>>({ num1: 3 })
  173. effect(() => {
  174. dummy = 0
  175. for (let key in numbers) {
  176. dummy += numbers[key]
  177. }
  178. })
  179. expect(dummy).toBe(3)
  180. numbers.num2 = 4
  181. expect(dummy).toBe(7)
  182. delete numbers.num1
  183. expect(dummy).toBe(4)
  184. })
  185. it('should observe symbol keyed properties', () => {
  186. const key = Symbol('symbol keyed prop')
  187. let dummy, hasDummy
  188. const obj = reactive({ [key]: 'value' })
  189. effect(() => (dummy = obj[key]))
  190. effect(() => (hasDummy = key in obj))
  191. expect(dummy).toBe('value')
  192. expect(hasDummy).toBe(true)
  193. obj[key] = 'newValue'
  194. expect(dummy).toBe('newValue')
  195. // @ts-ignore
  196. delete obj[key]
  197. expect(dummy).toBe(undefined)
  198. expect(hasDummy).toBe(false)
  199. })
  200. it('should not observe well-known symbol keyed properties', () => {
  201. const key = Symbol.isConcatSpreadable
  202. let dummy
  203. const array: any = reactive([])
  204. effect(() => (dummy = array[key]))
  205. expect(array[key]).toBe(undefined)
  206. expect(dummy).toBe(undefined)
  207. array[key] = true
  208. expect(array[key]).toBe(true)
  209. expect(dummy).toBe(undefined)
  210. })
  211. it('should observe function valued properties', () => {
  212. const oldFunc = () => {}
  213. const newFunc = () => {}
  214. let dummy
  215. const obj = reactive({ func: oldFunc })
  216. effect(() => (dummy = obj.func))
  217. expect(dummy).toBe(oldFunc)
  218. obj.func = newFunc
  219. expect(dummy).toBe(newFunc)
  220. })
  221. it('should observe chained getters relying on this', () => {
  222. const obj = reactive({
  223. a: 1,
  224. get b() {
  225. return this.a
  226. }
  227. })
  228. let dummy
  229. effect(() => (dummy = obj.b))
  230. expect(dummy).toBe(1)
  231. obj.a++
  232. expect(dummy).toBe(2)
  233. })
  234. it('should observe methods relying on this', () => {
  235. const obj = reactive({
  236. a: 1,
  237. b() {
  238. return this.a
  239. }
  240. })
  241. let dummy
  242. effect(() => (dummy = obj.b()))
  243. expect(dummy).toBe(1)
  244. obj.a++
  245. expect(dummy).toBe(2)
  246. })
  247. it('should not observe set operations without a value change', () => {
  248. let hasDummy, getDummy
  249. const obj = reactive({ prop: 'value' })
  250. const getSpy = jest.fn(() => (getDummy = obj.prop))
  251. const hasSpy = jest.fn(() => (hasDummy = 'prop' in obj))
  252. effect(getSpy)
  253. effect(hasSpy)
  254. expect(getDummy).toBe('value')
  255. expect(hasDummy).toBe(true)
  256. obj.prop = 'value'
  257. expect(getSpy).toHaveBeenCalledTimes(1)
  258. expect(hasSpy).toHaveBeenCalledTimes(1)
  259. expect(getDummy).toBe('value')
  260. expect(hasDummy).toBe(true)
  261. })
  262. it('should not observe raw mutations', () => {
  263. let dummy
  264. const obj = reactive<{ prop?: string }>({})
  265. effect(() => (dummy = toRaw(obj).prop))
  266. expect(dummy).toBe(undefined)
  267. obj.prop = 'value'
  268. expect(dummy).toBe(undefined)
  269. })
  270. it('should not be triggered by raw mutations', () => {
  271. let dummy
  272. const obj = reactive<{ prop?: string }>({})
  273. effect(() => (dummy = obj.prop))
  274. expect(dummy).toBe(undefined)
  275. toRaw(obj).prop = 'value'
  276. expect(dummy).toBe(undefined)
  277. })
  278. it('should not be triggered by inherited raw setters', () => {
  279. let dummy, parentDummy, hiddenValue: any
  280. const obj = reactive<{ prop?: number }>({})
  281. const parent = reactive({
  282. set prop(value) {
  283. hiddenValue = value
  284. },
  285. get prop() {
  286. return hiddenValue
  287. }
  288. })
  289. Object.setPrototypeOf(obj, parent)
  290. effect(() => (dummy = obj.prop))
  291. effect(() => (parentDummy = parent.prop))
  292. expect(dummy).toBe(undefined)
  293. expect(parentDummy).toBe(undefined)
  294. toRaw(obj).prop = 4
  295. expect(dummy).toBe(undefined)
  296. expect(parentDummy).toBe(undefined)
  297. })
  298. it('should avoid implicit infinite recursive loops with itself', () => {
  299. const counter = reactive({ num: 0 })
  300. const counterSpy = jest.fn(() => counter.num++)
  301. effect(counterSpy)
  302. expect(counter.num).toBe(1)
  303. expect(counterSpy).toHaveBeenCalledTimes(1)
  304. counter.num = 4
  305. expect(counter.num).toBe(5)
  306. expect(counterSpy).toHaveBeenCalledTimes(2)
  307. })
  308. it('should allow explicitly recursive raw function loops', () => {
  309. const counter = reactive({ num: 0 })
  310. const numSpy = jest.fn(() => {
  311. counter.num++
  312. if (counter.num < 10) {
  313. numSpy()
  314. }
  315. })
  316. effect(numSpy)
  317. expect(counter.num).toEqual(10)
  318. expect(numSpy).toHaveBeenCalledTimes(10)
  319. })
  320. it('should avoid infinite loops with other effects', () => {
  321. const nums = reactive({ num1: 0, num2: 1 })
  322. const spy1 = jest.fn(() => (nums.num1 = nums.num2))
  323. const spy2 = jest.fn(() => (nums.num2 = nums.num1))
  324. effect(spy1)
  325. effect(spy2)
  326. expect(nums.num1).toBe(1)
  327. expect(nums.num2).toBe(1)
  328. expect(spy1).toHaveBeenCalledTimes(1)
  329. expect(spy2).toHaveBeenCalledTimes(1)
  330. nums.num2 = 4
  331. expect(nums.num1).toBe(4)
  332. expect(nums.num2).toBe(4)
  333. expect(spy1).toHaveBeenCalledTimes(2)
  334. expect(spy2).toHaveBeenCalledTimes(2)
  335. nums.num1 = 10
  336. expect(nums.num1).toBe(10)
  337. expect(nums.num2).toBe(10)
  338. expect(spy1).toHaveBeenCalledTimes(3)
  339. expect(spy2).toHaveBeenCalledTimes(3)
  340. })
  341. it('should return a new reactive version of the function', () => {
  342. function greet() {
  343. return 'Hello World'
  344. }
  345. const effect1 = effect(greet)
  346. const effect2 = effect(greet)
  347. expect(typeof effect1).toBe('function')
  348. expect(typeof effect2).toBe('function')
  349. expect(effect1).not.toBe(greet)
  350. expect(effect1).not.toBe(effect2)
  351. })
  352. it('should discover new branches while running automatically', () => {
  353. let dummy
  354. const obj = reactive({ prop: 'value', run: false })
  355. const conditionalSpy = jest.fn(() => {
  356. dummy = obj.run ? obj.prop : 'other'
  357. })
  358. effect(conditionalSpy)
  359. expect(dummy).toBe('other')
  360. expect(conditionalSpy).toHaveBeenCalledTimes(1)
  361. obj.prop = 'Hi'
  362. expect(dummy).toBe('other')
  363. expect(conditionalSpy).toHaveBeenCalledTimes(1)
  364. obj.run = true
  365. expect(dummy).toBe('Hi')
  366. expect(conditionalSpy).toHaveBeenCalledTimes(2)
  367. obj.prop = 'World'
  368. expect(dummy).toBe('World')
  369. expect(conditionalSpy).toHaveBeenCalledTimes(3)
  370. })
  371. it('should discover new branches when running manually', () => {
  372. let dummy
  373. let run = false
  374. const obj = reactive({ prop: 'value' })
  375. const runner = effect(() => {
  376. dummy = run ? obj.prop : 'other'
  377. })
  378. expect(dummy).toBe('other')
  379. runner()
  380. expect(dummy).toBe('other')
  381. run = true
  382. runner()
  383. expect(dummy).toBe('value')
  384. obj.prop = 'World'
  385. expect(dummy).toBe('World')
  386. })
  387. it('should not be triggered by mutating a property, which is used in an inactive branch', () => {
  388. let dummy
  389. const obj = reactive({ prop: 'value', run: true })
  390. const conditionalSpy = jest.fn(() => {
  391. dummy = obj.run ? obj.prop : 'other'
  392. })
  393. effect(conditionalSpy)
  394. expect(dummy).toBe('value')
  395. expect(conditionalSpy).toHaveBeenCalledTimes(1)
  396. obj.run = false
  397. expect(dummy).toBe('other')
  398. expect(conditionalSpy).toHaveBeenCalledTimes(2)
  399. obj.prop = 'value2'
  400. expect(dummy).toBe('other')
  401. expect(conditionalSpy).toHaveBeenCalledTimes(2)
  402. })
  403. it('should not double wrap if the passed function is a effect', () => {
  404. const runner = effect(() => {})
  405. const otherRunner = effect(runner)
  406. expect(runner).not.toBe(otherRunner)
  407. expect(runner.raw).toBe(otherRunner.raw)
  408. })
  409. it('should not run multiple times for a single mutation', () => {
  410. let dummy
  411. const obj = reactive<Record<string, number>>({})
  412. const fnSpy = jest.fn(() => {
  413. for (const key in obj) {
  414. dummy = obj[key]
  415. }
  416. dummy = obj.prop
  417. })
  418. effect(fnSpy)
  419. expect(fnSpy).toHaveBeenCalledTimes(1)
  420. obj.prop = 16
  421. expect(dummy).toBe(16)
  422. expect(fnSpy).toHaveBeenCalledTimes(2)
  423. })
  424. it('should allow nested effects', () => {
  425. const nums = reactive({ num1: 0, num2: 1, num3: 2 })
  426. const dummy: any = {}
  427. const childSpy = jest.fn(() => (dummy.num1 = nums.num1))
  428. const childeffect = effect(childSpy)
  429. const parentSpy = jest.fn(() => {
  430. dummy.num2 = nums.num2
  431. childeffect()
  432. dummy.num3 = nums.num3
  433. })
  434. effect(parentSpy)
  435. expect(dummy).toEqual({ num1: 0, num2: 1, num3: 2 })
  436. expect(parentSpy).toHaveBeenCalledTimes(1)
  437. expect(childSpy).toHaveBeenCalledTimes(2)
  438. // this should only call the childeffect
  439. nums.num1 = 4
  440. expect(dummy).toEqual({ num1: 4, num2: 1, num3: 2 })
  441. expect(parentSpy).toHaveBeenCalledTimes(1)
  442. expect(childSpy).toHaveBeenCalledTimes(3)
  443. // this calls the parenteffect, which calls the childeffect once
  444. nums.num2 = 10
  445. expect(dummy).toEqual({ num1: 4, num2: 10, num3: 2 })
  446. expect(parentSpy).toHaveBeenCalledTimes(2)
  447. expect(childSpy).toHaveBeenCalledTimes(4)
  448. // this calls the parenteffect, which calls the childeffect once
  449. nums.num3 = 7
  450. expect(dummy).toEqual({ num1: 4, num2: 10, num3: 7 })
  451. expect(parentSpy).toHaveBeenCalledTimes(3)
  452. expect(childSpy).toHaveBeenCalledTimes(5)
  453. })
  454. it('should observe json methods', () => {
  455. let dummy = <Record<string, number>>{}
  456. const obj = reactive<Record<string, number>>({})
  457. effect(() => {
  458. dummy = JSON.parse(JSON.stringify(obj))
  459. })
  460. obj.a = 1
  461. expect(dummy.a).toBe(1)
  462. })
  463. it('should observe class method invocations', () => {
  464. class Model {
  465. count: number
  466. constructor() {
  467. this.count = 0
  468. }
  469. inc() {
  470. this.count++
  471. }
  472. }
  473. const model = reactive(new Model())
  474. let dummy
  475. effect(() => {
  476. dummy = model.count
  477. })
  478. expect(dummy).toBe(0)
  479. model.inc()
  480. expect(dummy).toBe(1)
  481. })
  482. it('lazy', () => {
  483. const obj = reactive({ foo: 1 })
  484. let dummy
  485. const runner = effect(() => (dummy = obj.foo), { lazy: true })
  486. expect(dummy).toBe(undefined)
  487. expect(runner()).toBe(1)
  488. expect(dummy).toBe(1)
  489. obj.foo = 2
  490. expect(dummy).toBe(2)
  491. })
  492. it('scheduler', () => {
  493. let runner: any, dummy
  494. const scheduler = jest.fn(_runner => {
  495. runner = _runner
  496. })
  497. const obj = reactive({ foo: 1 })
  498. effect(
  499. () => {
  500. dummy = obj.foo
  501. },
  502. { scheduler }
  503. )
  504. expect(scheduler).not.toHaveBeenCalled()
  505. expect(dummy).toBe(1)
  506. // should be called on first trigger
  507. obj.foo++
  508. expect(scheduler).toHaveBeenCalledTimes(1)
  509. // should not run yet
  510. expect(dummy).toBe(1)
  511. // manually run
  512. runner()
  513. // should have run
  514. expect(dummy).toBe(2)
  515. })
  516. it('events: onTrack', () => {
  517. let events: DebuggerEvent[] = []
  518. let dummy
  519. const onTrack = jest.fn((e: DebuggerEvent) => {
  520. events.push(e)
  521. })
  522. const obj = reactive({ foo: 1, bar: 2 })
  523. const runner = effect(
  524. () => {
  525. dummy = obj.foo
  526. dummy = 'bar' in obj
  527. dummy = Object.keys(obj)
  528. },
  529. { onTrack }
  530. )
  531. expect(dummy).toEqual(['foo', 'bar'])
  532. expect(onTrack).toHaveBeenCalledTimes(3)
  533. expect(events).toEqual([
  534. {
  535. effect: runner,
  536. target: toRaw(obj),
  537. type: TrackOpTypes.GET,
  538. key: 'foo'
  539. },
  540. {
  541. effect: runner,
  542. target: toRaw(obj),
  543. type: TrackOpTypes.HAS,
  544. key: 'bar'
  545. },
  546. {
  547. effect: runner,
  548. target: toRaw(obj),
  549. type: TrackOpTypes.ITERATE,
  550. key: ITERATE_KEY
  551. }
  552. ])
  553. })
  554. it('events: onTrigger', () => {
  555. let events: DebuggerEvent[] = []
  556. let dummy
  557. const onTrigger = jest.fn((e: DebuggerEvent) => {
  558. events.push(e)
  559. })
  560. const obj = reactive({ foo: 1 })
  561. const runner = effect(
  562. () => {
  563. dummy = obj.foo
  564. },
  565. { onTrigger }
  566. )
  567. obj.foo++
  568. expect(dummy).toBe(2)
  569. expect(onTrigger).toHaveBeenCalledTimes(1)
  570. expect(events[0]).toEqual({
  571. effect: runner,
  572. target: toRaw(obj),
  573. type: TriggerOpTypes.SET,
  574. key: 'foo',
  575. oldValue: 1,
  576. newValue: 2
  577. })
  578. // @ts-ignore
  579. delete obj.foo
  580. expect(dummy).toBeUndefined()
  581. expect(onTrigger).toHaveBeenCalledTimes(2)
  582. expect(events[1]).toEqual({
  583. effect: runner,
  584. target: toRaw(obj),
  585. type: TriggerOpTypes.DELETE,
  586. key: 'foo',
  587. oldValue: 2
  588. })
  589. })
  590. it('stop', () => {
  591. let dummy
  592. const obj = reactive({ prop: 1 })
  593. const runner = effect(() => {
  594. dummy = obj.prop
  595. })
  596. obj.prop = 2
  597. expect(dummy).toBe(2)
  598. stop(runner)
  599. obj.prop = 3
  600. expect(dummy).toBe(2)
  601. // stopped effect should still be manually callable
  602. runner()
  603. expect(dummy).toBe(3)
  604. })
  605. it('stop with scheduler', () => {
  606. let dummy
  607. const obj = reactive({ prop: 1 })
  608. const queue: (() => void)[] = []
  609. const runner = effect(
  610. () => {
  611. dummy = obj.prop
  612. },
  613. {
  614. scheduler: e => queue.push(e)
  615. }
  616. )
  617. obj.prop = 2
  618. expect(dummy).toBe(1)
  619. expect(queue.length).toBe(1)
  620. stop(runner)
  621. // a scheduled effect should not execute anymore after stopped
  622. queue.forEach(e => e())
  623. expect(dummy).toBe(1)
  624. })
  625. it('events: onStop', () => {
  626. const onStop = jest.fn()
  627. const runner = effect(() => {}, {
  628. onStop
  629. })
  630. stop(runner)
  631. expect(onStop).toHaveBeenCalled()
  632. })
  633. it('stop: a stopped effect is nested in a normal effect', () => {
  634. let dummy
  635. const obj = reactive({ prop: 1 })
  636. const runner = effect(() => {
  637. dummy = obj.prop
  638. })
  639. stop(runner)
  640. obj.prop = 2
  641. expect(dummy).toBe(1)
  642. // observed value in inner stopped effect
  643. // will track outer effect as an dependency
  644. effect(() => {
  645. runner()
  646. })
  647. expect(dummy).toBe(2)
  648. // notify outer effect to run
  649. obj.prop = 3
  650. expect(dummy).toBe(3)
  651. })
  652. it('markRaw', () => {
  653. const obj = reactive({
  654. foo: markRaw({
  655. prop: 0
  656. })
  657. })
  658. let dummy
  659. effect(() => {
  660. dummy = obj.foo.prop
  661. })
  662. expect(dummy).toBe(0)
  663. obj.foo.prop++
  664. expect(dummy).toBe(0)
  665. obj.foo = { prop: 1 }
  666. expect(dummy).toBe(1)
  667. })
  668. it('should not be trigger when the value and the old value both are NaN', () => {
  669. const obj = reactive({
  670. foo: NaN
  671. })
  672. const fnSpy = jest.fn(() => obj.foo)
  673. effect(fnSpy)
  674. obj.foo = NaN
  675. expect(fnSpy).toHaveBeenCalledTimes(1)
  676. })
  677. it('should trigger all effects when array length is set to 0', () => {
  678. const observed: any = reactive([1])
  679. let dummy, record
  680. effect(() => {
  681. dummy = observed.length
  682. })
  683. effect(() => {
  684. record = observed[0]
  685. })
  686. expect(dummy).toBe(1)
  687. expect(record).toBe(1)
  688. observed[1] = 2
  689. expect(observed[1]).toBe(2)
  690. observed.unshift(3)
  691. expect(dummy).toBe(3)
  692. expect(record).toBe(3)
  693. observed.length = 0
  694. expect(dummy).toBe(0)
  695. expect(record).toBeUndefined()
  696. })
  697. })