Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/arbor-store/tests/fixtures/Collection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { proxiable } from "../../src/decorators"

@proxiable
export class Collection<T> extends Array<T> {
push(...items: T[]): number {
// Testing that super is bound to the correct context
return super.push(...items)
}
}
6 changes: 6 additions & 0 deletions packages/arbor-store/tests/fixtures/Task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { proxiable } from "../../src/decorators"

@proxiable
export class Task {
constructor(public text: string, public done = false) {}
}
10 changes: 10 additions & 0 deletions packages/arbor-store/tests/fixtures/Tasks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { proxiable } from "../../src/decorators"
import { Collection } from "./Collection"
import { Task } from "./Task"

@proxiable
export class Tasks extends Collection<Task> {
addTask(text: string) {
this.push(new Task(text))
}
}
6 changes: 6 additions & 0 deletions packages/arbor-store/tests/fixtures/Todo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { proxiable } from "../../src/decorators"

@proxiable
export class Todo {
constructor(public text: string, public done = false) {}
}
20 changes: 20 additions & 0 deletions packages/arbor-store/tests/fixtures/Todos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { proxiable } from "../../src/decorators"
import { Collection } from "./Collection"
import { Todo } from "./Todo"

@proxiable
export class Todos extends Collection<Todo> {
addTodo(text: string) {
this.push(new Todo(text))
}

push(...todos: Todo[]): number {
todos.forEach((todo) => {
if (todo.done) {
throw new Error("Todo is already done")
}
})

return super.push(...todos)
}
}
34 changes: 34 additions & 0 deletions packages/arbor-store/tests/scoping/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { describe, expect, it, vi } from "vitest"
import { Arbor } from "../../src/arbor"
import { ScopedStore } from "../../src/scoping/store"
import { detach } from "../../src/utilities"
import { Todo } from "../fixtures/Todo"
import { Todos } from "../fixtures/Todos"
import { Task } from "../fixtures/Task"
import { Tasks } from "../fixtures/Tasks"

describe("Array", () => {
describe("Symbol.iterator", () => {
Expand Down Expand Up @@ -404,4 +408,34 @@ describe("Array", () => {

expect(store.state).toEqual([])
})

describe("method memoization", () => {
it("binds methods to the correct context in the inheritance hiearchy", () => {
const todo1 = new Todo("Do the dishes")
const todo2 = new Todo("Walk the dogs")
const task = new Task("Learn Arbor")
const todos = new Todos(todo1, todo2)
const tasks = new Tasks(task)

const store = new Arbor({ todos, tasks })
const scoped = new ScopedStore(store)

scoped.state.todos.addTodo("Clean the house")
scoped.state.tasks.addTask("Write tests")

expect(scoped.state.todos.addTodo).toBe(scoped.state.todos.addTodo)
expect(scoped.state.tasks.addTask).toBe(scoped.state.tasks.addTask)
expect(store.state.todos.length).toBe(3)
expect(store.state.tasks.length).toBe(2)

expect(store.state.todos[0]).toEqual(new Todo("Do the dishes"))
expect(store.state.todos[1]).toEqual(new Todo("Walk the dogs"))
expect(store.state.todos[2]).toEqual(new Todo("Clean the house"))
expect(store.state.todos[3]).toBeUndefined()

expect(store.state.tasks[0]).toEqual(new Task("Learn Arbor"))
expect(store.state.tasks[1]).toEqual(new Task("Write tests"))
expect(store.state.tasks[2]).toBeUndefined()
})
})
})