immutable.spec.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. import {
  2. observable,
  3. immutable,
  4. unwrap,
  5. isObservable,
  6. isImmutable,
  7. markNonReactive,
  8. markImmutable,
  9. lock,
  10. unlock,
  11. autorun
  12. } from '../src'
  13. describe('observer/immutable', () => {
  14. let warn: any
  15. beforeEach(() => {
  16. warn = jest.spyOn(console, 'warn')
  17. warn.mockImplementation(() => {})
  18. })
  19. afterEach(() => {
  20. warn.mockRestore()
  21. })
  22. describe('Object', () => {
  23. it('should make nested values immutable', () => {
  24. const original = { foo: 1, bar: { baz: 2 } }
  25. const observed = immutable(original)
  26. expect(observed).not.toBe(original)
  27. expect(isObservable(observed)).toBe(true)
  28. expect(isImmutable(observed)).toBe(true)
  29. expect(isObservable(original)).toBe(false)
  30. expect(isImmutable(original)).toBe(false)
  31. expect(isObservable(observed.bar)).toBe(true)
  32. expect(isImmutable(observed.bar)).toBe(true)
  33. expect(isObservable(original.bar)).toBe(false)
  34. expect(isImmutable(original.bar)).toBe(false)
  35. // get
  36. expect(observed.foo).toBe(1)
  37. // has
  38. expect('foo' in observed).toBe(true)
  39. // ownKeys
  40. expect(Object.keys(observed)).toEqual(['foo', 'bar'])
  41. })
  42. it('should not allow mutation', () => {
  43. const observed = immutable({ foo: 1, bar: { baz: 2 } })
  44. observed.foo = 2
  45. expect(observed.foo).toBe(1)
  46. expect(warn).toHaveBeenCalledTimes(1)
  47. observed.bar.baz = 3
  48. expect(observed.bar.baz).toBe(2)
  49. expect(warn).toHaveBeenCalledTimes(2)
  50. delete observed.foo
  51. expect(observed.foo).toBe(1)
  52. expect(warn).toHaveBeenCalledTimes(3)
  53. delete observed.bar.baz
  54. expect(observed.bar.baz).toBe(2)
  55. expect(warn).toHaveBeenCalledTimes(4)
  56. })
  57. it('should allow mutation when unlocked', () => {
  58. const observed: any = immutable({ foo: 1, bar: { baz: 2 } })
  59. unlock()
  60. observed.prop = 2
  61. observed.bar.qux = 3
  62. delete observed.bar.baz
  63. delete observed.foo
  64. lock()
  65. expect(observed.prop).toBe(2)
  66. expect(observed.foo).toBeUndefined()
  67. expect(observed.bar.qux).toBe(3)
  68. expect('baz' in observed.bar).toBe(false)
  69. expect(warn).not.toHaveBeenCalled()
  70. })
  71. it('should not trigger autoruns when locked', () => {
  72. const observed = immutable({ a: 1 })
  73. let dummy
  74. autorun(() => {
  75. dummy = observed.a
  76. })
  77. expect(dummy).toBe(1)
  78. observed.a = 2
  79. expect(observed.a).toBe(1)
  80. expect(dummy).toBe(1)
  81. })
  82. it('should trigger autoruns when unlocked', () => {
  83. const observed = immutable({ a: 1 })
  84. let dummy
  85. autorun(() => {
  86. dummy = observed.a
  87. })
  88. expect(dummy).toBe(1)
  89. unlock()
  90. observed.a = 2
  91. lock()
  92. expect(observed.a).toBe(2)
  93. expect(dummy).toBe(2)
  94. })
  95. })
  96. describe('Array', () => {
  97. it('should make nested values immutable', () => {
  98. const original: any[] = [{ foo: 1 }]
  99. const observed = immutable(original)
  100. expect(observed).not.toBe(original)
  101. expect(isObservable(observed)).toBe(true)
  102. expect(isImmutable(observed)).toBe(true)
  103. expect(isObservable(original)).toBe(false)
  104. expect(isImmutable(original)).toBe(false)
  105. expect(isObservable(observed[0])).toBe(true)
  106. expect(isImmutable(observed[0])).toBe(true)
  107. expect(isObservable(original[0])).toBe(false)
  108. expect(isImmutable(original[0])).toBe(false)
  109. // get
  110. expect(observed[0].foo).toBe(1)
  111. // has
  112. expect(0 in observed).toBe(true)
  113. // ownKeys
  114. expect(Object.keys(observed)).toEqual(['0'])
  115. })
  116. it('should not allow mutation', () => {
  117. const observed: any = immutable([{ foo: 1 }])
  118. observed[0] = 1
  119. expect(observed[0]).not.toBe(1)
  120. expect(warn).toHaveBeenCalledTimes(1)
  121. observed[0].foo = 2
  122. expect(observed[0].foo).toBe(1)
  123. expect(warn).toHaveBeenCalledTimes(2)
  124. // should block length mutation
  125. observed.length = 0
  126. expect(observed.length).toBe(1)
  127. expect(observed[0].foo).toBe(1)
  128. expect(warn).toHaveBeenCalledTimes(3)
  129. // mutation methods invoke set/length internally and thus are blocked as well
  130. observed.push(2)
  131. expect(observed.length).toBe(1)
  132. // push triggers two warnings on [1] and .length
  133. expect(warn).toHaveBeenCalledTimes(5)
  134. })
  135. it('should allow mutation when unlocked', () => {
  136. const observed: any[] = immutable([{ foo: 1, bar: { baz: 2 } }])
  137. unlock()
  138. observed[1] = 2
  139. observed.push(3)
  140. observed[0].foo = 2
  141. observed[0].bar.baz = 3
  142. lock()
  143. expect(observed.length).toBe(3)
  144. expect(observed[1]).toBe(2)
  145. expect(observed[2]).toBe(3)
  146. expect(observed[0].foo).toBe(2)
  147. expect(observed[0].bar.baz).toBe(3)
  148. expect(warn).not.toHaveBeenCalled()
  149. })
  150. it('should not trigger autoruns when locked', () => {
  151. const observed = immutable([{ a: 1 }])
  152. let dummy
  153. autorun(() => {
  154. dummy = observed[0].a
  155. })
  156. expect(dummy).toBe(1)
  157. observed[0].a = 2
  158. expect(observed[0].a).toBe(1)
  159. expect(dummy).toBe(1)
  160. observed[0] = { a: 2 }
  161. expect(observed[0].a).toBe(1)
  162. expect(dummy).toBe(1)
  163. })
  164. it('should trigger autoruns when unlocked', () => {
  165. const observed = immutable([{ a: 1 }])
  166. let dummy
  167. autorun(() => {
  168. dummy = observed[0].a
  169. })
  170. expect(dummy).toBe(1)
  171. unlock()
  172. observed[0].a = 2
  173. expect(observed[0].a).toBe(2)
  174. expect(dummy).toBe(2)
  175. observed[0] = { a: 3 }
  176. expect(observed[0].a).toBe(3)
  177. expect(dummy).toBe(3)
  178. observed.unshift({ a: 4 })
  179. expect(observed[0].a).toBe(4)
  180. expect(dummy).toBe(4)
  181. lock()
  182. })
  183. })
  184. const maps = [Map, WeakMap]
  185. maps.forEach((Collection: any) => {
  186. describe(Collection.name, () => {
  187. test('should make nested values immutable', () => {
  188. const key1 = {}
  189. const key2 = {}
  190. const original = new Collection([[key1, {}], [key2, {}]])
  191. const observed = immutable(original)
  192. expect(observed).not.toBe(original)
  193. expect(isObservable(observed)).toBe(true)
  194. expect(isImmutable(observed)).toBe(true)
  195. expect(isObservable(original)).toBe(false)
  196. expect(isImmutable(original)).toBe(false)
  197. expect(isObservable(observed.get(key1))).toBe(true)
  198. expect(isImmutable(observed.get(key1))).toBe(true)
  199. expect(isObservable(original.get(key1))).toBe(false)
  200. expect(isImmutable(original.get(key1))).toBe(false)
  201. })
  202. test('should not allow mutation & not trigger autorun', () => {
  203. const map = immutable(new Collection())
  204. const key = {}
  205. let dummy
  206. autorun(() => {
  207. dummy = map.get(key)
  208. })
  209. expect(dummy).toBeUndefined()
  210. map.set(key, 1)
  211. expect(dummy).toBeUndefined()
  212. expect(map.has(key)).toBe(false)
  213. expect(warn).toHaveBeenCalledTimes(1)
  214. })
  215. test('should allow mutation & trigger autorun when unlocked', () => {
  216. const map = immutable(new Collection())
  217. const isWeak = Collection === WeakMap
  218. const key = {}
  219. let dummy
  220. autorun(() => {
  221. dummy = map.get(key) + (isWeak ? 0 : map.size)
  222. })
  223. expect(dummy).toBeNaN()
  224. unlock()
  225. map.set(key, 1)
  226. lock()
  227. expect(dummy).toBe(isWeak ? 1 : 2)
  228. expect(map.get(key)).toBe(1)
  229. expect(warn).not.toHaveBeenCalled()
  230. })
  231. })
  232. })
  233. const sets = [Set, WeakSet]
  234. sets.forEach((Collection: any) => {
  235. describe(Collection.name, () => {
  236. test('should make nested values immutable', () => {
  237. const key1 = {}
  238. const key2 = {}
  239. const original = new Collection([key1, key2])
  240. const observed = immutable(original)
  241. expect(observed).not.toBe(original)
  242. expect(isObservable(observed)).toBe(true)
  243. expect(isImmutable(observed)).toBe(true)
  244. expect(isObservable(original)).toBe(false)
  245. expect(isImmutable(original)).toBe(false)
  246. expect(observed.has(observable(key1))).toBe(true)
  247. expect(original.has(observable(key1))).toBe(false)
  248. })
  249. test('should not allow mutation & not trigger autorun', () => {
  250. const set = immutable(new Collection())
  251. const key = {}
  252. let dummy
  253. autorun(() => {
  254. dummy = set.has(key)
  255. })
  256. expect(dummy).toBe(false)
  257. set.add(key)
  258. expect(dummy).toBe(false)
  259. expect(set.has(key)).toBe(false)
  260. expect(warn).toHaveBeenCalledTimes(1)
  261. })
  262. test('should allow mutation & trigger autorun when unlocked', () => {
  263. const set = immutable(new Collection())
  264. const key = {}
  265. let dummy
  266. autorun(() => {
  267. dummy = set.has(key)
  268. })
  269. expect(dummy).toBe(false)
  270. unlock()
  271. set.add(key)
  272. lock()
  273. expect(dummy).toBe(true)
  274. expect(set.has(key)).toBe(true)
  275. expect(warn).not.toHaveBeenCalled()
  276. })
  277. })
  278. })
  279. test('calling observable on an immutable should return immutable', () => {
  280. const a = immutable()
  281. const b = observable(a)
  282. expect(isImmutable(b)).toBe(true)
  283. // should point to same original
  284. expect(unwrap(a)).toBe(unwrap(b))
  285. })
  286. test('calling immutable on an observable should return immutable', () => {
  287. const a = observable()
  288. const b = immutable(a)
  289. expect(isImmutable(b)).toBe(true)
  290. // should point to same original
  291. expect(unwrap(a)).toBe(unwrap(b))
  292. })
  293. test('observing already observed value should return same Proxy', () => {
  294. const original = { foo: 1 }
  295. const observed = immutable(original)
  296. const observed2 = immutable(observed)
  297. expect(observed2).toBe(observed)
  298. })
  299. test('observing the same value multiple times should return same Proxy', () => {
  300. const original = { foo: 1 }
  301. const observed = immutable(original)
  302. const observed2 = immutable(original)
  303. expect(observed2).toBe(observed)
  304. })
  305. test('markNonReactive', () => {
  306. const obj = immutable({
  307. foo: { a: 1 },
  308. bar: markNonReactive({ b: 2 })
  309. })
  310. expect(isObservable(obj.foo)).toBe(true)
  311. expect(isObservable(obj.bar)).toBe(false)
  312. })
  313. test('markImmutable', () => {
  314. const obj = observable({
  315. foo: { a: 1 },
  316. bar: markImmutable({ b: 2 })
  317. })
  318. expect(isObservable(obj.foo)).toBe(true)
  319. expect(isObservable(obj.bar)).toBe(true)
  320. expect(isImmutable(obj.foo)).toBe(false)
  321. expect(isImmutable(obj.bar)).toBe(true)
  322. })
  323. })