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