Skip to content

Commit

Permalink
Merge pull request #137 from drborges/add-test-coverage-to-scoped-ref…
Browse files Browse the repository at this point in the history
…resh-children-links

Add test coverage to array link refresh within scoped store
  • Loading branch information
drborges authored Sep 4, 2024
2 parents 22ddbe2 + 5bd55b6 commit 03d810a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/arbor-store/tests/matchers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import "./toBeProxiedExactlyOnce"
import "./toBeSeeded"
import "./toBeTrackedNode"
import "./toBeTracking"
import "./toHaveLink"
import "./toHaveLinkFor"
import "./toHaveNodeFor"
23 changes: 23 additions & 0 deletions packages/arbor-store/tests/matchers/toHaveLink.ts
Original file line number Diff line number Diff line change
@@ -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}`,
}
},
})
19 changes: 19 additions & 0 deletions packages/arbor-store/tests/scoping/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
})
3 changes: 2 additions & 1 deletion packages/arbor-store/tests/vitest.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "vitest"
import { ArborNode } from "../src"
import { ArborNode, Link } from "../src"

declare module "vitest" {
interface Assertion<T> {
Expand All @@ -12,5 +12,6 @@ declare module "vitest" {
toBeNodeOf: (expected: unknown) => T
toHaveNodeFor: (expected: unknown) => T
toHaveLinkFor: (expected: unknown) => T
toHaveLink: (link?: Link) => T
}
}

0 comments on commit 03d810a

Please sign in to comment.