I have the vite mock file as per the docs, but for my use case I could not work it out how to use it properly in a test:
Simple Store – note there’s middlewares
export const useStore = create<Store>()(
devtools(
subscribeWithSelector((set) => ({
currentPage: 1,
error: null,
isLoading: false,
photos: [],
searchTerm: '',
setIsLoading: (isLoading) => set({ isLoading }),
setPhotos: (photos) => set({ photos }),
setSearchTerm: (searchTerm) => set({ searchTerm }),
setError: (error) => set({ error }),
}))
)
);
Simple example – but using the real store
test('renders when loading', () => {
useStore.setState({ isLoading: true }); // I'm wanting to substitute this for the mock store...
const component = render(<ImageGrid />);
expect(component.asFragment()).toMatchInlineSnapshot(`...`);
});
// ... because next tests will be contaminated because the state is not reset
Doing it this way will will not use the storeResetFns that’s defined in the mock.
How do I use the create function to create a mock store so I an easily set states for testing?
References:
Mocking zustand store for jest test
How to call Zustand / React hook outside of React component?
Spent hours looking online for the actual canonical answer for something so trivial.