Skip to content

Commit

Permalink
Add test coverage around method binding on deep OOP hiearchy
Browse files Browse the repository at this point in the history
  • Loading branch information
drborges committed Nov 6, 2024
1 parent 547da3b commit c976d5c
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/arbor-store/tests/fixtures/Collection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { proxiable } from "../../src/decorators"

@proxiable
export class Collection<T> extends Array<T> {}
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) {}
}
10 changes: 10 additions & 0 deletions packages/arbor-store/tests/fixtures/Todos.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 { Todo } from "./Todo"

@proxiable
export class Todos extends Collection<Todo> {
addTodo(text: string) {
this.push(new Todo(text))
}
}
32 changes: 32 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,32 @@ describe("Array", () => {

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

describe("deep OOP hiearchy", () => {
it("binds methods to the correct type in the 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(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()
})
})
})

0 comments on commit c976d5c

Please sign in to comment.