effect.spec.ts 17 KB

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