From 5bd55b640a5c50b45868b296faa35d8eef0b54fa Mon Sep 17 00:00:00 2001 From: Diego Borges Date: Wed, 4 Sep 2024 16:08:38 -0300 Subject: [PATCH] Add test coverage to array link refresh within scoped store --- packages/arbor-store/tests/matchers/index.ts | 1 + .../arbor-store/tests/matchers/toHaveLink.ts | 23 +++++++++++++++++++ .../arbor-store/tests/scoping/store.test.ts | 19 +++++++++++++++ packages/arbor-store/tests/vitest.d.ts | 3 ++- 4 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 packages/arbor-store/tests/matchers/toHaveLink.ts diff --git a/packages/arbor-store/tests/matchers/index.ts b/packages/arbor-store/tests/matchers/index.ts index db94a34..fa56a99 100644 --- a/packages/arbor-store/tests/matchers/index.ts +++ b/packages/arbor-store/tests/matchers/index.ts @@ -5,5 +5,6 @@ import "./toBeProxiedExactlyOnce" import "./toBeSeeded" import "./toBeTrackedNode" import "./toBeTracking" +import "./toHaveLink" import "./toHaveLinkFor" import "./toHaveNodeFor" diff --git a/packages/arbor-store/tests/matchers/toHaveLink.ts b/packages/arbor-store/tests/matchers/toHaveLink.ts new file mode 100644 index 0000000..5bff9a0 --- /dev/null +++ b/packages/arbor-store/tests/matchers/toHaveLink.ts @@ -0,0 +1,23 @@ +import { expect } from "vitest" + +import { isNode } from "../../src" + +expect.extend({ + toHaveLink(received, expected) { + if (!isNode(received)) { + return { + pass: false, + actual: received, + message: () => `Received value is not an Arbor node`, + } + } + + const link = received.$tree.getLinkFor(received) + + return { + pass: link === expected, + actual: received, + message: () => `Node link is not ${expected}. Instead it was ${link}`, + } + }, +}) diff --git a/packages/arbor-store/tests/scoping/store.test.ts b/packages/arbor-store/tests/scoping/store.test.ts index 0ce9349..fd9fa61 100644 --- a/packages/arbor-store/tests/scoping/store.test.ts +++ b/packages/arbor-store/tests/scoping/store.test.ts @@ -600,4 +600,23 @@ describe("path tracking", () => { expect(scoped).toBeTracking(node, "flag1") expect(scoped).toBeTracking(node, "flag2") }) + + it("refreshes array node links successfully", () => { + const store = new Arbor({ + list: [{ id: 1 }, { id: 2 }], + }) + + const node1 = store.state.list[0] + const node2 = store.state.list[1] + + expect(node1).toHaveLink("0") + expect(node2).toHaveLink("1") + + const scoped = new ScopedStore(store) + + scoped.state.list.splice(0, 1) + + expect(node1).toHaveLink(undefined) + expect(node2).toHaveLink("0") + }) }) diff --git a/packages/arbor-store/tests/vitest.d.ts b/packages/arbor-store/tests/vitest.d.ts index f6574a9..b8ac386 100644 --- a/packages/arbor-store/tests/vitest.d.ts +++ b/packages/arbor-store/tests/vitest.d.ts @@ -1,5 +1,5 @@ import "vitest" -import { ArborNode } from "../src" +import { ArborNode, Link } from "../src" declare module "vitest" { interface Assertion { @@ -12,5 +12,6 @@ declare module "vitest" { toBeNodeOf: (expected: unknown) => T toHaveNodeFor: (expected: unknown) => T toHaveLinkFor: (expected: unknown) => T + toHaveLink: (link?: Link) => T } }