Skip to content

Commit

Permalink
Merge pull request #138 from drborges/bug-fixes
Browse files Browse the repository at this point in the history
Bug fixes
  • Loading branch information
drborges authored Oct 13, 2024
2 parents 0f44df6 + a1cdf97 commit b462e44
Show file tree
Hide file tree
Showing 43 changed files with 1,305 additions and 122 deletions.
91 changes: 91 additions & 0 deletions examples/cypress/component/ShortCircuit.cy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import React from "react"

import { App } from "../../short-circuit/App"

describe("ShortCircuit App", () => {
it("successfully renders components even when short circuit boolean logic is used", () => {
cy.mount(<App />)

cy.contains("store.state.flag1: false")
cy.contains("store.state.flag2: false")
cy.contains("store.state.flag3: false")
cy.contains(
"store.state.flag1 || store.state.flag2 || store.state.flag3 = false"
)
cy.contains(
"store.state.flag1 && store.state.flag2 && store.state.flag3 = false"
)

cy.get("[data-testid='toggle-flag3']").click()

cy.contains("store.state.flag1: false")
cy.contains("store.state.flag2: false")
cy.contains("store.state.flag3: true")
cy.contains(
"store.state.flag1 || store.state.flag2 || store.state.flag3 = true"
)
cy.contains(
"store.state.flag1 && store.state.flag2 && store.state.flag3 = false"
)

cy.get("[data-testid='toggle-flag2']").click()

cy.contains("store.state.flag1: false")
cy.contains("store.state.flag2: true")
cy.contains("store.state.flag3: true")
cy.contains(
"store.state.flag1 || store.state.flag2 || store.state.flag3 = true"
)
cy.contains(
"store.state.flag1 && store.state.flag2 && store.state.flag3 = false"
)

cy.get("[data-testid='toggle-flag1']").click()

cy.contains("store.state.flag1: true")
cy.contains("store.state.flag2: true")
cy.contains("store.state.flag3: true")
cy.contains(
"store.state.flag1 || store.state.flag2 || store.state.flag3 = true"
)
cy.contains(
"store.state.flag1 && store.state.flag2 && store.state.flag3 = true"
)

cy.get("[data-testid='toggle-flag3']").click()

cy.contains("store.state.flag1: true")
cy.contains("store.state.flag2: true")
cy.contains("store.state.flag3: false")
cy.contains(
"store.state.flag1 || store.state.flag2 || store.state.flag3 = true"
)
cy.contains(
"store.state.flag1 && store.state.flag2 && store.state.flag3 = false"
)

cy.get("[data-testid='toggle-flag2']").click()

cy.contains("store.state.flag1: true")
cy.contains("store.state.flag2: false")
cy.contains("store.state.flag3: false")
cy.contains(
"store.state.flag1 || store.state.flag2 || store.state.flag3 = true"
)
cy.contains(
"store.state.flag1 && store.state.flag2 && store.state.flag3 = false"
)

cy.get("[data-testid='toggle-flag1']").click()

cy.contains("store.state.flag1: false")
cy.contains("store.state.flag2: false")
cy.contains("store.state.flag3: false")
cy.contains(
"store.state.flag1 || store.state.flag2 || store.state.flag3 = false"
)
cy.contains(
"store.state.flag1 && store.state.flag2 && store.state.flag3 = false"
)
})
})
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()
})
})
})
})
3 changes: 2 additions & 1 deletion examples/react-counter/Counter.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Arbor, useArbor } from "@arborjs/react"
import { Arbor } from "@arborjs/store"
import { useArbor } from "@arborjs/react"
import React, { memo } from "react"

import "./styles.css"
Expand Down
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>
)
}
2 changes: 1 addition & 1 deletion examples/react-todo/components/NewTodoForm.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { memo, SyntheticEvent } from "react"

import useNewTodo from "../store/useNewTodo"
import { store, Todo } from "../store/useTodos"
import { store } from "../store/useTodos"
import { activate, store as filterStore } from "../store/useTodosFilter"

export default memo(function NewTodoForm() {
Expand Down
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
3 changes: 2 additions & 1 deletion examples/react-todo/store/useNewTodo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Arbor } from "@arborjs/store"
import { useArbor } from "@arborjs/react"
import { LocalStorage, Logger } from "@arborjs/plugins"
import { Arbor, useArbor } from "@arborjs/react"

export interface Input {
value: string
Expand Down
2 changes: 1 addition & 1 deletion examples/react-todo/store/useTodos.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Json, SerializedBy, serializable } from "@arborjs/json"
import { LocalStorage, Logger } from "@arborjs/plugins"
import { Arbor, ArborNode, detach, proxiable } from "@arborjs/react"
import { Arbor, ArborNode, detach, proxiable } from "@arborjs/store"
import { v4 as uuid } from "uuid"

export type Status = "completed" | "active"
Expand Down
3 changes: 2 additions & 1 deletion examples/react-todo/store/useTodosFilter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LocalStorage, Logger } from "@arborjs/plugins"
import { Arbor, useArbor } from "@arborjs/react"
import { useArbor } from "@arborjs/react"
import { Arbor } from "@arborjs/store"

import { TodoList } from "./useTodos"

Expand Down
20 changes: 20 additions & 0 deletions examples/short-circuit/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from "react"

import { Highlight } from "./components/Highlight"
import { StoreState } from "./components/StoreState"
import { Actions } from "./components/Actions"
import { AreThereAnyFlagsTrue } from "./components/AreThereAnyFlagsTrue"
import { AreAllFlagsTrue } from "./components/AreAllFlagsTrue"

import "./styles.css"

export function App() {
return (
<Highlight label="<App />" key={Math.random()}>
<Actions />
<StoreState />
<AreThereAnyFlagsTrue />
<AreAllFlagsTrue />
</Highlight>
)
}
20 changes: 20 additions & 0 deletions examples/short-circuit/components/Actions.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from "react"

import { Highlight } from "./Highlight"
import { store } from "../store"

export function Actions() {
return (
<Highlight label="<Actions />" key={Math.random()}>
<button data-testid="toggle-flag1" onClick={store.state.toggleFlag1}>
Toggle <code>store.state.flag1</code>
</button>
<button data-testid="toggle-flag2" onClick={store.state.toggleFlag2}>
Toggle <code>store.state.flag2</code>
</button>
<button data-testid="toggle-flag3" onClick={store.state.toggleFlag3}>
Toggle <code>store.state.flag3</code>
</button>
</Highlight>
)
}
17 changes: 17 additions & 0 deletions examples/short-circuit/components/AreAllFlagsTrue.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from "react"
import { useArbor } from "@arborjs/react"

import { Highlight } from "./Highlight"
import { store } from "../store"

export function AreAllFlagsTrue() {
const app = useArbor(store)
return (
<Highlight label="<AreAllFlagsTrue />" key={Math.random()}>
<code>
store.state.flag1 && store.state.flag2 && store.state.flag3 ={" "}
{(app.flag1 && app.flag2 && app.flag3).toString()}
</code>
</Highlight>
)
}
17 changes: 17 additions & 0 deletions examples/short-circuit/components/AreThereAnyFlagsTrue.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from "react"
import { useArbor } from "@arborjs/react"

import { Highlight } from "./Highlight"
import { store } from "../store"

export function AreThereAnyFlagsTrue() {
const app = useArbor(store)
return (
<Highlight label="<AreThereAnyFlagsTrue />" key={Math.random()}>
<code>
store.state.flag1 || store.state.flag2 || store.state.flag3 ={" "}
{(app.flag1 || app.flag2 || app.flag3).toString()}
</code>
</Highlight>
)
}
21 changes: 21 additions & 0 deletions examples/short-circuit/components/Highlight.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from "react"

import { useAnimatedClassName } from "../hooks/useAnimatedClassName"

export function Highlight({
key,
children,
label,
}: {
key: number
children: React.ReactNode
label: string
}) {
const className = useAnimatedClassName(key)
return (
<div className={`highlight ${className}`}>
<span>{label}</span>
{children}
</div>
)
}
23 changes: 23 additions & 0 deletions examples/short-circuit/components/StoreState.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from "react"
import { useArbor } from "@arborjs/react"

import { Highlight } from "./Highlight"
import { store } from "../store"

export function StoreState() {
const state = useArbor(store)

return (
<Highlight label="<StoreState />" key={Math.random()}>
<div>
<code>store.state.flag1: {state.flag1.toString()}</code>
</div>
<div>
<code>store.state.flag2: {state.flag2.toString()}</code>
</div>
<div>
<code>store.state.flag3: {state.flag3.toString()}</code>
</div>
</Highlight>
)
}
16 changes: 16 additions & 0 deletions examples/short-circuit/hooks/useAnimatedClassName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useEffect, useState } from "react"

export function useAnimatedClassName(key: number) {
const [className, setClassName] = useState("")

useEffect(() => {
setClassName("animate")
const timeoutId = setTimeout(() => {
setClassName("")
}, 1000)

return () => clearTimeout(timeoutId)
}, [key])

return className
}
5 changes: 5 additions & 0 deletions examples/short-circuit/store/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Arbor } from "@arborjs/store"

import { ShortCircuitApp } from "./models/ShortCircuitApp"

export const store = new Arbor(new ShortCircuitApp())
Loading

0 comments on commit b462e44

Please sign in to comment.