From ce402284934551c872f429ccf5eec91bae5bc5fd Mon Sep 17 00:00:00 2001 From: Yann Leflour Date: Mon, 19 May 2025 17:19:01 +0200 Subject: [PATCH] test(enhance): add behavior tests --- packages/enhance/src/stub.test.ts | 44 +++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/packages/enhance/src/stub.test.ts b/packages/enhance/src/stub.test.ts index b542700..65f44d1 100644 --- a/packages/enhance/src/stub.test.ts +++ b/packages/enhance/src/stub.test.ts @@ -1,3 +1,43 @@ -import { describe } from "node:test"; +import assert from "node:assert/strict"; +import { describe, it } from "node:test"; +import { enhance } from "./enhance.lib.ts"; -describe("Stub", () => {}); +describe("enhance", () => { + it("enhances objects with custom methods", () => { + const base = { value: 1 }; + const enhanced = enhance("numbers", base, { + double: function () { + return this.value * 2; + }, + add: function (n: number) { + return this.value + n; + }, + }); + + assert.equal(enhanced.double(), 2); + assert.equal(enhanced.add(3), 4); + assert.strictEqual(enhanced.$(), base); + }); + + it("does not mutate the original object when enhanced again", () => { + const base = { value: 1 }; + const first = enhance("numbers", base, { + inc: function () { + return this.value + 1; + }, + }); + + const second = enhance("numbers", first, { + double: function () { + return this.value * 2; + }, + }); + + assert.equal(typeof (first as any).double, "undefined"); + assert.deepEqual(base, { value: 1 }); + assert.equal(first.inc(), 2); + assert.equal(second.double(), 2); + assert.strictEqual(first.$(), base); + assert.strictEqual(second.$().$(), base); + }); +});