Skip to content

Commit

Permalink
fix(docs): update testing doc (pmndrs#1739)
Browse files Browse the repository at this point in the history
* Update formatting

* Update testing doc

* Fix formatting

* Minor changes

* Update testing doc
  • Loading branch information
dbritto-dev authored Apr 7, 2023
1 parent f94ad23 commit 4c2c482
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion docs/getting-started/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ It may be the one state manager in the React space that gets all of these right.
You can try a live demo [here](https://codesandbox.io/s/dazzling-moon-itop4).

[zombie child problem]: https://react-redux.js.org/api/hooks#stale-props-and-zombie-children
[React concurrency]: https://github.com/bvaughn/rfcs/blob/useMutableSource/text/0000-use-mutable-source.md
[react concurrency]: https://github.com/bvaughn/rfcs/blob/useMutableSource/text/0000-use-mutable-source.md
[context loss]: https://github.com/facebook/react/issues/13332

## Installation
Expand Down
45 changes: 24 additions & 21 deletions docs/guides/testing.mdx → docs/guides/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ nav: 9

When running tests, the stores are not automatically reset before each test run.

Thus, there can be cases where the state of one test can affect another. To make sure all tests run with a pristine store state, you can mock `zustand` during testing and use the following code to create your store:
Thus, there can be cases where the state of one test can affect another. To make sure all tests run
with a pristine store state, you can mock `zustand` during testing and use the following code to
create your store:

```jsx
```js
import { create as actualCreate } from 'zustand'
// const { create: actualCreate } = jest.requireActual('zustand') // if using jest
import { act } from 'react-dom/test-utils'
Expand All @@ -34,13 +36,16 @@ beforeEach(() => {

The way you mock a dependency depends on your test runner/library.

In [jest](https://jestjs.io/), you can create a `__mocks__/zustand.js` and place the code in that file. If your app is using `zustand/vanilla` instead of `zustand`, then you'll have to place the above code in `__mocks__/zustand/vanilla.js`.
In [jest](https://jestjs.io/), you can create a `__mocks__/zustand.js` and place the code in that
file. If your app is using `zustand/vanilla` instead of `zustand`, then you'll have to place the
above code in `__mocks__/zustand/vanilla.js`.

### TypeScript usage

If you are using zustand, as documented in [TypeScript Guide](./typescript.md), use the following code:
If you are using zustand, as documented in [TypeScript Guide](./typescript.md), use the following
code:

```typescript
```ts
import { create as actualCreate, StateCreator } from 'zustand'
// if using Jest:
// import { StateCreator } from 'zustand';
Expand All @@ -51,12 +56,14 @@ import { act } from 'react-dom/test-utils'
const storeResetFns = new Set<() => void>()

// when creating a store, we get its initial state, create a reset function and add it in the set
export const create = <S>(createState: StateCreator<S>) => {
const store = actualCreate(createState)
const initialState = store.getState()
storeResetFns.add(() => store.setState(initialState, true))
return store
}
const create =
() =>
<S>(createState: StateCreator<S>) => {
const store = actualCreate(createState)
const initialState = store.getState()
storeResetFns.add(() => store.setState(initialState, true))
return store
}

// Reset all stores after each test run
beforeEach(() => {
Expand All @@ -66,7 +73,9 @@ beforeEach(() => {

## Resetting state between tests in **react-native** and **jest**

You should use the following code in the `__mocks__/zustand.js` file (the `__mocks__` directory should be adjacent to node_modules, placed in the same folder as node_modules, unless you configured roots to point to a folder other than the project root [jest docs: mocking node modules](https://jestjs.io/docs/manual-mocks#mocking-node-modules)):
You should use the following code in the `__mocks__/zustand.js` file (the `__mocks__` directory
should be adjacent to node_modules, placed in the same folder as node_modules, unless you
configured roots to point to a folder other than the project root [jest docs: mocking node modules](https://jestjs.io/docs/manual-mocks#mocking-node-modules)):

```js
import { act } from '@testing-library/react-native'
Expand All @@ -79,19 +88,13 @@ const storeResetFns = new Set()
export const create = (createState) => {
const store = actualCreate(createState)
const initialState = store.getState()
storeResetFns.add(() => {
store.setState(initialState, true)
})
storeResetFns.add(() => store.setState(initialState, true))
return store
}

// Reset all stores after each test run
beforeEach(async () => {
await act(() =>
storeResetFns.forEach((resetFn) => {
resetFn()
})
)
beforeEach(() => {
act(() => storeResetFns.forEach((resetFn) => resetFn()))
})
```

Expand Down

0 comments on commit 4c2c482

Please sign in to comment.