Skip to content

Commit

Permalink
fix: ajusta tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drusco committed Aug 4, 2023
1 parent 1c61fc2 commit e7c2eae
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 60 deletions.
99 changes: 57 additions & 42 deletions src/Emulator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,38 @@ import { isTraceable } from "./utils";
const $ = new Emulator();

describe("Emulator Class", () => {
describe("(getter) keys", () => {
it("Returns the list of all binding keys in use", () => {
const key = "test";
$.bind(key);
expect($.keys.includes(key)).toBe(true);
});
});

describe("(getter) length", () => {
it("Returns the number of built proxies", () => {
const total = $.length;
$.proxy();
expect($.length).toBe(total + 1);
});
});

describe("(getter) void", () => {
it("Returns the number of revoked proxies", () => {
const revoked = $.void;
$.revoke($.proxy());
expect($.void).toBe(revoked + 1);
});
});

describe("(getter) active", () => {
it("Returns the number of active proxies", () => {
const active = $.active;
$.proxy();
expect($.active).toBe(active + 1);
});
});

describe("(method) proxy", () => {
it(`Always returns a proxy function`, () => {
expect(typeof $.proxy({})).toBe("function");
Expand Down Expand Up @@ -130,19 +162,18 @@ describe("Emulator Class", () => {
describe("(method) children", () => {
it("Can access all the direct children of a proxy", () => {
const parent = $.proxy();
parent.girl = 10;
parent.boy = 5;
parent.alien = NaN;
parent.alien.notDirectChild = true;
parent.daugter = 25;
parent.son = 30;
parent.son.parentGrandson = 2;

expect($.children(parent).length).toBe(3);
expect($.children(parent).length).toBe(2);
});

it("Can use [Symbol.iterator] to access all children as well", () => {
const parent = $.proxy();

parent.girl = 1;
parent.boy = 2;
parent.daughter = 25;
parent.son = 30;

const children = [...parent];

Expand All @@ -154,36 +185,6 @@ describe("Emulator Class", () => {
});
});

describe("(method) revoke", () => {
it("Turns a proxy unusable", () => {
const proxy = $.proxy();
$.revoke(proxy);
expect(proxy).toThrow();
expect(() => proxy.property).toThrow();
expect(() => (proxy.property = true)).toThrow();
expect(() => delete proxy.property).toThrow();
expect(() => new proxy().toThrow());
expect(() => proxy.method()).toThrow();
});
});

describe("(method) destroy", () => {
it("Destroys the emulator and turns it unusable", () => {
const $ = new Emulator();
$.destroy();
expect($.proxy).toThrow();
expect($.bind).toThrow();
});
});

describe("(method) count", () => {
it("Returns the number of proxies in the emulator", () => {
const current = $.count();
$.proxy();
expect($.count()).toBe(current + 1);
});
});

describe("(method) encode", () => {
it("Encodes a proxy synchronously", () => {
const proxy = $.proxy();
Expand All @@ -205,11 +206,25 @@ describe("Emulator Class", () => {
});
});

describe("(getter) keys", () => {
it("Returns a list of all keys in use", () => {
const ref = "refsTest";
$.bind(ref);
expect($.keys.includes(ref)).toBe(true);
describe("(method) revoke", () => {
it("Turns a proxy unusable", () => {
const proxy = $.proxy();
$.revoke(proxy);
expect(proxy).toThrow();
expect(() => proxy.property).toThrow();
expect(() => (proxy.property = true)).toThrow();
expect(() => delete proxy.property).toThrow();
expect(() => new proxy().toThrow());
expect(() => proxy.method()).toThrow();
});
});

describe("(method) destroy", () => {
it("Destroys the emulator and turns it unusable", () => {
const $ = new Emulator();
$.destroy();
expect($.proxy).toThrow();
expect($.bind).toThrow();
});
});
});
Expand Down
32 changes: 19 additions & 13 deletions src/Emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export default class Emulator extends EventEmitter implements Exotic.Emulator {
const data: Exotic.emulator.data = {
options,
keys: Object.create(null),
itemCount: 0, // total item count including revoked items, it only increases
activeItems: 0, // items that are not revoked
totalProxies: 0, // total item count including revoked items, it only increases
activeProxies: 0, // items that are not revoked
};

map.emulators.set(this, data);
Expand All @@ -21,6 +21,22 @@ export default class Emulator extends EventEmitter implements Exotic.Emulator {
return Reflect.ownKeys(keys);
}

get active(): number {
const { activeProxies }: Exotic.emulator.data = map.emulators.get(this);
return activeProxies;
}

get void(): number {
const { activeProxies, totalProxies }: Exotic.emulator.data =
map.emulators.get(this);
return totalProxies - activeProxies;
}

get length(): number {
const { totalProxies }: Exotic.emulator.data = map.emulators.get(this);
return totalProxies;
}

bind(key: Exotic.key): Exotic.Proxy {
const { keys }: Exotic.emulator.data = map.emulators.get(this);
const group = keys[key];
Expand Down Expand Up @@ -66,16 +82,6 @@ export default class Emulator extends EventEmitter implements Exotic.Emulator {
return Reflect.ownKeys(sandbox);
}

count(): number {
const { activeItems }: Exotic.emulator.data = map.emulators.get(this);
return activeItems;
}

total(): number {
const { itemCount }: Exotic.emulator.data = map.emulators.get(this);
return itemCount;
}

encode(value: unknown): unknown {
if (findProxy(value)) {
const { id } = map.proxies.get(this.proxy(value));
Expand Down Expand Up @@ -122,7 +128,7 @@ export default class Emulator extends EventEmitter implements Exotic.Emulator {

revoke();

data.activeItems -= 1;
data.activeProxies -= 1;

this.emit("revoke", id);
}
Expand Down
4 changes: 2 additions & 2 deletions src/types/Exotic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ declare namespace Exotic {
interface data {
options: options;
keys: bindings;
itemCount: number;
activeItems: number;
totalProxies: number;
activeProxies: number;
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/utils/createProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const createProxy = (
const data: Exotic.emulator.data = map.emulators.get(scope);
const { keys } = data;

const id = ++data.itemCount;
const mock = function () {} as Exotic.Mock;
const id = ++data.totalProxies;
const mock = function mock() {} as Exotic.Mock;

const traceable = isTraceable(target);
const { proxy, revoke } = Proxy.revocable<Exotic.Proxy>(
Expand Down Expand Up @@ -61,7 +61,7 @@ const createProxy = (
});

group.length += 1;
data.activeItems += 1;
data.activeProxies += 1;

map.mocks.set(mock, proxy);
map.proxies.set(proxy, proxyData);
Expand Down

0 comments on commit e7c2eae

Please sign in to comment.