|
| 1 | +import { reactive, isReactive, toRaw } from '../src/reactive' |
| 2 | +import { ref, isRef } from '../src/ref' |
| 3 | +import { effect } from '../src/effect' |
| 4 | + |
| 5 | +describe('reactivity/reactive/Array', () => { |
| 6 | + test('should make Array reactive', () => { |
| 7 | + const original = [{ foo: 1 }] |
| 8 | + const observed = reactive(original) |
| 9 | + expect(observed).not.toBe(original) |
| 10 | + expect(isReactive(observed)).toBe(true) |
| 11 | + expect(isReactive(original)).toBe(false) |
| 12 | + expect(isReactive(observed[0])).toBe(true) |
| 13 | + // get |
| 14 | + expect(observed[0].foo).toBe(1) |
| 15 | + // has |
| 16 | + expect(0 in observed).toBe(true) |
| 17 | + // ownKeys |
| 18 | + expect(Object.keys(observed)).toEqual(['0']) |
| 19 | + }) |
| 20 | + |
| 21 | + test('cloned reactive Array should point to observed values', () => { |
| 22 | + const original = [{ foo: 1 }] |
| 23 | + const observed = reactive(original) |
| 24 | + const clone = observed.slice() |
| 25 | + expect(isReactive(clone[0])).toBe(true) |
| 26 | + expect(clone[0]).not.toBe(original[0]) |
| 27 | + expect(clone[0]).toBe(observed[0]) |
| 28 | + }) |
| 29 | + |
| 30 | + test('observed value should proxy mutations to original (Array)', () => { |
| 31 | + const original: any[] = [{ foo: 1 }, { bar: 2 }] |
| 32 | + const observed = reactive(original) |
| 33 | + // set |
| 34 | + const value = { baz: 3 } |
| 35 | + const reactiveValue = reactive(value) |
| 36 | + observed[0] = value |
| 37 | + expect(observed[0]).toBe(reactiveValue) |
| 38 | + expect(original[0]).toBe(value) |
| 39 | + // delete |
| 40 | + delete observed[0] |
| 41 | + expect(observed[0]).toBeUndefined() |
| 42 | + expect(original[0]).toBeUndefined() |
| 43 | + // mutating methods |
| 44 | + observed.push(value) |
| 45 | + expect(observed[2]).toBe(reactiveValue) |
| 46 | + expect(original[2]).toBe(value) |
| 47 | + }) |
| 48 | + |
| 49 | + test('Array identity methods should work with raw values', () => { |
| 50 | + const raw = {} |
| 51 | + const arr = reactive([{}, {}]) |
| 52 | + arr.push(raw) |
| 53 | + expect(arr.indexOf(raw)).toBe(2) |
| 54 | + expect(arr.indexOf(raw, 3)).toBe(-1) |
| 55 | + expect(arr.includes(raw)).toBe(true) |
| 56 | + expect(arr.includes(raw, 3)).toBe(false) |
| 57 | + expect(arr.lastIndexOf(raw)).toBe(2) |
| 58 | + expect(arr.lastIndexOf(raw, 1)).toBe(-1) |
| 59 | + |
| 60 | + // should work also for the observed version |
| 61 | + const observed = arr[2] |
| 62 | + expect(arr.indexOf(observed)).toBe(2) |
| 63 | + expect(arr.indexOf(observed, 3)).toBe(-1) |
| 64 | + expect(arr.includes(observed)).toBe(true) |
| 65 | + expect(arr.includes(observed, 3)).toBe(false) |
| 66 | + expect(arr.lastIndexOf(observed)).toBe(2) |
| 67 | + expect(arr.lastIndexOf(observed, 1)).toBe(-1) |
| 68 | + }) |
| 69 | + |
| 70 | + test('Array identity methods should be reactive', () => { |
| 71 | + const obj = {} |
| 72 | + const arr = reactive([obj, {}]) |
| 73 | + |
| 74 | + let index: number = -1 |
| 75 | + effect(() => { |
| 76 | + index = arr.indexOf(obj) |
| 77 | + }) |
| 78 | + expect(index).toBe(0) |
| 79 | + arr.reverse() |
| 80 | + expect(index).toBe(1) |
| 81 | + }) |
| 82 | + |
| 83 | + describe('Array methods w/ refs', () => { |
| 84 | + let original: any[] |
| 85 | + beforeEach(() => { |
| 86 | + original = reactive([1, ref(2)]) |
| 87 | + }) |
| 88 | + |
| 89 | + // read + copy |
| 90 | + test('read only copy methods', () => { |
| 91 | + const res = original.concat([3, ref(4)]) |
| 92 | + const raw = toRaw(res) |
| 93 | + expect(isRef(raw[1])).toBe(true) |
| 94 | + expect(isRef(raw[3])).toBe(true) |
| 95 | + }) |
| 96 | + |
| 97 | + // read + write |
| 98 | + test('read + write mutating methods', () => { |
| 99 | + const res = original.copyWithin(0, 1, 2) |
| 100 | + const raw = toRaw(res) |
| 101 | + expect(isRef(raw[0])).toBe(true) |
| 102 | + expect(isRef(raw[1])).toBe(true) |
| 103 | + }) |
| 104 | + |
| 105 | + test('read + indentity', () => { |
| 106 | + const ref = original[1] |
| 107 | + expect(ref).toBe(toRaw(original)[1]) |
| 108 | + expect(original.indexOf(ref)).toBe(1) |
| 109 | + }) |
| 110 | + }) |
| 111 | +}) |
0 commit comments