Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(updater): fix multiple updater in react with patches mode #8

Merged
merged 1 commit into from
Apr 12, 2024

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);

@unadlib unadlib merged commit 31a3547 into main Apr 12, 2024
6 checks passed
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