|
| 1 | +import { createMakeProxyFunction, getChildObject, UNPROXIED_OBJ_KEY } from "../src/trackObjectUse" |
| 2 | +import { isObjectOrArray } from "../src/utility" |
| 3 | +describe("getChildObject", () => { |
| 4 | + const testObj = { |
| 5 | + a: 1, |
| 6 | + b: { |
| 7 | + c: { |
| 8 | + d: [ |
| 9 | + 1, |
| 10 | + 2, |
| 11 | + { |
| 12 | + e: { f: "g" } |
| 13 | + } |
| 14 | + ] |
| 15 | + } |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + it("returns the part of the object indicated by the provided string of keys", () => { |
| 20 | + expect(getChildObject(testObj, "b.c.d.2.e")).toEqual({ f: "g" }) |
| 21 | + }) |
| 22 | + it("returns the object if the key list is empty", () => { |
| 23 | + expect(getChildObject(testObj, "")).toEqual(testObj) |
| 24 | + }) |
| 25 | +}) |
| 26 | + |
| 27 | +describe("createMakeProxyFunction", () => { |
| 28 | + const isProxy = obj => isObjectOrArray(obj[UNPROXIED_OBJ_KEY]) |
| 29 | + const isDoubleProxied = obj => |
| 30 | + isObjectOrArray(obj[UNPROXIED_OBJ_KEY]) && |
| 31 | + isObjectOrArray(obj[UNPROXIED_OBJ_KEY][UNPROXIED_OBJ_KEY]) |
| 32 | + |
| 33 | + it("returns a function that creates a proxy to track object use", () => { |
| 34 | + const accessedProperties = {} |
| 35 | + const makeProxy = createMakeProxyFunction({ accessedProperties }) |
| 36 | + const object = { a: { b: "c" }, d: [1, 2, 3, 4, 5] } |
| 37 | + const proxy = makeProxy(object) |
| 38 | + |
| 39 | + const test1 = proxy.a.b |
| 40 | + const test2 = proxy.d[2] |
| 41 | + expect(accessedProperties).toEqual({ |
| 42 | + a: { b: "c" }, |
| 43 | + d: [undefined, undefined, 3] |
| 44 | + }) |
| 45 | + }) |
| 46 | + |
| 47 | + it("will create proxies of child objects when they are accessed", () => { |
| 48 | + const accessedProperties = {} |
| 49 | + const makeProxy = createMakeProxyFunction({ accessedProperties }) |
| 50 | + const object = { a: { b: "c" }, d: [1, 2, 3, 4, 5] } |
| 51 | + const proxy = makeProxy(object) |
| 52 | + |
| 53 | + const test1 = proxy.a.b |
| 54 | + |
| 55 | + expect(isProxy(proxy.a)).toBe(true) |
| 56 | + // the original child is preserved as a non-proxy |
| 57 | + expect(isProxy(object.a)).toBe(false) |
| 58 | + }) |
| 59 | + |
| 60 | + it("does not try to proxy non-object or non-array values", () => { |
| 61 | + const accessedProperties = {} |
| 62 | + const makeProxy = createMakeProxyFunction({ accessedProperties }) |
| 63 | + const object = { a: { b: "c" }, d: [1, 2, 3, 4, 5] } |
| 64 | + const proxy = makeProxy(object) |
| 65 | + |
| 66 | + const test1 = proxy.a.b |
| 67 | + |
| 68 | + expect(isProxy(proxy.a.b)).toBe(false) |
| 69 | + }) |
| 70 | + |
| 71 | + it("updates values in accessed properties if they change and are accessed again", () => { |
| 72 | + const accessedProperties = {} |
| 73 | + const makeProxy = createMakeProxyFunction({ accessedProperties }) |
| 74 | + const object = { a: { b: "c" }, d: [1, 2, 3, 4, 5] } |
| 75 | + const proxy = makeProxy(object) |
| 76 | + |
| 77 | + const test1 = proxy.a.b |
| 78 | + expect(accessedProperties).toEqual({ |
| 79 | + a: { b: "c" } |
| 80 | + }) |
| 81 | + |
| 82 | + object.a.b = "e" |
| 83 | + const test2 = proxy.a.b |
| 84 | + expect(accessedProperties).toEqual({ |
| 85 | + a: { b: "e" } |
| 86 | + }) |
| 87 | + }) |
| 88 | + |
| 89 | + it("will not augment the accessedProperties object if shouldSkipProxy argument returns true", () => { |
| 90 | + const accessedProperties = {} |
| 91 | + const makeProxy = createMakeProxyFunction({ |
| 92 | + accessedProperties, |
| 93 | + shouldSkipProxy: () => true |
| 94 | + }) |
| 95 | + const object = { a: { b: "c" }, d: [1, 2, 3, 4, 5] } |
| 96 | + const proxy = makeProxy(object) |
| 97 | + |
| 98 | + const test1 = proxy.a.b |
| 99 | + expect(accessedProperties).toEqual({}) |
| 100 | + }) |
| 101 | + |
| 102 | + it("just returns the value if the key is not on the objects prototype", () => { |
| 103 | + const accessedProperties = {} |
| 104 | + const makeProxy = createMakeProxyFunction({ accessedProperties }) |
| 105 | + const object = { a: { b: "c" }, d: [1, 2, 3, 4, 5] } |
| 106 | + const proxy = makeProxy(object) |
| 107 | + |
| 108 | + const isPrototypeOf = proxy.isPrototypeOf |
| 109 | + expect(accessedProperties).toEqual({}) |
| 110 | + }) |
| 111 | + |
| 112 | + it("has a hidden key for accessing the unproxied object from the proxy", () => { |
| 113 | + const accessedProperties = {} |
| 114 | + const makeProxy = createMakeProxyFunction({ accessedProperties }) |
| 115 | + const object = { a: { b: "c" }, d: [1, 2, 3, 4, 5] } |
| 116 | + const proxy = makeProxy(object) |
| 117 | + |
| 118 | + expect(isProxy(proxy)).toBe(true) |
| 119 | + expect(isProxy(proxy[UNPROXIED_OBJ_KEY])).toBe(false) |
| 120 | + expect(object).toEqual(proxy[UNPROXIED_OBJ_KEY]) |
| 121 | + }) |
| 122 | + |
| 123 | + it("makes sure never to proxy an already-proxied object", () => { |
| 124 | + const accessedProperties = {} |
| 125 | + const makeProxy = createMakeProxyFunction({ accessedProperties }) |
| 126 | + const object = { a: { b: "c" }, d: [1, 2, 3, 4, 5] } |
| 127 | + const doubleProxy = makeProxy(makeProxy(object)) |
| 128 | + |
| 129 | + expect(isProxy(doubleProxy)).toBe(true) |
| 130 | + expect(isDoubleProxied(doubleProxy)).toBe(false) |
| 131 | + }) |
| 132 | +}) |
0 commit comments