forked from SawyerHood/recoil-undo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a basic unit test for demo example
- Loading branch information
1 parent
8ff726f
commit 7dcb946
Showing
4 changed files
with
117 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"deno.enable": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,93 @@ | ||
import { ExampleComponent } from '.' | ||
import React from 'react'; | ||
import { render, fireEvent } from '@testing-library/react'; | ||
import '@testing-library/jest-dom/extend-expect'; | ||
import { useUndo, useRedo, RecoilUndoRoot } from './index'; | ||
import { | ||
RecoilRoot, | ||
atom, | ||
selector, | ||
useRecoilState, | ||
useRecoilValue, | ||
} from 'recoil'; | ||
|
||
describe('ExampleComponent', () => { | ||
it('is truthy', () => { | ||
expect(ExampleComponent).toBeTruthy() | ||
}) | ||
}) | ||
const COUNT = atom({ | ||
default: 0, | ||
key: 'count', | ||
}); | ||
|
||
const TWO_TIMES = selector({ | ||
get: ({ get }) => get(COUNT) * 2, | ||
key: 'two_times', | ||
}); | ||
|
||
const App = () => { | ||
return ( | ||
<RecoilRoot> | ||
<RecoilUndoRoot trackedAtoms={[COUNT]}> | ||
<Counter /> | ||
</RecoilUndoRoot> | ||
</RecoilRoot> | ||
); | ||
}; | ||
|
||
function Counter() { | ||
const [count, setCount] = useRecoilState(COUNT); | ||
const double = useRecoilValue(TWO_TIMES); | ||
const undo = useUndo(); | ||
const redo = useRedo(); | ||
return ( | ||
<div> | ||
<div> | ||
<button | ||
data-testid='dec' | ||
onClick={() => setCount((count) => count - 1)} | ||
> | ||
- | ||
</button> | ||
<span data-testid='count'>{count}</span> | ||
<button | ||
data-testid='inc' | ||
onClick={() => setCount((count) => count + 1)} | ||
> | ||
+ | ||
</button> | ||
</div> | ||
<button data-testid='undo' onClick={undo}> | ||
Undo | ||
</button> | ||
<button data-testid='redo' onClick={redo}> | ||
Redo | ||
</button> | ||
<div>{double}</div> | ||
</div> | ||
); | ||
} | ||
|
||
describe('recoil-undo', () => { | ||
it('handles a simple undo and redo case', () => { | ||
const { getByTestId } = render(<App />); | ||
const inc = getByTestId('inc'); | ||
const dec = getByTestId('dec'); | ||
const count = getByTestId('count'); | ||
const undoButton = getByTestId('undo'); | ||
const redoButton = getByTestId('redo'); | ||
|
||
const plus = () => fireEvent.click(inc); | ||
const minus = () => fireEvent.click(dec); | ||
const getCount = () => Number(count.textContent); | ||
const undo = () => fireEvent.click(undoButton); | ||
const redo = () => fireEvent.click(redoButton); | ||
|
||
plus(); | ||
expect(getCount()).toBe(1); | ||
|
||
plus(); | ||
plus(); | ||
minus(); | ||
expect(getCount()).toBe(2); | ||
undo(); | ||
expect(getCount()).toBe(3); | ||
redo(); | ||
expect(getCount()).toBe(2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/// <reference types="react-scripts" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters