|
| 1 | +/* eslint-disable @typescript-eslint/no-unsafe-call */ |
| 2 | +/* eslint-disable @typescript-eslint/no-unsafe-assignment */ |
| 3 | +import { replayProxy } from "./replayProxy"; |
| 4 | +import { hasPathBeenVisited } from "~/hasPathBeenVisited"; |
| 5 | +import { ProxySymbol } from "~/ProxySymbol"; |
| 6 | +import { recursiveProxyMock } from "~/recursiveProxyMock"; |
| 7 | + |
| 8 | +describe("replayProxy", () => { |
| 9 | + test("console.warn when argument isn't a proxy mock", () => { |
| 10 | + const consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(); |
| 11 | + replayProxy(null, []); |
| 12 | + expect(consoleWarnSpy).toHaveBeenCalled(); |
| 13 | + }); |
| 14 | + |
| 15 | + test("example: store and replay operations", () => { |
| 16 | + const greeter = jest.fn(); |
| 17 | + class TestClass { |
| 18 | + public e(name: string): void { |
| 19 | + greeter(`Hi ${name}`); |
| 20 | + } |
| 21 | + } |
| 22 | + const target = { |
| 23 | + a: { |
| 24 | + b: function (): { c: TestClass } { |
| 25 | + return { |
| 26 | + c: new TestClass(), |
| 27 | + }; |
| 28 | + }, |
| 29 | + }, |
| 30 | + }; |
| 31 | + const proxy = recursiveProxyMock<typeof target>(); |
| 32 | + proxy.a.b().c.e("Jason"); |
| 33 | + replayProxy(proxy, target); |
| 34 | + expect(greeter).toHaveBeenCalledWith("Hi Jason"); |
| 35 | + }); |
| 36 | + |
| 37 | + test("replay: apply", () => { |
| 38 | + const target = jest.fn(); |
| 39 | + const proxy = recursiveProxyMock(); |
| 40 | + proxy("potato"); |
| 41 | + replayProxy(proxy, target); |
| 42 | + expect(target).toHaveBeenCalledWith("potato"); |
| 43 | + }); |
| 44 | + |
| 45 | + test("replay: construct", () => { |
| 46 | + const greeter = jest.fn(); |
| 47 | + class TestClass { |
| 48 | + public e(name: string): void { |
| 49 | + greeter(`Hi ${name}`); |
| 50 | + } |
| 51 | + } |
| 52 | + const proxy = recursiveProxyMock<typeof TestClass>(); |
| 53 | + new proxy().e("test"); |
| 54 | + replayProxy(proxy, TestClass); |
| 55 | + expect(greeter).toHaveBeenCalledWith("Hi test"); |
| 56 | + }); |
| 57 | + |
| 58 | + test("replay: defineProperty", () => { |
| 59 | + const obj: Record<string, Record<string, number>> = {}; |
| 60 | + const proxy = recursiveProxyMock<typeof obj>(); |
| 61 | + Object.defineProperty(proxy, "k", { |
| 62 | + value: {}, |
| 63 | + enumerable: true, |
| 64 | + configurable: true, |
| 65 | + writable: true, |
| 66 | + }); |
| 67 | + Object.defineProperty(proxy.k, "number", { |
| 68 | + value: 100, |
| 69 | + enumerable: true, |
| 70 | + configurable: true, |
| 71 | + writable: true, |
| 72 | + }); |
| 73 | + replayProxy(proxy, obj); |
| 74 | + expect(obj).toStrictEqual({ |
| 75 | + k: { |
| 76 | + number: 100, |
| 77 | + }, |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + test("replay: deleteProperty", () => { |
| 82 | + type PartialObj = { |
| 83 | + num?: number; |
| 84 | + person: { |
| 85 | + name?: string; |
| 86 | + }; |
| 87 | + }; |
| 88 | + const obj: PartialObj = { |
| 89 | + num: 7, |
| 90 | + person: { |
| 91 | + name: "Joe", |
| 92 | + }, |
| 93 | + }; |
| 94 | + const proxy = recursiveProxyMock<PartialObj>(); |
| 95 | + delete proxy.num; |
| 96 | + delete proxy.person.name; |
| 97 | + replayProxy(proxy, obj); |
| 98 | + expect(obj).toStrictEqual({ |
| 99 | + person: {}, |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + test("replay: get", () => { |
| 104 | + const getter = jest.fn(); |
| 105 | + const obj = { |
| 106 | + a: { |
| 107 | + b: { |
| 108 | + get c(): number { |
| 109 | + getter(); |
| 110 | + return 7; |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }; |
| 115 | + const proxy = recursiveProxyMock<typeof obj>(); |
| 116 | + // eslint-disable-next-line @typescript-eslint/no-unused-expressions |
| 117 | + proxy.a.b.c; |
| 118 | + replayProxy(proxy, obj); |
| 119 | + expect(getter).toHaveBeenCalled(); |
| 120 | + }); |
| 121 | + |
| 122 | + test("replay: getOwnPropertyDescriptor", () => { |
| 123 | + const obj = recursiveProxyMock<unknown>(); |
| 124 | + const proxy = recursiveProxyMock<typeof obj>(); |
| 125 | + Object.getOwnPropertyDescriptor(Object.getOwnPropertyDescriptor(proxy, "person")!.value, "name"); |
| 126 | + replayProxy(proxy, obj); |
| 127 | + expect( |
| 128 | + hasPathBeenVisited(obj, [ |
| 129 | + "person", |
| 130 | + ProxySymbol.GET_OWN_PROPERTY_DESCRIPTOR, |
| 131 | + "name", |
| 132 | + ProxySymbol.GET_OWN_PROPERTY_DESCRIPTOR, |
| 133 | + ]) |
| 134 | + ).toStrictEqual(true); |
| 135 | + }); |
| 136 | + |
| 137 | + test("replay: getPrototypeOf", () => { |
| 138 | + const obj = recursiveProxyMock<unknown>(); |
| 139 | + const proxy = recursiveProxyMock<typeof obj>(); |
| 140 | + Object.getPrototypeOf(proxy); |
| 141 | + replayProxy(proxy, obj); |
| 142 | + expect(hasPathBeenVisited(obj, [ProxySymbol.GET_PROTOTYPE_OF])).toStrictEqual(true); |
| 143 | + }); |
| 144 | + |
| 145 | + test("replay: has", () => { |
| 146 | + const obj = recursiveProxyMock<Record<string, unknown>>(); |
| 147 | + const proxy = recursiveProxyMock<typeof obj>(); |
| 148 | + // eslint-disable-next-line @typescript-eslint/no-unused-expressions |
| 149 | + "favorite" in proxy; |
| 150 | + replayProxy(proxy, obj); |
| 151 | + expect(hasPathBeenVisited(obj, ["favorite", ProxySymbol.HAS])).toStrictEqual(true); |
| 152 | + }); |
| 153 | + |
| 154 | + test("replay: isExtensible", () => { |
| 155 | + const obj = recursiveProxyMock<unknown>(); |
| 156 | + const proxy = recursiveProxyMock<typeof obj>(); |
| 157 | + Object.isExtensible(proxy); |
| 158 | + replayProxy(proxy, obj); |
| 159 | + expect(hasPathBeenVisited(obj, [ProxySymbol.IS_EXTENSIBLE])).toStrictEqual(true); |
| 160 | + }); |
| 161 | + |
| 162 | + test("replay: isOwnKeys", () => { |
| 163 | + const obj = recursiveProxyMock<Record<string, unknown>>(); |
| 164 | + const proxy = recursiveProxyMock<typeof obj>(); |
| 165 | + Object.keys(proxy); |
| 166 | + replayProxy(proxy, obj); |
| 167 | + expect(hasPathBeenVisited(obj, [ProxySymbol.OWN_KEYS])).toStrictEqual(true); |
| 168 | + }); |
| 169 | + |
| 170 | + test("replay: preventExtensions", () => { |
| 171 | + const obj = recursiveProxyMock<Record<string, unknown>>(); |
| 172 | + const proxy = recursiveProxyMock<typeof obj>(); |
| 173 | + expect(() => { |
| 174 | + Object.preventExtensions(proxy); |
| 175 | + }).toThrow(TypeError); |
| 176 | + expect(() => { |
| 177 | + replayProxy(proxy, obj); |
| 178 | + }).toThrow(TypeError); |
| 179 | + expect(hasPathBeenVisited(obj, [ProxySymbol.PREVENT_EXTENSIONS])).toStrictEqual(true); |
| 180 | + }); |
| 181 | + |
| 182 | + test("replay: set", () => { |
| 183 | + const obj = { |
| 184 | + num: 7, |
| 185 | + person: { |
| 186 | + name: "Joe", |
| 187 | + }, |
| 188 | + }; |
| 189 | + const proxy = recursiveProxyMock<typeof obj>(); |
| 190 | + proxy.num = 10; |
| 191 | + proxy.person.name = "Bob"; |
| 192 | + replayProxy(proxy, obj); |
| 193 | + expect(obj).toStrictEqual({ |
| 194 | + num: 10, |
| 195 | + person: { |
| 196 | + name: "Bob", |
| 197 | + }, |
| 198 | + }); |
| 199 | + }); |
| 200 | + |
| 201 | + test("replay: setPrototypeOf", () => { |
| 202 | + // eslint-disable-next-line @typescript-eslint/no-empty-function |
| 203 | + function target(): void {} |
| 204 | + const proxy = recursiveProxyMock<typeof target>(); |
| 205 | + const prototype = { |
| 206 | + getName: jest.fn(), |
| 207 | + }; |
| 208 | + Object.setPrototypeOf(proxy, prototype); |
| 209 | + replayProxy(proxy, target); |
| 210 | + expect(Object.getPrototypeOf(target)).toStrictEqual(prototype); |
| 211 | + }); |
| 212 | +}); |
0 commit comments