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

create new arrays #58

Merged
merged 2 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions apps/web/pages/separate.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { temporal } from "zundo";
import create from "zustand";

interface MyState {
bears: number;
increment: () => void;
decrement: () => void;
}

const useStore = create(
temporal<MyState>((set) => ({
bears: 0,
increment: () => set((state) => ({ bears: state.bears + 1 })),
decrement: () => set((state) => ({ bears: state.bears - 1 })),
})),
);
const useTemporalStore = create(useStore.temporal);

const UndoBar = () => {
const { undo, redo, futureStates, pastStates } = useTemporalStore();
return (
<div>
past states: {JSON.stringify(pastStates)}
<br />
future states: {JSON.stringify(futureStates)}
<br />
<button onClick={() => undo()} disabled={!pastStates.length}>
undo
</button>
<button onClick={() => redo()} disabled={!futureStates.length}>
redo
</button>
</div>
);
};

const StateBar = () => {
const store = useStore();
const { bears, increment, decrement } = store;
return (
<div>
current state: {JSON.stringify(store)}
<br />
<br />
bears: {bears}
<br />
<button onClick={increment}>increment</button>
<button onClick={decrement}>decrement</button>
</div>
);
};

const App = () => {
return (
<div>
<h1>
{" "}
<span role="img" aria-label="bear">
🐻
</span>{" "}
<span role="img" aria-label="recycle">
♻️
</span>{" "}
Zundo!
</h1>
<StateBar />
<br />
<UndoBar />
</div>
);
};

export default App;
28 changes: 14 additions & 14 deletions packages/zundo/__tests__/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ describe('Middleware options', () => {
store.getState().increment();
store.getState().increment();
});
expect(pastStates.length).toBe(2);
expect(pastStates[0]).toEqual({
expect(store.temporal.getState().pastStates.length).toBe(2);
expect(store.temporal.getState().pastStates[0]).toEqual({
count: 0,
count2: 0,
increment: expect.any(Function),
decrement: expect.any(Function),
doNothing: expect.any(Function),
});
expect(pastStates[1]).toEqual({
expect(store.temporal.getState().pastStates[1]).toEqual({
count: 1,
count2: 1,
increment: expect.any(Function),
Expand All @@ -95,17 +95,17 @@ describe('Middleware options', () => {
});
const { pastStates, futureStates } =
storeWithPartialize.temporal.getState();
expect(pastStates.length).toBe(0);
expect(futureStates.length).toBe(0);
expect(storeWithPartialize.temporal.getState().pastStates.length).toBe(0);
expect(storeWithPartialize.temporal.getState().futureStates.length).toBe(0);
act(() => {
storeWithPartialize.getState().increment();
storeWithPartialize.getState().increment();
});
expect(pastStates.length).toBe(2);
expect(pastStates[0]).toEqual({
expect(storeWithPartialize.temporal.getState().pastStates.length).toBe(2);
expect(storeWithPartialize.temporal.getState().pastStates[0]).toEqual({
count: 0,
});
expect(pastStates[1]).toEqual({
expect(storeWithPartialize.temporal.getState().pastStates[1]).toEqual({
count: 1,
});
expect(storeWithPartialize.getState()).toContain({ count: 2, count2: 2 });
Expand All @@ -119,16 +119,16 @@ describe('Middleware options', () => {
});
const { undo, pastStates, futureStates } =
storeWithPartialize.temporal.getState();
expect(pastStates.length).toBe(0);
expect(futureStates.length).toBe(0);
expect(storeWithPartialize.temporal.getState().pastStates.length).toBe(0);
expect(storeWithPartialize.temporal.getState().futureStates.length).toBe(0);

act(() => {
storeWithPartialize.getState().increment();
storeWithPartialize.getState().increment();
undo();
});
expect(futureStates.length).toBe(1);
expect(futureStates[0]).toEqual({
expect(storeWithPartialize.temporal.getState().futureStates.length).toBe(1);
expect(storeWithPartialize.temporal.getState().futureStates[0]).toEqual({
count: 2,
});
expect(storeWithPartialize.getState()).toEqual({
Expand All @@ -141,8 +141,8 @@ describe('Middleware options', () => {
act(() => {
undo();
});
expect(futureStates.length).toBe(2);
expect(futureStates[1]).toEqual({
expect(storeWithPartialize.temporal.getState().futureStates.length).toBe(2);
expect(storeWithPartialize.temporal.getState().futureStates[1]).toEqual({
count: 1,
});
expect(storeWithPartialize.getState()).toEqual({
Expand Down
92 changes: 46 additions & 46 deletions packages/zundo/__tests__/zundo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,16 @@ describe('temporal middleware', () => {
store.getState().increment();
store.getState().increment();
});
expect(pastStates.length).toBe(3);
expect(store.temporal.getState().pastStates.length).toBe(3);
act(() => {
undo(2);
});
expect(pastStates.length).toBe(1);
expect(store.temporal.getState().pastStates.length).toBe(1);
expect(store.getState().count).toBe(1);
act(() => {
undo();
});
expect(pastStates.length).toBe(0);
expect(store.temporal.getState().pastStates.length).toBe(0);
expect(store.getState().count).toBe(0);
});
});
Expand Down Expand Up @@ -133,17 +133,17 @@ describe('temporal middleware', () => {
store.getState().increment();
store.getState().increment();
});
expect(pastStates.length).toBe(3);
expect(store.temporal.getState().pastStates.length).toBe(3);
act(() => {
undo(2);
});
expect(pastStates.length).toBe(1);
expect(store.temporal.getState().pastStates.length).toBe(1);
expect(store.getState().count).toBe(1);
expect(futureStates.length).toBe(2);
expect(store.temporal.getState().futureStates.length).toBe(2);
act(() => {
redo(2);
});
expect(pastStates.length).toBe(3);
expect(store.temporal.getState().pastStates.length).toBe(3);
expect(store.getState().count).toBe(3);
});
});
Expand All @@ -155,56 +155,56 @@ describe('temporal middleware', () => {
act(() => {
store.getState().increment();
});
expect(pastStates.length).toBe(1);
expect(store.temporal.getState().pastStates.length).toBe(1);
act(() => {
store.getState().increment();
store.getState().decrement();
});
expect(pastStates.length).toBe(3);
expect(store.temporal.getState().pastStates.length).toBe(3);
act(() => {
undo(2);
});
expect(pastStates.length).toBe(1);
expect(futureStates.length).toBe(2);
expect(store.temporal.getState().pastStates.length).toBe(1);
expect(store.temporal.getState().futureStates.length).toBe(2);
act(() => {
redo();
});
expect(pastStates.length).toBe(2);
expect(futureStates.length).toBe(1);
expect(store.temporal.getState().pastStates.length).toBe(2);
expect(store.temporal.getState().futureStates.length).toBe(1);
act(() => {
clear();
});
expect(pastStates.length).toBe(0);
expect(futureStates.length).toBe(0);
expect(store.temporal.getState().pastStates.length).toBe(0);
expect(store.temporal.getState().futureStates.length).toBe(0);
});

it('should update pastStates', () => {
const { undo, redo, clear, pastStates } = store.temporal.getState();
expect(pastStates.length).toBe(0);
expect(store.temporal.getState().pastStates.length).toBe(0);
act(() => {
store.getState().increment();
});
expect(pastStates.length).toBe(1);
expect(store.temporal.getState().pastStates.length).toBe(1);
act(() => {
store.getState().decrement();
});
expect(pastStates.length).toBe(2);
expect(store.temporal.getState().pastStates.length).toBe(2);
act(() => {
undo();
});
expect(pastStates.length).toBe(1);
expect(store.temporal.getState().pastStates.length).toBe(1);
act(() => {
undo();
});
expect(pastStates.length).toBe(0);
expect(store.temporal.getState().pastStates.length).toBe(0);
act(() => {
redo();
});
expect(pastStates.length).toBe(1);
expect(store.temporal.getState().pastStates.length).toBe(1);
act(() => {
clear();
});
expect(pastStates.length).toBe(0);
expect(store.temporal.getState().pastStates.length).toBe(0);
});

it('should update futureStates', () => {
Expand All @@ -213,24 +213,24 @@ describe('temporal middleware', () => {
act(() => {
store.getState().increment();
});
expect(futureStates.length).toBe(0);
expect(store.temporal.getState().futureStates.length).toBe(0);
act(() => {
store.getState().increment();
store.getState().decrement();
});
expect(futureStates.length).toBe(0);
expect(store.temporal.getState().futureStates.length).toBe(0);
act(() => {
undo(2);
});
expect(futureStates.length).toBe(2);
expect(store.temporal.getState().futureStates.length).toBe(2);
act(() => {
redo();
});
expect(futureStates.length).toBe(1);
expect(store.temporal.getState().futureStates.length).toBe(1);
act(() => {
clear();
});
expect(futureStates.length).toBe(0);
expect(store.temporal.getState().futureStates.length).toBe(0);
});

it('properly tracks state values after clearing', () => {
Expand All @@ -242,40 +242,40 @@ describe('temporal middleware', () => {
store.getState().increment();
store.getState().increment();
});
expect(pastStates.length).toBe(3);
expect(store.temporal.getState().pastStates.length).toBe(3);
act(() => {
clear();
});
expect(pastStates.length).toBe(0);
expect(futureStates.length).toBe(0);
expect(pastStates).toEqual([]);
expect(futureStates).toEqual([]);
expect(store.temporal.getState().pastStates.length).toBe(0);
expect(store.temporal.getState().futureStates.length).toBe(0);
expect(store.temporal.getState().pastStates).toEqual([]);
expect(store.temporal.getState().futureStates).toEqual([]);
expect(store.getState().count).toBe(3);
act(() => {
store.getState().increment();
});
expect(pastStates.length).toBe(1);
expect(store.temporal.getState().pastStates.length).toBe(1);
expect(store.getState().count).toBe(4);
act(() => {
store.getState().increment();
store.getState().increment();
store.getState().increment();
});
expect(pastStates.length).toBe(4);
expect(store.temporal.getState().pastStates.length).toBe(4);
expect(store.getState().count).toBe(7);
act(() => {
undo(3);
});
expect(pastStates.length).toBe(1);
expect(store.temporal.getState().pastStates.length).toBe(1);
expect(store.getState().count).toBe(4);
expect(futureStates.length).toBe(3);
expect(store.temporal.getState().futureStates.length).toBe(3);
act(() => {
clear();
});
expect(pastStates.length).toBe(0);
expect(futureStates.length).toBe(0);
expect(pastStates).toEqual([]);
expect(futureStates).toEqual([]);
expect(store.temporal.getState().pastStates.length).toBe(0);
expect(store.temporal.getState().futureStates.length).toBe(0);
expect(store.temporal.getState().pastStates).toEqual([]);
expect(store.temporal.getState().futureStates).toEqual([]);
expect(store.getState().count).toBe(4);
});

Expand All @@ -289,20 +289,20 @@ describe('temporal middleware', () => {
store.getState().increment();
store.getState().increment();
});
expect(pastStates.length).toBe(3);
expect(futureStates.length).toBe(0);
expect(store.temporal.getState().pastStates.length).toBe(3);
expect(store.temporal.getState().futureStates.length).toBe(0);
act(() => {
undo(2);
});
expect(pastStates.length).toBe(1);
expect(futureStates.length).toBe(2);
expect(store.temporal.getState().pastStates.length).toBe(1);
expect(store.temporal.getState().futureStates.length).toBe(2);
act(() => {
store.getState().increment();
store.getState().increment();
store.getState().increment();
});
expect(pastStates.length).toBe(4);
expect(futureStates.length).toBe(0);
expect(store.temporal.getState().pastStates.length).toBe(4);
expect(store.temporal.getState().futureStates.length).toBe(0);
});

describe('temporal tracking status', () => {
Expand Down
Loading