Skip to content

Commit

Permalink
feat: agrega jest y mejora emulator.spec.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
drusco committed Jul 20, 2023
1 parent cf9cbf0 commit 19b1370
Show file tree
Hide file tree
Showing 8 changed files with 2,694 additions and 1,583 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
},
"plugins": ["@typescript-eslint", "prettier"],
"rules": {
"@typescript-eslint/no-unused-vars": "error"
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/no-explicit-any": "off"

}
}
5 changes: 5 additions & 0 deletions jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
};
4,140 changes: 2,612 additions & 1,528 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"scripts": {
"dev": "tsc --watch",
"build": "tsc",
"test": "mocha --grep Emulator",
"test": "jest",
"lint": "eslint --ext .js,.ts src",
"lint:fix": "eslint --fix"
},
Expand All @@ -23,15 +23,18 @@
"email": "pedrogallardo7011@gmail.com"
},
"devDependencies": {
"@types/expect": "^24.3.0",
"@types/jest": "^29.5.3",
"@types/node": "^20.4.2",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"eslint": "^8.45.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-prettier": "^5.0.0",
"husky": "^8.0.3",
"mocha": "^10.2.0",
"jest": "^29.6.1",
"prettier": "^3.0.0",
"ts-jest": "^29.1.1",
"typescript": "^5.1.6"
}
}
64 changes: 35 additions & 29 deletions test/Emulator.js → src/Emulator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
import { describe, it, expect } from "@jest/globals";
import Emulator from "./Emulator";
import assert from "assert";
import Emulator from "../dist/Emulator.js";

const emulator = new Emulator();
const emulator2 = new Emulator();

describe("Emulator", function () {
describe("methods", function () {
describe("#use()", function () {
it("returns function [proxy]", function () {
//import EmulatorNS from "../src/Emulator.js";

const emulator = new Emulator({});
const emulator2 = new Emulator({});

describe("Emulator", () => {
describe("methods", () => {
describe("#use()", () => {
it(`Returns a proxy function when parameter(s) type is object | string | function | undefined`, () => {
assert.strictEqual(typeof emulator.use({}), "function");
assert.strictEqual(typeof emulator.use([]), "function");
expect(typeof emulator.use("test")).toStrictEqual("function");
assert.strictEqual(typeof emulator.use(() => {}), "function");
assert.strictEqual(typeof emulator.use(), "function");
});

it("can have an id and be found by it", function () {
const id = "unique-id";
const a = emulator.use(id);
assert.strictEqual(a, emulator.use(id));
it("Can be referenced by a string identifier", function () {
const id = "";
const proxy = emulator.use(id);
assert.strictEqual(proxy, emulator.use(id));
});

it("can be based on an external object", function () {
it("Can be referenced by an object", function () {
const object = { external: true };
const a = emulator.use(object);
assert.strictEqual(Emulator.equal(a, object), true);
assert.strictEqual(emulator.use(object), a);
assert.strictEqual(a.external, true);
const proxy = emulator.use(object);
assert.strictEqual(emulator.use(object), proxy);
assert.strictEqual(proxy.external, true);
});

it("can be based on an external array", function () {
Expand Down Expand Up @@ -52,13 +57,14 @@ describe("Emulator", function () {

it("exists within instance if searched by id", function () {
const id = "id-123";
const a = emulator.use(id);
emulator.use(id);
assert.strictEqual(emulator.used(id), true);
});

it("exists within instance if searched by object", function () {
const object = { external: true };
const a = emulator.use(object);
const proxy = emulator.use(object);
assert.strictEqual(emulator.used(proxy), true);
assert.strictEqual(emulator.used(object), true);
});
});
Expand Down Expand Up @@ -97,9 +103,9 @@ describe("Emulator", function () {
const current = emulator.count();
const expected = current + 3;

const a = emulator.use();
const b = emulator.use("b");
const c = emulator.use("c");
emulator.use();
emulator.use("b");
emulator.use("c");

assert.strictEqual(emulator.count(), expected);
});
Expand All @@ -110,9 +116,9 @@ describe("Emulator", function () {
const current = emulator.groups();
const expected = current + 2;

const a = emulator.use();
const b = emulator.use("group-b-2");
const c = emulator.use("group-c-2");
emulator.use();
emulator.use("group-b-2");
emulator.use("group-c-2");

assert.strictEqual(emulator.groups(), expected);
});
Expand All @@ -139,7 +145,7 @@ describe("Emulator", function () {
describe("#exist()", function () {
it("checks if value is group, proxy or target", function () {
const target = { external: "yes" };
const a = emulator.use(target);
emulator.use(target);
const group = "my-group-id-456";
const proxyGroup = emulator.use(group);

Expand Down Expand Up @@ -217,7 +223,7 @@ describe("Emulator", function () {

it("will not set a value to the original target", function () {
const a = emulator.use();
const deep = { deep: true };
const deep = { deep: true, property: undefined };

a.set2 = { object: true };

Expand Down Expand Up @@ -245,7 +251,7 @@ describe("Emulator", function () {

it("can delete a property from a proxy and its original target", function () {
const a = emulator.use();
const deep = { deep: true };
const deep = { deep: true, property: undefined };

a.set2 = { object: true };

Expand Down
49 changes: 28 additions & 21 deletions src/Emulator.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import EmulatorNS from "./Emulator.d";
import EmulatorNS from "./types/Emulator";
import { EventEmitter } from "events";

export default class Emulator
extends EventEmitter
implements EmulatorNS.EmulatorClass
{
constructor(options: EmulatorNS.options) {
constructor(options: EmulatorNS.options = {}) {
super();

const data: EmulatorNS.private = {
Expand All @@ -19,7 +19,10 @@ export default class Emulator
secret.set(this, data);
}

use(value: unknown): unknown {
use(
value?: object | EmulatorNS.functionLike | undefined | string,
): EmulatorNS.functionLike;
use(value?: unknown): unknown {
const isString = typeof value === "string";
const isObject = typeof value === "object";
const isFunction = typeof value === "function";
Expand All @@ -35,7 +38,7 @@ export default class Emulator
}

if (isFunction) {
if (proxies.has(value)) return value; // value is already a proxy
if (proxies.has(value as EmulatorNS.functionLike)) return value; // value is already a proxy
}

if (isObject || isFunction) {
Expand Down Expand Up @@ -64,7 +67,7 @@ export default class Emulator
return itemCount;
}

used(value: unknown): boolean {
used(value: any): boolean {
const { bindings }: EmulatorNS.private = secret.get(this);
const isGroup = typeof value === "string" && !!bindings[value];

Expand All @@ -88,7 +91,7 @@ export default class Emulator
let origin: EmulatorNS.origin;

if (typeof b === "function") {
const item = proxies.get(b);
const item = proxies.get(b as EmulatorNS.functionLike);
origin = item.origin;
}

Expand All @@ -97,7 +100,7 @@ export default class Emulator
if (item === a) return true;
origin = null;
if (typeof item === "function" && this.exists(item)) {
origin = proxies.get(item).origin;
origin = proxies.get(item as EmulatorNS.functionLike).origin;
}
}

Expand All @@ -106,10 +109,13 @@ export default class Emulator

encode(
value: EmulatorNS.traceable,
callback: EmulatorNS.functionLike,
callback?: EmulatorNS.functionLike,
): object {
if (typeof value === "function" && proxies.has(value)) {
const { id } = proxies.get(value);
if (
typeof value === "function" &&
proxies.has(value as EmulatorNS.functionLike)
) {
const { id } = proxies.get(value as EmulatorNS.functionLike);

if (typeof callback == "function") {
callback(value);
Expand All @@ -134,14 +140,14 @@ export default class Emulator

exists(item: EmulatorNS.traceable): boolean {
if (typeof item !== "function") return false;
return proxies.has(item);
return proxies.has(item as EmulatorNS.functionLike);
}

getId(item: EmulatorNS.traceable): number {
// Access the internal id of a proxy
if (typeof item !== "function") return;
if (!this.exists(item)) return;
return proxies.get(item).id;
return proxies.get(item as EmulatorNS.functionLike).id;
}

revoke(...args: EmulatorNS.functionLike[]): void {
Expand Down Expand Up @@ -324,16 +330,12 @@ const traps = {
};

const isTraceable = ((value: unknown): boolean => {
// if (typeof value !== "object") return false;
// if (value === null) return false;
// if (targets.has(value)) return false; // has item linked to it
// if (typeof value === "function" && proxies.has(value)) return false; // is item
// return true;

if (!(typeof value === "object" || typeof value === "function")) return false;
const isObject = typeof value === "object";
const isFunction = typeof value === "function";
if (!(isObject || isFunction)) return false;
if (value === null) return false;
if (targets.has(value)) return false; // has item linked to it
if (typeof value === "function" && proxies.has(value)) return false; // is item
if (isFunction && proxies.has(value as EmulatorNS.functionLike)) return false; // is item
return true;
}) satisfies EmulatorNS.isTraceable;

Expand Down Expand Up @@ -378,7 +380,12 @@ const bindTraceable = ((
groupId?: string,
): EmulatorNS.functionLike => {
if (targets.has(target)) return targets.get(target); // return proxy linked to target
if (typeof target === "function" && proxies.has(target)) return target; // target is already proxy
if (
typeof target === "function" &&
proxies.has(target as EmulatorNS.functionLike)
) {
return target as EmulatorNS.functionLike; // target is already proxy
}

const data: EmulatorNS.private = secret.get(scope);
const { bindings } = data;
Expand Down
6 changes: 5 additions & 1 deletion src/Emulator.d.ts → src/types/Emulator.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ declare namespace Emulator {
}

// eslint-disable-next-line @typescript-eslint/ban-types
type functionLike = Function;
interface functionLike extends Function {
(...args: any[]): void;
[x: string]: any;
}

type traceable = object | functionLike;
}

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
"rootDir": "./src"
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "**/*.spec.ts"]
"exclude": ["node_modules", "src/**/*.spec.ts"]
}

0 comments on commit 19b1370

Please sign in to comment.