forked from aholachek/redux-usage-report
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasicProxyBehaviorTest.js
56 lines (51 loc) · 1.97 KB
/
basicProxyBehaviorTest.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
describe("basic proxy behavior", () => {
it("if a proxied object is proxied again, both proxies will be active, rather than the second proxy simply overriding the first", () => {
const handler1 = {
get(target, propKey) {
return "proxied get"
}
}
const handler2 = {
get(target, propKey) {
return `${target[propKey]} is wrapped with an outer proxy`
}
}
const proxy = new Proxy({ test: 1 }, handler1)
const wrappedProxy = new Proxy(proxy, handler2)
expect(proxy.test).toBe("proxied get")
expect(wrappedProxy.test).toBe("proxied get is wrapped with an outer proxy")
})
it("there can be a special hidden key that just returns the unproxied target to avoid this proxy nesting", () => {
const handler1 = {
get(target, propKey) {
if (propKey === "__initialVal") return target
if (target.__initialVal !== undefined) return target.__initialVal[propKey]
return "proxied get"
}
}
const handler2 = {
get(target, propKey) {
const value =
target.__initialVal !== undefined ? target.__initialVal[propKey] : target[propKey]
return `${value} is wrapped with an outer proxy`
}
}
const proxy = new Proxy({ test: 1 }, handler1)
const wrappedProxy = new Proxy(proxy, handler2)
expect(proxy.test).toBe("proxied get")
expect(proxy.__initialVal.test).toBe(1)
expect(wrappedProxy.test).toBe("1 is wrapped with an outer proxy")
})
it("JSON.parse(JSON.stringify(obj)) will return a non-proxied object from a proxied object, after calling the proxy's get methods", () => {
const handler1 = {
get: jest.fn(function(target, propKey) {
return "proxied get"
})
}
const proxy = new Proxy({ test: 1 }, handler1)
const copied = JSON.parse(JSON.stringify(proxy))
expect(handler1.get.mock.calls.length).toBe(2)
expect(copied.test).toBe("proxied get")
expect(handler1.get.mock.calls.length).toBe(2)
})
})