|
| 1 | +import { shallowReactive, isReactive, reactive } from '../src/reactive' |
| 2 | +import { effect } from '../src/effect' |
| 3 | + |
| 4 | +describe('shallowReactive', () => { |
| 5 | + test('should not make non-reactive properties reactive', () => { |
| 6 | + const props = shallowReactive({ n: { foo: 1 } }) |
| 7 | + expect(isReactive(props.n)).toBe(false) |
| 8 | + }) |
| 9 | + |
| 10 | + test('should keep reactive properties reactive', () => { |
| 11 | + const props: any = shallowReactive({ n: reactive({ foo: 1 }) }) |
| 12 | + props.n = reactive({ foo: 2 }) |
| 13 | + expect(isReactive(props.n)).toBe(true) |
| 14 | + }) |
| 15 | + |
| 16 | + describe('collections', () => { |
| 17 | + test('should be reactive', () => { |
| 18 | + const shallowSet = shallowReactive(new Set()) |
| 19 | + const a = {} |
| 20 | + let size |
| 21 | + |
| 22 | + effect(() => { |
| 23 | + size = shallowSet.size |
| 24 | + }) |
| 25 | + |
| 26 | + expect(size).toBe(0) |
| 27 | + |
| 28 | + shallowSet.add(a) |
| 29 | + expect(size).toBe(1) |
| 30 | + |
| 31 | + shallowSet.delete(a) |
| 32 | + expect(size).toBe(0) |
| 33 | + }) |
| 34 | + |
| 35 | + test('should not observe when iterating', () => { |
| 36 | + const shallowSet = shallowReactive(new Set()) |
| 37 | + const a = {} |
| 38 | + shallowSet.add(a) |
| 39 | + |
| 40 | + const spreadA = [...shallowSet][0] |
| 41 | + expect(isReactive(spreadA)).toBe(false) |
| 42 | + }) |
| 43 | + |
| 44 | + test('should not get reactive entry', () => { |
| 45 | + const shallowMap = shallowReactive(new Map()) |
| 46 | + const a = {} |
| 47 | + const key = 'a' |
| 48 | + |
| 49 | + shallowMap.set(key, a) |
| 50 | + |
| 51 | + expect(isReactive(shallowMap.get(key))).toBe(false) |
| 52 | + }) |
| 53 | + |
| 54 | + test('should not get reactive on foreach', () => { |
| 55 | + const shallowSet = shallowReactive(new Set()) |
| 56 | + const a = {} |
| 57 | + shallowSet.add(a) |
| 58 | + |
| 59 | + shallowSet.forEach(x => expect(isReactive(x)).toBe(false)) |
| 60 | + }) |
| 61 | + |
| 62 | + // #1210 |
| 63 | + test('onTrack on called on objectSpread', () => { |
| 64 | + const onTrackFn = jest.fn() |
| 65 | + const shallowSet = shallowReactive(new Set()) |
| 66 | + let a |
| 67 | + effect( |
| 68 | + () => { |
| 69 | + a = Array.from(shallowSet) |
| 70 | + }, |
| 71 | + { |
| 72 | + onTrack: onTrackFn |
| 73 | + } |
| 74 | + ) |
| 75 | + |
| 76 | + expect(a).toMatchObject([]) |
| 77 | + expect(onTrackFn).toHaveBeenCalled() |
| 78 | + }) |
| 79 | + }) |
| 80 | + |
| 81 | + describe('array', () => { |
| 82 | + test('should be reactive', () => { |
| 83 | + const shallowArray = shallowReactive<unknown[]>([]) |
| 84 | + const a = {} |
| 85 | + let size |
| 86 | + |
| 87 | + effect(() => { |
| 88 | + size = shallowArray.length |
| 89 | + }) |
| 90 | + |
| 91 | + expect(size).toBe(0) |
| 92 | + |
| 93 | + shallowArray.push(a) |
| 94 | + expect(size).toBe(1) |
| 95 | + |
| 96 | + shallowArray.pop() |
| 97 | + expect(size).toBe(0) |
| 98 | + }) |
| 99 | + test('should not observe when iterating', () => { |
| 100 | + const shallowArray = shallowReactive<object[]>([]) |
| 101 | + const a = {} |
| 102 | + shallowArray.push(a) |
| 103 | + |
| 104 | + const spreadA = [...shallowArray][0] |
| 105 | + expect(isReactive(spreadA)).toBe(false) |
| 106 | + }) |
| 107 | + |
| 108 | + test('onTrack on called on objectSpread', () => { |
| 109 | + const onTrackFn = jest.fn() |
| 110 | + const shallowArray = shallowReactive([]) |
| 111 | + let a |
| 112 | + effect( |
| 113 | + () => { |
| 114 | + a = Array.from(shallowArray) |
| 115 | + }, |
| 116 | + { |
| 117 | + onTrack: onTrackFn |
| 118 | + } |
| 119 | + ) |
| 120 | + |
| 121 | + expect(a).toMatchObject([]) |
| 122 | + expect(onTrackFn).toHaveBeenCalled() |
| 123 | + }) |
| 124 | + }) |
| 125 | +}) |
0 commit comments