Skip to content

Commit

Permalink
Increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
drborges committed Oct 12, 2024
1 parent 05f7797 commit a1cdf97
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 1 deletion.
33 changes: 33 additions & 0 deletions examples/cypress/component/SimpleTodoList.cy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from "react"

import { TodoList, store } from "../../react-todo/store/useTodos"
import SimpleTodoListApp from "../../react-todo/SimpleApp"

describe("SimpleTodoList", () => {
beforeEach(() => {
store.setState(new TodoList())
})

it("preserves list items references within store upon removal", () => {
cy.mount(<SimpleTodoListApp />)

cy.findByTestId("add-todo-input").type("Clean the house")
cy.findByText("Add").click()

cy.findByTestId("add-todo-input").type("Walk the dogs")
cy.findByText("Add")
.click()
.then(() => {
const todo1 = store.state[0]
const todo2 = store.state[1]

cy.findByTestId(`todo-${todo1.id}`).within(() => {
cy.findByText("Delete").click()
})

cy.findByTestId(`todo-${todo2.id}`).within(() => {
cy.findByText("Delete").click()
})
})
})
})
25 changes: 25 additions & 0 deletions examples/cypress/component/TodoList.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,29 @@ describe("TodoList", () => {
cy.findByText("0 of 1 completed").should("exist")
})
})

it("preserves list items references within store upon removal", () => {
cy.mount(<TodoListApp />)

cy.findByTestId("add-todo-input").type("Clean the house")
cy.findByText("Add").click()

cy.findByTestId("add-todo-input").type("Walk the dogs")
cy.findByText("Add").click()

cy.findByText("All")
.click()
.then(() => {
const todo1 = store.state[0]
const todo2 = store.state[1]

cy.findByTestId(`todo-${todo1.id}`).within(() => {
cy.findByText("Delete").click()
})

cy.findByTestId(`todo-${todo2.id}`).within(() => {
cy.findByText("Delete").click()
})
})
})
})
24 changes: 24 additions & 0 deletions examples/react-todo/SimpleApp.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from "react"

import Summary from "./components/Summary"
import NewTodoForm from "./components/NewTodoForm"
import SimpleTodoList from "./components/SimpleTodoList"

import "./index.css"

export default function SimpleApp() {
return (
<div className="app">
<h1>todos</h1>

<div className="body">
<NewTodoForm />
<SimpleTodoList />

<div className="footer">
<Summary />
</div>
</div>
</div>
)
}
17 changes: 17 additions & 0 deletions examples/react-todo/components/SimpleTodoList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React, { memo } from "react"
import { useArbor } from "@arborjs/react"

import { TodoList, store } from "../store/useTodos"
import TodoView from "./TodoView"

export default memo(function SimpleTodoList() {
const todos = useArbor(store) as TodoList

return (
<div className="todo-list">
{todos.map((todo) => (
<TodoView key={todo.uuid} id={todo.uuid} />
))}
</div>
)
})
2 changes: 1 addition & 1 deletion examples/react-todo/components/TodoView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface TodoProps {

export default memo(function TodoView({ id }: TodoProps) {
const [editing, setEditing] = useState(false)
const todo = useArbor(store.state.find(t => t.uuid === id)!)
const todo = useArbor(store.state.find((t) => t.uuid === id)!)

return (
<div
Expand Down
15 changes: 15 additions & 0 deletions packages/arbor-store/tests/scoping/array.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it, vi } from "vitest"
import { Arbor } from "../../src/arbor"
import { ScopedStore } from "../../src/scoping/store"
import { detach } from "../../src/utilities"

describe("Array", () => {
describe("Symbol.iterator", () => {
Expand Down Expand Up @@ -389,4 +390,18 @@ describe("Array", () => {
expect(subscriber).toHaveBeenCalledTimes(2)
})
})

it("preserves tree links when deleting items via detach", () => {
const state = [{ id: 1 }, { id: 2 }]

const store = new Arbor(state)
const scoped = new ScopedStore(store)
const item1 = scoped.state[0]
const item2 = scoped.state[1]

detach(item1)
detach(item2)

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

0 comments on commit a1cdf97

Please sign in to comment.