Skip to content

Conversation

unadlib
Copy link
Member

@unadlib unadlib commented Apr 12, 2024

Fix: when patches options is true, its state updates should behave as expected and support React Strict Mode.

const baseState = [1];
const { result } = renderHook(() =>
  useMutative(() => baseState, { enablePatches: true })
);

expect(result.current).toHaveLength(4);

let [state, setState, patches, inversePatches] = result.current;
expect(state).toEqual([1]);
expect(patches).toEqual([]);
expect(inversePatches).toEqual([]);

act(() => {
  setState((draft) => {
    draft.push(2);
  });
  setState((draft) => {
    draft.push(3);
    draft.push(4);
  });
});

[state, setState, patches, inversePatches] = result.current;
expect(state).toEqual([1, 2, 3, 4]);
expect(apply(baseState, patches)).toEqual(state);
expect(apply(state, inversePatches)).toEqual(baseState);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant