-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
restructure __internals to shave bytes; add react tests (#82)
- Loading branch information
Showing
12 changed files
with
966 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { TemporalState, temporal } from 'zundo'; | ||
import { StoreApi, useStore, create } from 'zustand'; | ||
|
||
interface MyState { | ||
bears: number; | ||
increment: () => void; | ||
decrement: () => void; | ||
} | ||
|
||
const useMyStore = create( | ||
temporal<MyState>((set) => ({ | ||
bears: 0, | ||
increment: () => set((state) => ({ bears: state.bears + 1 })), | ||
decrement: () => set((state) => ({ bears: state.bears - 1 })), | ||
})), | ||
); | ||
|
||
type ExtractState<S> = S extends { | ||
getState: () => infer T; | ||
} ? T : never; | ||
type ReadonlyStoreApi<T> = Pick<StoreApi<T>, 'getState' | 'subscribe'>; | ||
type WithReact<S extends ReadonlyStoreApi<unknown>> = S & { | ||
getServerState?: () => ExtractState<S>; | ||
}; | ||
|
||
const useTemporalStore = <S extends WithReact<StoreApi<TemporalState<MyState>>>, U>( | ||
selector: (state: ExtractState<S>) => U, | ||
equality?: (a: U, b: U) => boolean, | ||
): U => { | ||
const state = useStore(useMyStore.temporal as any, selector, equality); | ||
return state | ||
} | ||
|
||
const HistoryBar = () => { | ||
const futureStates = useTemporalStore((state) => state.futureStates); | ||
const pastStates = useTemporalStore((state) => state.pastStates); | ||
return ( | ||
<div> | ||
past states: {JSON.stringify(pastStates)} | ||
<br /> | ||
future states: {JSON.stringify(futureStates)} | ||
<br /> | ||
</div> | ||
); | ||
}; | ||
|
||
const UndoBar = () => { | ||
const { undo, redo } = useTemporalStore((state) => ({ | ||
undo: state.undo, | ||
redo: state.redo, | ||
})); | ||
return ( | ||
<div> | ||
<button onClick={() => undo()}>undo</button> | ||
<button onClick={() => redo()}>redo</button> | ||
</div> | ||
); | ||
}; | ||
|
||
const StateBar = () => { | ||
const store = useMyStore(); | ||
const { bears, increment, decrement } = store; | ||
return ( | ||
<div> | ||
current state: {JSON.stringify(store)} | ||
<br /> | ||
<br /> | ||
bears: {bears} | ||
<br /> | ||
<button onClick={increment}>increment</button> | ||
<button onClick={decrement}>decrement</button> | ||
</div> | ||
); | ||
}; | ||
|
||
const App = () => { | ||
return ( | ||
<div> | ||
<h1> | ||
{' '} | ||
<span role="img" aria-label="bear"> | ||
🐻 | ||
</span>{' '} | ||
<span role="img" aria-label="recycle"> | ||
♻️ | ||
</span>{' '} | ||
Zundo! | ||
</h1> | ||
<StateBar /> | ||
<br /> | ||
<UndoBar /> | ||
<HistoryBar /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
{ | ||
"extends": "tsconfig/nextjs.json", | ||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"], | ||
"exclude": ["node_modules"] | ||
"exclude": ["node_modules"], | ||
"compilerOptions": { | ||
"jsx": "react-jsx" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import React from 'react'; | ||
import { fireEvent, render, screen } from '@testing-library/react'; | ||
import Reactive from '../../../apps/web/pages/reactive'; | ||
|
||
describe('React Re-renders when state changes', () => { | ||
it('it', () => { | ||
const { queryByLabelText, getByLabelText, queryByText, getByText } = render( | ||
<Reactive />, | ||
); | ||
|
||
expect(queryByText(/bears: 0/i)).toBeTruthy(); | ||
expect(queryByText(/increment/i)).toBeTruthy(); | ||
expect(queryByText(/past states: \[\]/i)).toBeTruthy(); | ||
expect(queryByText(/future states: \[\]/i)).toBeTruthy(); | ||
|
||
const incrementButton = getByText(/increment/i); | ||
fireEvent.click(incrementButton); | ||
fireEvent.click(incrementButton); | ||
|
||
expect(queryByText(/bears: 2/i)).toBeTruthy(); | ||
expect(queryByText(/past states: \[{"bears":0},{"bears":1}\]/i)).toBeTruthy(); | ||
expect(queryByText(/future states: \[\]/i)).toBeTruthy(); | ||
|
||
expect(queryByText(/undo/i, { | ||
selector: 'button', | ||
})).toBeTruthy(); | ||
|
||
const undoButton = getByText(/undo/i, { | ||
selector: 'button', | ||
}); | ||
|
||
fireEvent.click(undoButton); | ||
fireEvent.click(undoButton); | ||
|
||
expect(queryByText(/bears: 0/i)).toBeTruthy(); | ||
expect(queryByText(/past states: \[\]/i)).toBeTruthy(); | ||
expect(queryByText(/future states: \[{"bears":2},{"bears":1}\]/i)).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { expect, afterEach } from 'vitest'; | ||
import { cleanup } from '@testing-library/react'; | ||
import matchers from '@testing-library/jest-dom/matchers'; | ||
|
||
// extends Vitest's expect method with methods from react-testing-library | ||
expect.extend(matchers); | ||
|
||
// runs a cleanup after each test case (e.g. clearing jsdom) | ||
afterEach(() => { | ||
cleanup(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.