-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from udecode/fix/60
Fix #60
- Loading branch information
Showing
9 changed files
with
176 additions
and
21 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,6 @@ | ||
--- | ||
'zustand-x': patch | ||
--- | ||
|
||
- Fixes #60 – `[DEPRECATED] Passing a vanilla store will be unsupported in a future version` | ||
- Support `equalityFn` towards v5. See https://github.com/pmndrs/zustand/discussions/1937. |
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,135 @@ | ||
import '@testing-library/jest-dom'; | ||
|
||
import React from 'react'; | ||
import { act, render, renderHook } from '@testing-library/react'; | ||
|
||
import { createZustandStore } from './createStore'; | ||
|
||
describe('createAtomStore', () => { | ||
describe('single provider', () => { | ||
type MyTestStoreValue = { | ||
name: string; | ||
age: number; | ||
}; | ||
|
||
const INITIAL_NAME = 'John'; | ||
const INITIAL_AGE = 42; | ||
|
||
const initialTestStoreValue: MyTestStoreValue = { | ||
name: INITIAL_NAME, | ||
age: INITIAL_AGE, | ||
}; | ||
|
||
const store = createZustandStore('myTestStore')(initialTestStoreValue); | ||
const useSelectors = () => store.use; | ||
const actions = store.set; | ||
const selectors = store.get; | ||
|
||
const ReadOnlyConsumer = () => { | ||
const name = useSelectors().name(); | ||
const age = useSelectors().age(); | ||
|
||
return ( | ||
<div> | ||
<span>{name}</span> | ||
<span>{age}</span> | ||
</div> | ||
); | ||
}; | ||
|
||
const WriteOnlyConsumer = () => { | ||
return ( | ||
<button | ||
type="button" | ||
onClick={() => { | ||
selectors.age(); | ||
actions.age(selectors.age() + 1); | ||
}} | ||
> | ||
consumerSetAge | ||
</button> | ||
); | ||
}; | ||
|
||
beforeEach(() => { | ||
renderHook(() => actions.name(INITIAL_NAME)); | ||
renderHook(() => actions.age(INITIAL_AGE)); | ||
}); | ||
|
||
it('read only', () => { | ||
const { getByText } = render(<ReadOnlyConsumer />); | ||
|
||
expect(getByText(INITIAL_NAME)).toBeInTheDocument(); | ||
expect(getByText(INITIAL_AGE)).toBeInTheDocument(); | ||
}); | ||
|
||
it('actions', () => { | ||
const { getByText } = render( | ||
<> | ||
<ReadOnlyConsumer /> | ||
<WriteOnlyConsumer /> | ||
</> | ||
); | ||
expect(getByText(INITIAL_NAME)).toBeInTheDocument(); | ||
expect(getByText(INITIAL_AGE)).toBeInTheDocument(); | ||
|
||
act(() => getByText('consumerSetAge').click()); | ||
|
||
expect(getByText(INITIAL_NAME)).toBeInTheDocument(); | ||
expect(getByText(INITIAL_AGE + 1)).toBeInTheDocument(); | ||
expect(store.store.getState().age).toBe(INITIAL_AGE + 1); | ||
}); | ||
}); | ||
|
||
describe('multiple unrelated stores', () => { | ||
type MyFirstTestStoreValue = { name: string }; | ||
type MySecondTestStoreValue = { age: number }; | ||
|
||
const initialFirstTestStoreValue: MyFirstTestStoreValue = { | ||
name: 'My name', | ||
}; | ||
|
||
const initialSecondTestStoreValue: MySecondTestStoreValue = { | ||
age: 72, | ||
}; | ||
|
||
const myFirstTestStoreStore = createZustandStore('myFirstTestStore')( | ||
initialFirstTestStoreValue | ||
); | ||
const mySecondTestStoreStore = createZustandStore('mySecondTestStore')( | ||
initialSecondTestStoreValue | ||
); | ||
|
||
const FirstReadOnlyConsumer = () => { | ||
const name = myFirstTestStoreStore.use.name(); | ||
|
||
return ( | ||
<div> | ||
<span>{name}</span> | ||
</div> | ||
); | ||
}; | ||
|
||
const SecondReadOnlyConsumer = () => { | ||
const age = mySecondTestStoreStore.use.age(); | ||
|
||
return ( | ||
<div> | ||
<span>{age}</span> | ||
</div> | ||
); | ||
}; | ||
|
||
it('returns the value for the correct store', () => { | ||
const { getByText } = render( | ||
<> | ||
<FirstReadOnlyConsumer /> | ||
<SecondReadOnlyConsumer /> | ||
</> | ||
); | ||
|
||
expect(getByText('My name')).toBeInTheDocument(); | ||
expect(getByText(72)).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
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
13 changes: 10 additions & 3 deletions
13
packages/zustand-x/src/utils/generateStateHookSelectors.ts
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
8 changes: 4 additions & 4 deletions
8
packages/zustand-x/src/utils/generateStateTrackedHooksSelectors.ts
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import '@testing-library/jest-dom'; | ||
|
||
jest.spyOn(global.console, 'warn').mockImplementation(() => jest.fn()); | ||
jest.spyOn(global.console, 'warn').mockImplementation((message) => { | ||
throw new Error(message); | ||
}); |