Skip to content

Commit

Permalink
fix: ajusta testes implementando emulator.target
Browse files Browse the repository at this point in the history
  • Loading branch information
drusco committed Aug 1, 2023
1 parent 434bd91 commit 3daf879
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 64 deletions.
52 changes: 31 additions & 21 deletions src/Emulator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe("Emulator", () => {
const reference = { test: true };
const proxy = $.proxy(reference);
expect(proxy).toBe($.proxy(reference));
expect(proxy.test).toBe(true);
expect($.target(proxy.test)).toBe(true);
});

it("Returns a proxy function for undefined properties", () => {
Expand All @@ -50,17 +50,19 @@ describe("Emulator", () => {

arrayProxy.push("test");

expect(arrayProxy[0]).toBe(1);
expect(arrayProxy[1]).toBe(2);
expect(arrayProxy[2]).toBe(3);
expect($.target(arrayProxy[0])).toBe(1);
expect($.target(arrayProxy[1])).toBe(2);
expect($.target(arrayProxy[2])).toBe(3);
expect(arrayProxy[3]).toBe(reference[3]);
expect(arrayProxy[arrayProxy.length - 1]).toBe("test");
expect($.target(arrayProxy[$.target(arrayProxy.length) - 1])).toBe(
"test",
);
expect(typeof arrayProxy[reference.length - 1]).toBe("function");

expect(reference.length).toBe(5);
expect(reference.length).toBe(6);
expect(reference[reference.length]).toBe(undefined); // does not change the target

expect(arrayProxy.length).toBe(reference.length + 1);
expect($.target(arrayProxy.length)).toBe(reference.length);
expect($.target(arrayProxy.pop())).toBe("test");
});
});
Expand All @@ -72,6 +74,14 @@ describe("Emulator", () => {
const targetFromProxy = $.target(proxy);
expect(target).toBe(targetFromProxy);
});

it("Uses a newtarget from an internal function call on untraceable targets", () => {
const target = "abc";
const proxy = $.proxy(target);
const proxy2 = proxy.substring(1);
expect(typeof proxy2).toBe("function");
expect($.target(proxy2)).toBe("bc");
});
});

describe("namespace", () => {
Expand Down Expand Up @@ -166,19 +176,19 @@ describe("Proxy", () => {
delete proxy.toDelete;

expect(typeof proxy.unknown).toBe("function");
expect(proxy.number).toBe(100);
expect(proxy.null).toBe(null);
expect(proxy.boolean).toBe(true);
expect(proxy.undefined).toBe(undefined);
expect($.target(proxy.number)).toBe(100);
expect($.target(proxy.null)).toBe(null);
expect($.target(proxy.boolean)).toBe(true);
expect($.target(proxy.undefined)).toBe(undefined);
expect(typeof proxy.object).toBe("function");
expect(typeof proxy.array).toBe("function");
expect(proxy.string).toBe("test");
expect($.target(proxy.string)).toBe("test");
expect($.target(proxy.function())).toBe("test");
expect(proxy.object.boolean).toBe(false);
expect($.target(proxy.object.boolean)).toBe(false);
expect(typeof proxy.object.sub).toBe("function");
expect(proxy.object.sub.deep).toBe($.proxy(deep));
expect(proxy.object.sub.deep.test).toBe(true);
expect(proxy.toDelete).toBe(undefined);
expect($.target(proxy.object.sub.deep.test)).toBe(true);
expect($.target(proxy.toDelete)).toBe(undefined);
});

it("Will not set a value to the original target", () => {
Expand All @@ -192,7 +202,7 @@ describe("Proxy", () => {
$.proxy(deep).test = null;

expect(deep.test).toBe(true);
expect($.proxy(deep).test).toBe(null);
expect($.target($.proxy(deep).test)).toBe(null);
});

it("Can delete a property from a proxy and its original target", () => {
Expand All @@ -207,7 +217,7 @@ describe("Proxy", () => {
delete proxy.set;

expect(deep.property).toBe(undefined);
expect(proxy.set).toBe(undefined);
expect($.target(proxy.set)).toBe(undefined);
});
});

Expand Down Expand Up @@ -235,10 +245,10 @@ describe("Proxy", () => {
expect(typeof instance).toBe("function");
expect(typeof value).toBe("function");
expect(typeof test.unknown).toBe("function");
expect(instance.property).toBe(true);
expect(test.property).toBe(45);
expect(test.traceable[0]).toBe(55);
expect(test.inner).toBe(true);
expect($.target(instance.property)).toBe(true);
expect($.target(test.property)).toBe(45);
expect($.target(test.traceable[0])).toBe(55);
expect($.target(test.inner)).toBe(true);
expect(test.value).toBe($.proxy(param));
});

Expand Down
2 changes: 1 addition & 1 deletion src/utils/traps/apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const apply = (
let newTarget: any;

if (typeof target === "function") {
const value = Reflect.apply(target, that, args);
const value = Reflect.apply(scope.target(target), scope.target(that), args);

if (isTraceable(value))
newTarget = createProxy(scope, value, namespace, origin);
Expand Down
34 changes: 6 additions & 28 deletions src/utils/traps/get.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Exotic from "../../types/Exotic";
import createProxy from "../createProxy";
import isTraceable from "../isTraceable";
import findProxy from "../findProxy";
import map from "../map";

Expand All @@ -14,40 +13,19 @@ const get = (dummy: Exotic.FunctionLike, key: string): unknown => {
proxy,
};

let protoValue: any;
const untraceableTarget = !isTraceable(target);
let value: any = sandbox[key];

if (untraceableTarget) {
// get new target from sandbox
if (value === undefined) {
try {
protoValue = target[key];
// target may be untraceable
value = target[key];
} catch (error) {
/* empty */
}
}

const sandboxKeys = Reflect.ownKeys(sandbox);
const newSandboxKey = !sandboxKeys.includes(key); //&& !protoValue;

if (newSandboxKey) {
let newTarget: any;
const valueExists = map.targets.has(target) && target[key] !== undefined;
const traceable = !(valueExists && !isTraceable(target[key]));

if (valueExists) newTarget = target[key];

const value = traceable
? createProxy(scope, newTarget, namespace, origin)
: newTarget;

sandbox[key] = value;
}

if (key === "substring")
console.log(2222, newSandboxKey, protoValue, sandbox[key]);

if (protoValue) {
//return protoValue;
}
sandbox[key] = createProxy(scope, value, namespace, origin);

return Reflect.get(sandbox, key);
};
Expand Down
22 changes: 8 additions & 14 deletions src/utils/traps/set.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Exotic from "../../types/Exotic";
import createProxy from "../createProxy";
import isTraceable from "../isTraceable";
import findProxy from "../findProxy";
import map from "../map";

Expand All @@ -11,7 +10,6 @@ const set = (
): boolean => {
const proxy = findProxy(dummy);
const { scope, namespace, sandbox } = map.proxies.get(proxy);
const traceable = isTraceable(value);

const origin: Exotic.proxy.origin = {
action: "set",
Expand All @@ -20,19 +18,15 @@ const set = (
value,
};

const newValue = traceable
? createProxy(scope, value, namespace, origin)
: value;
const newValue = createProxy(scope, value, namespace, origin);
origin.value = newValue;

if (newValue === value) {
// new value is not traceable
scope.emit("action", {
action: "set",
proxy,
key,
value: newValue,
});
}
scope.emit("action", {
action: "set",
proxy,
key,
value: newValue,
});

// set new value to sandbox only
return Reflect.set(sandbox, key, newValue);
Expand Down

0 comments on commit 3daf879

Please sign in to comment.