autorun.spec.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. import {
  2. observable,
  3. autorun,
  4. stop,
  5. unwrap,
  6. OperationTypes,
  7. DebuggerEvent,
  8. markNonReactive
  9. } from '../src/index'
  10. import { ITERATE_KEY } from '../src/autorun'
  11. describe('observer/autorun', () => {
  12. it('should run the passed function once (wrapped by a autorun)', () => {
  13. const fnSpy = jest.fn(() => {})
  14. autorun(fnSpy)
  15. expect(fnSpy).toHaveBeenCalledTimes(1)
  16. })
  17. it('should observe basic properties', () => {
  18. let dummy
  19. const counter = observable({ num: 0 })
  20. autorun(() => (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 = observable({ num1: 0, num2: 0 })
  28. autorun(() => (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 autoruns', () => {
  34. let dummy1, dummy2
  35. const counter = observable({ num: 0 })
  36. autorun(() => (dummy1 = counter.num))
  37. autorun(() => (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 = observable({ nested: { num: 0 } })
  47. autorun(() => (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 = observable({ prop: 'value' })
  55. autorun(() => (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: any = observable({ prop: 'value' })
  63. autorun(() => (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 = observable({ num: 0 })
  73. const parentCounter = observable({ num: 2 })
  74. Object.setPrototypeOf(counter, parentCounter)
  75. autorun(() => (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 = observable({ num: 0 })
  87. const parentCounter = observable({ num: 2 })
  88. Object.setPrototypeOf(counter, parentCounter)
  89. autorun(() => (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: any = observable({})
  101. const parent = observable({
  102. set prop(value) {
  103. hiddenValue = value
  104. },
  105. get prop() {
  106. return hiddenValue
  107. }
  108. })
  109. Object.setPrototypeOf(obj, parent)
  110. autorun(() => (dummy = obj.prop))
  111. autorun(() => (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 = observable({ num: 0 })
  125. autorun(() => (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 = observable(['Hello'])
  136. autorun(() => (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 = observable(['Hello'])
  146. autorun(() => (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: any[] = observable([])
  156. list[1] = 'World!'
  157. autorun(() => (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: any = observable({ num1: 3 })
  167. autorun(() => {
  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 = observable({ [key]: 'value' })
  183. autorun(() => (dummy = obj[key]))
  184. autorun(() => (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 = observable([])
  197. autorun(() => (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 = observable({ func: oldFunc })
  209. autorun(() => (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 = observable({ prop: 'value' })
  217. const getSpy = jest.fn(() => (getDummy = obj.prop))
  218. const hasSpy = jest.fn(() => (hasDummy = 'prop' in obj))
  219. autorun(getSpy)
  220. autorun(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: any = observable()
  232. autorun(() => (dummy = unwrap(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: any = observable()
  240. autorun(() => (dummy = obj.prop))
  241. expect(dummy).toBe(undefined)
  242. unwrap(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: any = observable({})
  248. const parent = observable({
  249. set prop(value) {
  250. hiddenValue = value
  251. },
  252. get prop() {
  253. return hiddenValue
  254. }
  255. })
  256. Object.setPrototypeOf(obj, parent)
  257. autorun(() => (dummy = obj.prop))
  258. autorun(() => (parentDummy = parent.prop))
  259. expect(dummy).toBe(undefined)
  260. expect(parentDummy).toBe(undefined)
  261. unwrap(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 = observable({ num: 0 })
  267. const counterSpy = jest.fn(() => counter.num++)
  268. autorun(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 = observable({ num: 0 })
  277. // TODO: this should be changed to autorun loops, can it be done?
  278. const numSpy = jest.fn(() => {
  279. counter.num++
  280. if (counter.num < 10) {
  281. numSpy()
  282. }
  283. })
  284. autorun(numSpy)
  285. expect(counter.num).toEqual(10)
  286. expect(numSpy).toHaveBeenCalledTimes(10)
  287. })
  288. it('should avoid infinite loops with other autoruns', () => {
  289. const nums = observable({ num1: 0, num2: 1 })
  290. const spy1 = jest.fn(() => (nums.num1 = nums.num2))
  291. const spy2 = jest.fn(() => (nums.num2 = nums.num1))
  292. autorun(spy1)
  293. autorun(spy2)
  294. expect(nums.num1).toBe(1)
  295. expect(nums.num2).toBe(1)
  296. expect(spy1).toHaveBeenCalledTimes(1)
  297. expect(spy2).toHaveBeenCalledTimes(1)
  298. nums.num2 = 4
  299. expect(nums.num1).toBe(4)
  300. expect(nums.num2).toBe(4)
  301. expect(spy1).toHaveBeenCalledTimes(2)
  302. expect(spy2).toHaveBeenCalledTimes(2)
  303. nums.num1 = 10
  304. expect(nums.num1).toBe(10)
  305. expect(nums.num2).toBe(10)
  306. expect(spy1).toHaveBeenCalledTimes(3)
  307. expect(spy2).toHaveBeenCalledTimes(3)
  308. })
  309. it('should return a new reactive version of the function', () => {
  310. function greet() {
  311. return 'Hello World'
  312. }
  313. const autorun1 = autorun(greet)
  314. const autorun2 = autorun(greet)
  315. expect(typeof autorun1).toBe('function')
  316. expect(typeof autorun2).toBe('function')
  317. expect(autorun1).not.toBe(greet)
  318. expect(autorun1).not.toBe(autorun2)
  319. })
  320. it('should discover new branches while running automatically', () => {
  321. let dummy
  322. const obj = observable({ prop: 'value', run: false })
  323. const conditionalSpy = jest.fn(() => {
  324. dummy = obj.run ? obj.prop : 'other'
  325. })
  326. autorun(conditionalSpy)
  327. expect(dummy).toBe('other')
  328. expect(conditionalSpy).toHaveBeenCalledTimes(1)
  329. obj.prop = 'Hi'
  330. expect(dummy).toBe('other')
  331. expect(conditionalSpy).toHaveBeenCalledTimes(1)
  332. obj.run = true
  333. expect(dummy).toBe('Hi')
  334. expect(conditionalSpy).toHaveBeenCalledTimes(2)
  335. obj.prop = 'World'
  336. expect(dummy).toBe('World')
  337. expect(conditionalSpy).toHaveBeenCalledTimes(3)
  338. })
  339. it('should discover new branches when running manually', () => {
  340. let dummy
  341. let run = false
  342. const obj = observable({ prop: 'value' })
  343. const runner = autorun(() => {
  344. dummy = run ? obj.prop : 'other'
  345. })
  346. expect(dummy).toBe('other')
  347. runner()
  348. expect(dummy).toBe('other')
  349. run = true
  350. runner()
  351. expect(dummy).toBe('value')
  352. obj.prop = 'World'
  353. expect(dummy).toBe('World')
  354. })
  355. it('should not be triggered by mutating a property, which is used in an inactive branch', () => {
  356. let dummy
  357. const obj = observable({ prop: 'value', run: true })
  358. const conditionalSpy = jest.fn(() => {
  359. dummy = obj.run ? obj.prop : 'other'
  360. })
  361. autorun(conditionalSpy)
  362. expect(dummy).toBe('value')
  363. expect(conditionalSpy).toHaveBeenCalledTimes(1)
  364. obj.run = false
  365. expect(dummy).toBe('other')
  366. expect(conditionalSpy).toHaveBeenCalledTimes(2)
  367. obj.prop = 'value2'
  368. expect(dummy).toBe('other')
  369. expect(conditionalSpy).toHaveBeenCalledTimes(2)
  370. })
  371. it('should not double wrap if the passed function is a autorun', () => {
  372. const runner = autorun(() => {})
  373. const otherRunner = autorun(runner)
  374. expect(runner).not.toBe(otherRunner)
  375. expect(runner.raw).toBe(otherRunner.raw)
  376. })
  377. it('should not run multiple times for a single mutation', () => {
  378. let dummy
  379. const obj: any = observable()
  380. const fnSpy = jest.fn(() => {
  381. for (const key in obj) {
  382. dummy = obj[key]
  383. }
  384. dummy = obj.prop
  385. })
  386. autorun(fnSpy)
  387. expect(fnSpy).toHaveBeenCalledTimes(1)
  388. obj.prop = 16
  389. expect(dummy).toBe(16)
  390. expect(fnSpy).toHaveBeenCalledTimes(2)
  391. })
  392. it('should allow nested autoruns', () => {
  393. const nums = observable({ num1: 0, num2: 1, num3: 2 })
  394. const dummy: any = {}
  395. const childSpy = jest.fn(() => (dummy.num1 = nums.num1))
  396. const childautorun = autorun(childSpy)
  397. const parentSpy = jest.fn(() => {
  398. dummy.num2 = nums.num2
  399. childautorun()
  400. dummy.num3 = nums.num3
  401. })
  402. autorun(parentSpy)
  403. expect(dummy).toEqual({ num1: 0, num2: 1, num3: 2 })
  404. expect(parentSpy).toHaveBeenCalledTimes(1)
  405. expect(childSpy).toHaveBeenCalledTimes(2)
  406. // this should only call the childautorun
  407. nums.num1 = 4
  408. expect(dummy).toEqual({ num1: 4, num2: 1, num3: 2 })
  409. expect(parentSpy).toHaveBeenCalledTimes(1)
  410. expect(childSpy).toHaveBeenCalledTimes(3)
  411. // this calls the parentautorun, which calls the childautorun once
  412. nums.num2 = 10
  413. expect(dummy).toEqual({ num1: 4, num2: 10, num3: 2 })
  414. expect(parentSpy).toHaveBeenCalledTimes(2)
  415. expect(childSpy).toHaveBeenCalledTimes(4)
  416. // this calls the parentautorun, which calls the childautorun once
  417. nums.num3 = 7
  418. expect(dummy).toEqual({ num1: 4, num2: 10, num3: 7 })
  419. expect(parentSpy).toHaveBeenCalledTimes(3)
  420. expect(childSpy).toHaveBeenCalledTimes(5)
  421. })
  422. it('should observe class method invocations', () => {
  423. class Model {
  424. count: number
  425. constructor() {
  426. this.count = 0
  427. }
  428. inc() {
  429. this.count++
  430. }
  431. }
  432. const model = observable(new Model())
  433. let dummy
  434. autorun(() => {
  435. dummy = model.count
  436. })
  437. expect(dummy).toBe(0)
  438. model.inc()
  439. expect(dummy).toBe(1)
  440. })
  441. it('scheduler', () => {
  442. let runner: any, dummy
  443. const scheduler = jest.fn(_runner => {
  444. runner = _runner
  445. })
  446. const obj = observable({ foo: 1 })
  447. autorun(
  448. () => {
  449. dummy = obj.foo
  450. },
  451. { scheduler }
  452. )
  453. expect(scheduler).not.toHaveBeenCalled()
  454. expect(dummy).toBe(1)
  455. // should be called on first trigger
  456. obj.foo++
  457. expect(scheduler).toHaveBeenCalledTimes(1)
  458. // should not run yet
  459. expect(dummy).toBe(1)
  460. // manually run
  461. runner()
  462. // should have run
  463. expect(dummy).toBe(2)
  464. })
  465. it('events: onTrack', () => {
  466. let events: any[] = []
  467. let dummy
  468. const onTrack = jest.fn((e: DebuggerEvent) => {
  469. events.push(e)
  470. })
  471. const obj = observable({ foo: 1, bar: 2 })
  472. const runner = autorun(
  473. () => {
  474. dummy = obj.foo
  475. dummy = 'bar' in obj
  476. dummy = Object.keys(obj)
  477. },
  478. { onTrack }
  479. )
  480. expect(dummy).toEqual(['foo', 'bar'])
  481. expect(onTrack).toHaveBeenCalledTimes(3)
  482. expect(events).toEqual([
  483. {
  484. runner,
  485. target: unwrap(obj),
  486. type: OperationTypes.GET,
  487. key: 'foo'
  488. },
  489. {
  490. runner,
  491. target: unwrap(obj),
  492. type: OperationTypes.HAS,
  493. key: 'bar'
  494. },
  495. {
  496. runner,
  497. target: unwrap(obj),
  498. type: OperationTypes.ITERATE,
  499. key: ITERATE_KEY
  500. }
  501. ])
  502. })
  503. it('events: onTrigger', () => {
  504. let events: any[] = []
  505. let dummy
  506. const onTrigger = jest.fn((e: DebuggerEvent) => {
  507. events.push(e)
  508. })
  509. const obj = observable({ foo: 1 })
  510. const runner = autorun(
  511. () => {
  512. dummy = obj.foo
  513. },
  514. { onTrigger }
  515. )
  516. obj.foo++
  517. expect(dummy).toBe(2)
  518. expect(onTrigger).toHaveBeenCalledTimes(1)
  519. expect(events[0]).toEqual({
  520. runner,
  521. target: unwrap(obj),
  522. type: OperationTypes.SET,
  523. key: 'foo',
  524. oldValue: 1,
  525. newValue: 2
  526. })
  527. delete obj.foo
  528. expect(dummy).toBeUndefined()
  529. expect(onTrigger).toHaveBeenCalledTimes(2)
  530. expect(events[1]).toEqual({
  531. runner,
  532. target: unwrap(obj),
  533. type: OperationTypes.DELETE,
  534. key: 'foo',
  535. oldValue: 2
  536. })
  537. })
  538. it('stop', () => {
  539. let dummy
  540. const obj = observable({ prop: 1 })
  541. const runner = autorun(() => {
  542. dummy = obj.prop
  543. })
  544. obj.prop = 2
  545. expect(dummy).toBe(2)
  546. stop(runner)
  547. obj.prop = 3
  548. expect(dummy).toBe(2)
  549. // stopped runner should still be manually callable
  550. runner()
  551. expect(dummy).toBe(3)
  552. })
  553. it('markNonReactive', () => {
  554. const obj = observable({
  555. foo: markNonReactive({
  556. prop: 0
  557. })
  558. })
  559. let dummy
  560. autorun(() => {
  561. dummy = obj.foo.prop
  562. })
  563. expect(dummy).toBe(0)
  564. obj.foo.prop++
  565. expect(dummy).toBe(0)
  566. obj.foo = { prop: 1 }
  567. expect(dummy).toBe(1)
  568. })
  569. })