Skip to content

Commit

Permalink
Increase test coverage around node handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
drborges committed Sep 7, 2024
1 parent 44b5a18 commit 219ae51
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 3 deletions.
68 changes: 68 additions & 0 deletions packages/arbor-store/tests/handlers/array.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { describe, expect, it } from "vitest"

import { Arbor } from "../../src/arbor"

describe("ArrayHandler", () => {
describe("Symbol.iterator", () => {
it("exposes child nodes via iterator", () => {
const store = new Arbor([
{ name: "Alice" },
{ name: "Bob" },
{ name: "Carol" },
])

const alice = store.state[0]
const bob = store.state[1]
const carol = store.state[2]

const iterator = store.state[Symbol.iterator]()

const user1 = iterator.next().value
const user2 = iterator.next().value
const user3 = iterator.next().value

expect(user1).toBe(alice)
expect(user2).toBe(bob)
expect(user3).toBe(carol)
})

it("exposes child nodes", () => {
const store = new Arbor([
{ name: "Alice" },
{ name: "Bob" },
{ name: "Carol" },
])

const alice = store.state[0]
const bob = store.state[1]
const carol = store.state[2]

const entries = Object.entries(store.state)

expect(entries[0][0]).toEqual("0")
expect(entries[0][1]).toBe(alice)
expect(entries[1][0]).toEqual("1")
expect(entries[1][1]).toBe(bob)
expect(entries[2][0]).toEqual("2")
expect(entries[2][1]).toBe(carol)
})

it("exposes child nodes via spread operator", () => {
const store = new Arbor([
{ name: "Alice" },
{ name: "Bob" },
{ name: "Carol" },
])

const alice = store.state[0]
const bob = store.state[1]
const carol = store.state[2]

const users = [...store.state]

expect(users[0]).toBe(alice)
expect(users[1]).toBe(bob)
expect(users[2]).toBe(carol)
})
})
})
46 changes: 46 additions & 0 deletions packages/arbor-store/tests/handlers/default.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { describe, expect, it } from "vitest"

import { Arbor } from "../../src/arbor"

describe("DefaultHandler", () => {
describe("Symbol.iterator", () => {
it("exposes child nodes via Object.entries", () => {
const store = new Arbor({
user1: { name: "Alice" },
user2: { name: "Bob" },
user3: { name: "Carol" },
})

const alice = store.state.user1
const bob = store.state.user2
const carol = store.state.user3

const entries = Object.entries(store.state)

expect(entries[0][0]).toEqual("user1")
expect(entries[0][1]).toBe(alice)
expect(entries[1][0]).toEqual("user2")
expect(entries[1][1]).toBe(bob)
expect(entries[2][0]).toEqual("user3")
expect(entries[2][1]).toBe(carol)
})

it("exposes child nodes via spread operator", () => {
const store = new Arbor({
user1: { name: "Alice" },
user2: { name: "Bob" },
user3: { name: "Carol" },
})

const alice = store.state.user1
const bob = store.state.user2
const carol = store.state.user3

const users = { ...store.state }

expect(users.user1).toBe(alice)
expect(users.user2).toBe(bob)
expect(users.user3).toBe(carol)
})
})
})
88 changes: 88 additions & 0 deletions packages/arbor-store/tests/handlers/map.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { describe, expect, it } from "vitest"

import { Arbor } from "../../src/arbor"

describe("MapHandler", () => {
describe("Symbol.iterator", () => {
it("exposes child nodes via Map#entries", () => {
const store = new Arbor(
new Map([
[0, { name: "Alice" }],
[1, { name: "Bob" }],
[2, { name: "Carol" }],
])
)

const alice = store.state.get(0)
const bob = store.state.get(1)
const carol = store.state.get(2)

const entries = store.state.entries()

const entry1 = entries.next().value
const entry2 = entries.next().value
const entry3 = entries.next().value

expect(entry1[0]).toEqual(0)
expect(entry1[1]).toBe(alice)
expect(entry2[0]).toEqual(1)
expect(entry2[1]).toBe(bob)
expect(entry3[0]).toEqual(2)
expect(entry3[1]).toBe(carol)
})

it("exposes child nodes via iterator", () => {
const store = new Arbor(
new Map([
[0, { name: "Alice" }],
[1, { name: "Bob" }],
[2, { name: "Carol" }],
])
)

const alice = store.state.get(0)
const bob = store.state.get(1)
const carol = store.state.get(2)

const entries = store.state[Symbol.iterator]()

const entry1 = entries.next().value
const entry2 = entries.next().value
const entry3 = entries.next().value

expect(entry1[0]).toEqual(0)
expect(entry1[1]).toBe(alice)
expect(entry2[0]).toEqual(1)
expect(entry2[1]).toBe(bob)
expect(entry3[0]).toEqual(2)
expect(entry3[1]).toBe(carol)
})

it("exposes child nodes via spread operator", () => {
const store = new Arbor(
new Map([
[0, { name: "Alice" }],
[1, { name: "Bob" }],
[2, { name: "Carol" }],
])
)

const alice = store.state.get(0)
const bob = store.state.get(1)
const carol = store.state.get(2)

const entries = [...store.state]

const entry1 = entries[0]
const entry2 = entries[1]
const entry3 = entries[2]

expect(entry1[0]).toEqual(0)
expect(entry1[1]).toBe(alice)
expect(entry2[0]).toEqual(1)
expect(entry2[1]).toBe(bob)
expect(entry3[0]).toEqual(2)
expect(entry3[1]).toBe(carol)
})
})
})
27 changes: 24 additions & 3 deletions packages/arbor-store/tests/scoping/map.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
import { describe, expect, it } from "vitest"
import { Arbor } from "../../src/arbor"
import { ScopedStore } from "../../src/scoping/store"
import { unwrap } from "../../src/utilities"

describe("map", () => {
describe("#get", () => {
it("access children nodes", () => {
const bob = { name: "Bob" }
const alice = { name: "Alice" }
const store = new Arbor(
new Map([
[1, alice],
[2, bob],
])
)

const scope = new ScopedStore(store)

const scopedNode1 = scope.state.get(1)
const scopedNode2 = scope.state.get(2)

expect(unwrap(scopedNode1)).toBeNodeOf(alice)
expect(unwrap(scopedNode2)).toBeNodeOf(bob)
expect(scopedNode1).toBeTrackedNode()
expect(scopedNode2).toBeTrackedNode()
})
})

describe("Symbol.iterator", () => {
it("can convert a map node from a scoped store to array", () => {
const store = new Arbor(
Expand Down Expand Up @@ -33,9 +57,6 @@ describe("map", () => {

const scope = new ScopedStore(store)

expect(scope.state.get(1)).toBeTrackedNode()
expect(scope.state.get(2)).toBeTrackedNode()

// const list = Array.from(scope.state)

// expect(list[0][1]).toBeNodeOf(alice)
Expand Down

0 comments on commit 219ae51

Please sign in to comment.