Gathering detailed insights and metrics for zustand-jest-utils
Gathering detailed insights and metrics for zustand-jest-utils
Gathering detailed insights and metrics for zustand-jest-utils
Gathering detailed insights and metrics for zustand-jest-utils
npm install zustand-jest-utils
Typescript
Module System
Node Version
NPM Version
62.5
Supply Chain
95.4
Quality
75.2
Maintenance
100
Vulnerability
99.6
License
JavaScript (54.44%)
TypeScript (45.56%)
Total Downloads
692
Last Day
15
Last Week
60
Last Month
242
Last Year
419
MIT License
11 Commits
1 Watchers
1 Branches
2 Contributors
Updated on Mar 23, 2023
Minified
Minified + Gzipped
Latest Version
0.1.0
Package Id
zustand-jest-utils@0.1.0
Unpacked Size
11.24 kB
Size
4.12 kB
File Count
6
NPM Version
8.19.2
Node Version
16.18.1
Published on
Mar 23, 2023
Cumulative downloads
Total Downloads
Last Day
0%
15
Compared to previous day
Last Week
20%
60
Compared to previous week
Last Month
520.5%
242
Compared to previous month
Last Year
355.4%
419
Compared to previous year
4
zustand-jest-utils
is a set of utility functions designed to simplify testing custom stores in your React applications. The package is compatible with Zustand and provides functions for creating mock stores, replacing stores, rendering components with custom stores, and resetting stores.
Install the package using npm:
npm install zustand-jest-utils
First, import the utility functions from the package:
import {
createTestStore,
createMockStore,
replaceStore,
renderWithStore,
resetStore,
} from 'zustand-jest-utils';
Use the createTestStore function to create a custom store with an initial state and custom actions:
const initialState = { count: 0 };
const customActions = (set) => ({
increment: () => set((state) => ({ count: state.count + 1 })),
});
const store = createTestStore(initialState, customActions);
Use the createMockStore function to create a mock store with an initial state, custom actions, and optional action overrides:
const initialState = { count: 0 };
const customActions = (set) => ({
increment: () => set((state) => ({ count: state.count + 1 })),
});
const overrides = { increment: () => {} };
const mockStore = createMockStore(initialState, customActions, overrides);
Use the replaceStore function to replace an old store with a new store:
const newStore = replaceStore(oldStore, mockStore);
Use the renderWithStore function to render a component with a custom store. This function internally uses the React Testing Library's render method:
import { MyComponent } from './MyComponent';
const { getByText } = renderWithStore(<MyComponent />, store);
Use the resetStore function to reset a store to its initial state:
resetStore(store, initialState);
Here's an example of how to use React Testing Store Utils in a test:
import {
createMockStore,
renderWithStore,
} from 'zustand-jest-utils';
import { fireEvent, screen } from '@testing-library/react';
import MyComponent from './MyComponent';
test('MyComponent should update the count on button click', () => {
const initialState = { count: 0 };
const customActions = (set) => ({
increment: () => set((state) => ({ count: state.count + 1 })),
});
const store = createMockStore(initialState, customActions);
renderWithStore(<MyComponent />, store);
fireEvent.click(screen.getByText('Increment'));
expect(store.getState().count).toBe(1);
});
zustand-jest-utils
is released under the MIT License.
Feel free to open issues or submit pull requests to help improve this package. Your contributions are always welcome!
No vulnerabilities found.
No security vulnerabilities found.