immutable.spec.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. })
  51. it('should allow mutation when unlocked', () => {
  52. const observed = immutable({ foo: 1, bar: { baz: 2 } })
  53. unlock()
  54. observed.foo = 2
  55. observed.bar.baz = 3
  56. lock()
  57. expect(observed.foo).toBe(2)
  58. expect(observed.bar.baz).toBe(3)
  59. expect(warn).not.toHaveBeenCalled()
  60. })
  61. it('should not trigger autoruns when locked', () => {
  62. const observed = immutable({ a: 1 })
  63. let dummy
  64. autorun(() => {
  65. dummy = observed.a
  66. })
  67. expect(dummy).toBe(1)
  68. observed.a = 2
  69. expect(observed.a).toBe(1)
  70. expect(dummy).toBe(1)
  71. })
  72. it('should trigger autoruns when unlocked', () => {
  73. const observed = immutable({ a: 1 })
  74. let dummy
  75. autorun(() => {
  76. dummy = observed.a
  77. })
  78. expect(dummy).toBe(1)
  79. unlock()
  80. observed.a = 2
  81. lock()
  82. expect(observed.a).toBe(2)
  83. expect(dummy).toBe(2)
  84. })
  85. })
  86. describe('Array', () => {
  87. it('should make nested values immutable', () => {
  88. const original: any[] = [{ foo: 1 }]
  89. const observed = immutable(original)
  90. expect(observed).not.toBe(original)
  91. expect(isObservable(observed)).toBe(true)
  92. expect(isImmutable(observed)).toBe(true)
  93. expect(isObservable(original)).toBe(false)
  94. expect(isImmutable(original)).toBe(false)
  95. expect(isObservable(observed[0])).toBe(true)
  96. expect(isImmutable(observed[0])).toBe(true)
  97. expect(isObservable(original[0])).toBe(false)
  98. expect(isImmutable(original[0])).toBe(false)
  99. // get
  100. expect(observed[0].foo).toBe(1)
  101. // has
  102. expect(0 in observed).toBe(true)
  103. // ownKeys
  104. expect(Object.keys(observed)).toEqual(['0'])
  105. })
  106. it('should not allow mutation', () => {
  107. const observed: any = immutable([{ foo: 1 }])
  108. observed[0] = 1
  109. expect(observed[0]).not.toBe(1)
  110. expect(warn).toHaveBeenCalledTimes(1)
  111. observed[0].foo = 2
  112. expect(observed[0].foo).toBe(1)
  113. expect(warn).toHaveBeenCalledTimes(2)
  114. // should block length mutation
  115. observed.length = 0
  116. expect(observed.length).toBe(1)
  117. expect(observed[0].foo).toBe(1)
  118. expect(warn).toHaveBeenCalledTimes(3)
  119. // mutation methods invoke set/length internally and thus are blocked as well
  120. observed.push(2)
  121. expect(observed.length).toBe(1)
  122. // push triggers two warnings on [1] and .length
  123. expect(warn).toHaveBeenCalledTimes(5)
  124. })
  125. it('should allow mutation when unlocked', () => {
  126. const observed: any[] = immutable([{ foo: 1, bar: { baz: 2 } }])
  127. unlock()
  128. observed[1] = 2
  129. observed.push(3)
  130. observed[0].foo = 2
  131. observed[0].bar.baz = 3
  132. lock()
  133. expect(observed.length).toBe(3)
  134. expect(observed[1]).toBe(2)
  135. expect(observed[2]).toBe(3)
  136. expect(observed[0].foo).toBe(2)
  137. expect(observed[0].bar.baz).toBe(3)
  138. expect(warn).not.toHaveBeenCalled()
  139. })
  140. it('should not trigger autoruns when locked', () => {
  141. const observed = immutable([{ a: 1 }])
  142. let dummy
  143. autorun(() => {
  144. dummy = observed[0].a
  145. })
  146. expect(dummy).toBe(1)
  147. observed[0].a = 2
  148. expect(observed[0].a).toBe(1)
  149. expect(dummy).toBe(1)
  150. observed[0] = { a: 2 }
  151. expect(observed[0].a).toBe(1)
  152. expect(dummy).toBe(1)
  153. })
  154. it('should trigger autoruns when unlocked', () => {
  155. const observed = immutable([{ a: 1 }])
  156. let dummy
  157. autorun(() => {
  158. dummy = observed[0].a
  159. })
  160. expect(dummy).toBe(1)
  161. unlock()
  162. observed[0].a = 2
  163. expect(observed[0].a).toBe(2)
  164. expect(dummy).toBe(2)
  165. observed[0] = { a: 3 }
  166. expect(observed[0].a).toBe(3)
  167. expect(dummy).toBe(3)
  168. observed.unshift({ a: 4 })
  169. expect(observed[0].a).toBe(4)
  170. expect(dummy).toBe(4)
  171. lock()
  172. })
  173. })
  174. ;[Map, WeakMap].forEach((Collection: any) => {
  175. describe(Collection.name, () => {
  176. test('should make nested values immutable', () => {
  177. const key1 = {}
  178. const key2 = {}
  179. const original = new Collection([[key1, {}], [key2, {}]])
  180. const observed = immutable(original)
  181. expect(observed).not.toBe(original)
  182. expect(isObservable(observed)).toBe(true)
  183. expect(isImmutable(observed)).toBe(true)
  184. expect(isObservable(original)).toBe(false)
  185. expect(isImmutable(original)).toBe(false)
  186. expect(isObservable(observed.get(key1))).toBe(true)
  187. expect(isImmutable(observed.get(key1))).toBe(true)
  188. expect(isObservable(original.get(key1))).toBe(false)
  189. expect(isImmutable(original.get(key1))).toBe(false)
  190. })
  191. test('should not allow mutation & not trigger autorun', () => {
  192. const map = immutable(new Collection())
  193. const key = {}
  194. let dummy
  195. autorun(() => {
  196. dummy = map.get(key)
  197. })
  198. expect(dummy).toBeUndefined()
  199. map.set(key, 1)
  200. expect(dummy).toBeUndefined()
  201. expect(map.has(key)).toBe(false)
  202. expect(warn).toHaveBeenCalledTimes(1)
  203. })
  204. test('should allow mutation & trigger autorun when unlocked', () => {
  205. const map = immutable(new Collection())
  206. const key = {}
  207. let dummy
  208. autorun(() => {
  209. dummy = map.get(key)
  210. })
  211. expect(dummy).toBeUndefined()
  212. unlock()
  213. map.set(key, 1)
  214. lock()
  215. expect(dummy).toBe(1)
  216. expect(map.get(key)).toBe(1)
  217. expect(warn).not.toHaveBeenCalled()
  218. })
  219. })
  220. })
  221. ;[Set, WeakSet].forEach((Collection: any) => {
  222. describe(Collection.name, () => {
  223. test('should make nested values immutable', () => {
  224. const key1 = {}
  225. const key2 = {}
  226. const original = new Collection([key1, key2])
  227. const observed = immutable(original)
  228. expect(observed).not.toBe(original)
  229. expect(isObservable(observed)).toBe(true)
  230. expect(isImmutable(observed)).toBe(true)
  231. expect(isObservable(original)).toBe(false)
  232. expect(isImmutable(original)).toBe(false)
  233. expect(observed.has(observable(key1))).toBe(true)
  234. expect(original.has(observable(key1))).toBe(false)
  235. })
  236. test('should not allow mutation & not trigger autorun', () => {
  237. const set = immutable(new Collection())
  238. const key = {}
  239. let dummy
  240. autorun(() => {
  241. dummy = set.has(key)
  242. })
  243. expect(dummy).toBe(false)
  244. set.add(key)
  245. expect(dummy).toBe(false)
  246. expect(set.has(key)).toBe(false)
  247. expect(warn).toHaveBeenCalledTimes(1)
  248. })
  249. test('should allow mutation & trigger autorun when unlocked', () => {
  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. unlock()
  258. set.add(key)
  259. lock()
  260. expect(dummy).toBe(true)
  261. expect(set.has(key)).toBe(true)
  262. expect(warn).not.toHaveBeenCalled()
  263. })
  264. })
  265. })
  266. test('calling observable on an immutable should return immutable', () => {
  267. const a = immutable()
  268. const b = observable(a)
  269. expect(isImmutable(b)).toBe(true)
  270. // should point to same original
  271. expect(unwrap(a)).toBe(unwrap(b))
  272. })
  273. test('calling immutable on an observable should return immutable', () => {
  274. const a = observable()
  275. const b = immutable(a)
  276. expect(isImmutable(b)).toBe(true)
  277. // should point to same original
  278. expect(unwrap(a)).toBe(unwrap(b))
  279. })
  280. test('observing already observed value should return same Proxy', () => {
  281. const original = { foo: 1 }
  282. const observed = immutable(original)
  283. const observed2 = immutable(observed)
  284. expect(observed2).toBe(observed)
  285. })
  286. test('observing the same value multiple times should return same Proxy', () => {
  287. const original = { foo: 1 }
  288. const observed = immutable(original)
  289. const observed2 = immutable(original)
  290. expect(observed2).toBe(observed)
  291. })
  292. test('markNonReactive', () => {
  293. const obj = immutable({
  294. foo: { a: 1 },
  295. bar: markNonReactive({ b: 2 })
  296. })
  297. expect(isObservable(obj.foo)).toBe(true)
  298. expect(isObservable(obj.bar)).toBe(false)
  299. })
  300. test('markImmutable', () => {
  301. const obj = observable({
  302. foo: { a: 1 },
  303. bar: markImmutable({ b: 2 })
  304. })
  305. expect(isObservable(obj.foo)).toBe(true)
  306. expect(isObservable(obj.bar)).toBe(true)
  307. expect(isImmutable(obj.foo)).toBe(false)
  308. expect(isImmutable(obj.bar)).toBe(true)
  309. })
  310. })