-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create use posts store initial and add tests
feat #30
- Loading branch information
1 parent
12faee0
commit 44a1ffb
Showing
3 changed files
with
67 additions
and
0 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,14 @@ | ||
import * as zustand from 'zustand'; | ||
|
||
const actualCreate = zustand.create; | ||
export const storeResetFns = new Set<() => void>(); | ||
export const myCustomCreate = <T>() => { | ||
return (stateCreator: zustand.StateCreator<T>) => { | ||
const store = actualCreate(stateCreator); | ||
const initialState = store.getState(); | ||
storeResetFns.add(() => { | ||
store.setState(initialState, true); | ||
}); | ||
return store; | ||
}; | ||
}; |
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,53 @@ | ||
import { renderHook, act } from '@testing-library/react'; | ||
import * as zustand from 'zustand'; | ||
|
||
import { usePostsStore } from './usePostsStore'; | ||
|
||
import { myCustomCreate, storeResetFns } from './__mocks__/zunstandMock'; | ||
|
||
jest.spyOn(zustand, 'create').mockImplementation(myCustomCreate as never); | ||
|
||
describe('usePostsStore', () => { | ||
afterEach(() => { | ||
act(() => { | ||
storeResetFns.forEach((resetFn) => { | ||
resetFn(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when initialize', () => { | ||
it('posts should be zero', () => { | ||
const { result } = renderHook(() => usePostsStore((state) => state)); | ||
expect(result.current.postsQuantity).toBe(0); | ||
}); | ||
}); | ||
|
||
describe('when increased the number of posts', () => { | ||
it('set value to tree', () => { | ||
const { result } = renderHook(() => usePostsStore((state) => state)); | ||
|
||
act(() => { | ||
result.current.increase(3); | ||
}); | ||
|
||
expect(result.current.postsQuantity).toBe(3); | ||
}); | ||
}); | ||
|
||
describe('when we reset the number of posts', () => { | ||
it('change to tree and reset to zero', () => { | ||
const { result } = renderHook(() => usePostsStore((state) => state)); | ||
|
||
act(() => { | ||
result.current.increase(2); | ||
}); | ||
|
||
act(() => { | ||
result.current.reset(); | ||
}); | ||
|
||
expect(result.current.postsQuantity).toBe(0); | ||
}); | ||
}); | ||
}); |
File renamed without changes.