| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- import {
- reactive,
- immutable,
- toRaw,
- isReactive,
- isImmutable,
- markNonReactive,
- markImmutable,
- lock,
- unlock,
- effect,
- ref
- } from '../src'
- describe('reactivity/immutable', () => {
- let warn: any
- beforeEach(() => {
- warn = jest.spyOn(console, 'warn')
- warn.mockImplementation(() => {})
- })
- afterEach(() => {
- warn.mockRestore()
- })
- describe('Object', () => {
- it('should make nested values immutable', () => {
- const original = { foo: 1, bar: { baz: 2 } }
- const observed = immutable(original)
- expect(observed).not.toBe(original)
- expect(isReactive(observed)).toBe(true)
- expect(isImmutable(observed)).toBe(true)
- expect(isReactive(original)).toBe(false)
- expect(isImmutable(original)).toBe(false)
- expect(isReactive(observed.bar)).toBe(true)
- expect(isImmutable(observed.bar)).toBe(true)
- expect(isReactive(original.bar)).toBe(false)
- expect(isImmutable(original.bar)).toBe(false)
- // get
- expect(observed.foo).toBe(1)
- // has
- expect('foo' in observed).toBe(true)
- // ownKeys
- expect(Object.keys(observed)).toEqual(['foo', 'bar'])
- })
- it('should not allow mutation', () => {
- const observed: any = immutable({ foo: 1, bar: { baz: 2 } })
- observed.foo = 2
- expect(observed.foo).toBe(1)
- expect(warn).toHaveBeenCalledTimes(1)
- observed.bar.baz = 3
- expect(observed.bar.baz).toBe(2)
- expect(warn).toHaveBeenCalledTimes(2)
- delete observed.foo
- expect(observed.foo).toBe(1)
- expect(warn).toHaveBeenCalledTimes(3)
- delete observed.bar.baz
- expect(observed.bar.baz).toBe(2)
- expect(warn).toHaveBeenCalledTimes(4)
- })
- it('should allow mutation when unlocked', () => {
- const observed: any = immutable({ foo: 1, bar: { baz: 2 } })
- unlock()
- observed.prop = 2
- observed.bar.qux = 3
- delete observed.bar.baz
- delete observed.foo
- lock()
- expect(observed.prop).toBe(2)
- expect(observed.foo).toBeUndefined()
- expect(observed.bar.qux).toBe(3)
- expect('baz' in observed.bar).toBe(false)
- expect(warn).not.toHaveBeenCalled()
- })
- it('should not trigger effects when locked', () => {
- const observed: any = immutable({ a: 1 })
- let dummy
- effect(() => {
- dummy = observed.a
- })
- expect(dummy).toBe(1)
- observed.a = 2
- expect(observed.a).toBe(1)
- expect(dummy).toBe(1)
- })
- it('should trigger effects when unlocked', () => {
- const observed: any = immutable({ a: 1 })
- let dummy
- effect(() => {
- dummy = observed.a
- })
- expect(dummy).toBe(1)
- unlock()
- observed.a = 2
- lock()
- expect(observed.a).toBe(2)
- expect(dummy).toBe(2)
- })
- })
- describe('Array', () => {
- it('should make nested values immutable', () => {
- const original: any[] = [{ foo: 1 }]
- const observed = immutable(original)
- expect(observed).not.toBe(original)
- expect(isReactive(observed)).toBe(true)
- expect(isImmutable(observed)).toBe(true)
- expect(isReactive(original)).toBe(false)
- expect(isImmutable(original)).toBe(false)
- expect(isReactive(observed[0])).toBe(true)
- expect(isImmutable(observed[0])).toBe(true)
- expect(isReactive(original[0])).toBe(false)
- expect(isImmutable(original[0])).toBe(false)
- // get
- expect(observed[0].foo).toBe(1)
- // has
- expect(0 in observed).toBe(true)
- // ownKeys
- expect(Object.keys(observed)).toEqual(['0'])
- })
- it('should not allow mutation', () => {
- const observed: any = immutable([{ foo: 1 }])
- observed[0] = 1
- expect(observed[0]).not.toBe(1)
- expect(warn).toHaveBeenCalledTimes(1)
- observed[0].foo = 2
- expect(observed[0].foo).toBe(1)
- expect(warn).toHaveBeenCalledTimes(2)
- // should block length mutation
- observed.length = 0
- expect(observed.length).toBe(1)
- expect(observed[0].foo).toBe(1)
- expect(warn).toHaveBeenCalledTimes(3)
- // mutation methods invoke set/length internally and thus are blocked as well
- observed.push(2)
- expect(observed.length).toBe(1)
- // push triggers two warnings on [1] and .length
- expect(warn).toHaveBeenCalledTimes(5)
- })
- it('should allow mutation when unlocked', () => {
- const observed: any = immutable([{ foo: 1, bar: { baz: 2 } }])
- unlock()
- observed[1] = 2
- observed.push(3)
- observed[0].foo = 2
- observed[0].bar.baz = 3
- lock()
- expect(observed.length).toBe(3)
- expect(observed[1]).toBe(2)
- expect(observed[2]).toBe(3)
- expect(observed[0].foo).toBe(2)
- expect(observed[0].bar.baz).toBe(3)
- expect(warn).not.toHaveBeenCalled()
- })
- it('should not trigger effects when locked', () => {
- const observed: any = immutable([{ a: 1 }])
- let dummy
- effect(() => {
- dummy = observed[0].a
- })
- expect(dummy).toBe(1)
- observed[0].a = 2
- expect(observed[0].a).toBe(1)
- expect(dummy).toBe(1)
- observed[0] = { a: 2 }
- expect(observed[0].a).toBe(1)
- expect(dummy).toBe(1)
- })
- it('should trigger effects when unlocked', () => {
- const observed: any = immutable([{ a: 1 }])
- let dummy
- effect(() => {
- dummy = observed[0].a
- })
- expect(dummy).toBe(1)
- unlock()
- observed[0].a = 2
- expect(observed[0].a).toBe(2)
- expect(dummy).toBe(2)
- observed[0] = { a: 3 }
- expect(observed[0].a).toBe(3)
- expect(dummy).toBe(3)
- observed.unshift({ a: 4 })
- expect(observed[0].a).toBe(4)
- expect(dummy).toBe(4)
- lock()
- })
- })
- const maps = [Map, WeakMap]
- maps.forEach((Collection: any) => {
- describe(Collection.name, () => {
- test('should make nested values immutable', () => {
- const key1 = {}
- const key2 = {}
- const original = new Collection([[key1, {}], [key2, {}]])
- const observed = immutable(original)
- expect(observed).not.toBe(original)
- expect(isReactive(observed)).toBe(true)
- expect(isImmutable(observed)).toBe(true)
- expect(isReactive(original)).toBe(false)
- expect(isImmutable(original)).toBe(false)
- expect(isReactive(observed.get(key1))).toBe(true)
- expect(isImmutable(observed.get(key1))).toBe(true)
- expect(isReactive(original.get(key1))).toBe(false)
- expect(isImmutable(original.get(key1))).toBe(false)
- })
- test('should not allow mutation & not trigger effect', () => {
- const map = immutable(new Collection())
- const key = {}
- let dummy
- effect(() => {
- dummy = map.get(key)
- })
- expect(dummy).toBeUndefined()
- map.set(key, 1)
- expect(dummy).toBeUndefined()
- expect(map.has(key)).toBe(false)
- expect(warn).toHaveBeenCalledTimes(1)
- })
- test('should allow mutation & trigger effect when unlocked', () => {
- const map = immutable(new Collection())
- const isWeak = Collection === WeakMap
- const key = {}
- let dummy
- effect(() => {
- dummy = map.get(key) + (isWeak ? 0 : map.size)
- })
- expect(dummy).toBeNaN()
- unlock()
- map.set(key, 1)
- lock()
- expect(dummy).toBe(isWeak ? 1 : 2)
- expect(map.get(key)).toBe(1)
- expect(warn).not.toHaveBeenCalled()
- })
- if (Collection === Map) {
- test('should retrive immutable values on iteration', () => {
- const key1 = {}
- const key2 = {}
- const original = new Collection([[key1, {}], [key2, {}]])
- const observed: any = immutable(original)
- for (const [key, value] of observed) {
- expect(isImmutable(key)).toBe(true)
- expect(isImmutable(value)).toBe(true)
- }
- observed.forEach((value: any) => {
- expect(isImmutable(value)).toBe(true)
- })
- for (const value of observed.values()) {
- expect(isImmutable(value)).toBe(true)
- }
- })
- }
- })
- })
- const sets = [Set, WeakSet]
- sets.forEach((Collection: any) => {
- describe(Collection.name, () => {
- test('should make nested values immutable', () => {
- const key1 = {}
- const key2 = {}
- const original = new Collection([key1, key2])
- const observed = immutable(original)
- expect(observed).not.toBe(original)
- expect(isReactive(observed)).toBe(true)
- expect(isImmutable(observed)).toBe(true)
- expect(isReactive(original)).toBe(false)
- expect(isImmutable(original)).toBe(false)
- expect(observed.has(reactive(key1))).toBe(true)
- expect(original.has(reactive(key1))).toBe(false)
- })
- test('should not allow mutation & not trigger effect', () => {
- const set = immutable(new Collection())
- const key = {}
- let dummy
- effect(() => {
- dummy = set.has(key)
- })
- expect(dummy).toBe(false)
- set.add(key)
- expect(dummy).toBe(false)
- expect(set.has(key)).toBe(false)
- expect(warn).toHaveBeenCalledTimes(1)
- })
- test('should allow mutation & trigger effect when unlocked', () => {
- const set = immutable(new Collection())
- const key = {}
- let dummy
- effect(() => {
- dummy = set.has(key)
- })
- expect(dummy).toBe(false)
- unlock()
- set.add(key)
- lock()
- expect(dummy).toBe(true)
- expect(set.has(key)).toBe(true)
- expect(warn).not.toHaveBeenCalled()
- })
- if (Collection === Set) {
- test('should retrive immutable values on iteration', () => {
- const original = new Collection([{}, {}])
- const observed: any = immutable(original)
- for (const value of observed) {
- expect(isImmutable(value)).toBe(true)
- }
- observed.forEach((value: any) => {
- expect(isImmutable(value)).toBe(true)
- })
- for (const value of observed.values()) {
- expect(isImmutable(value)).toBe(true)
- }
- for (const [v1, v2] of observed.entries()) {
- expect(isImmutable(v1)).toBe(true)
- expect(isImmutable(v2)).toBe(true)
- }
- })
- }
- })
- })
- test('calling reactive on an immutable should return immutable', () => {
- const a = immutable({})
- const b = reactive(a)
- expect(isImmutable(b)).toBe(true)
- // should point to same original
- expect(toRaw(a)).toBe(toRaw(b))
- })
- test('calling immutable on a reactive object should return immutable', () => {
- const a = reactive({})
- const b = immutable(a)
- expect(isImmutable(b)).toBe(true)
- // should point to same original
- expect(toRaw(a)).toBe(toRaw(b))
- })
- test('observing already observed value should return same Proxy', () => {
- const original = { foo: 1 }
- const observed = immutable(original)
- const observed2 = immutable(observed)
- expect(observed2).toBe(observed)
- })
- test('observing the same value multiple times should return same Proxy', () => {
- const original = { foo: 1 }
- const observed = immutable(original)
- const observed2 = immutable(original)
- expect(observed2).toBe(observed)
- })
- test('markNonReactive', () => {
- const obj = immutable({
- foo: { a: 1 },
- bar: markNonReactive({ b: 2 })
- })
- expect(isReactive(obj.foo)).toBe(true)
- expect(isReactive(obj.bar)).toBe(false)
- })
- test('markImmutable', () => {
- const obj = reactive({
- foo: { a: 1 },
- bar: markImmutable({ b: 2 })
- })
- expect(isReactive(obj.foo)).toBe(true)
- expect(isReactive(obj.bar)).toBe(true)
- expect(isImmutable(obj.foo)).toBe(false)
- expect(isImmutable(obj.bar)).toBe(true)
- })
- test('should make ref immutable', () => {
- const n: any = immutable(ref(1))
- n.value = 2
- expect(n.value).toBe(1)
- expect(warn).toHaveBeenCalledTimes(1)
- })
- })
|