Skip to content

Commit

Permalink
Add a basic unit test for demo example
Browse files Browse the repository at this point in the history
  • Loading branch information
SawyerHood committed Jun 24, 2020
1 parent 8ff726f commit 7dcb946
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"deno.enable": false
}
98 changes: 92 additions & 6 deletions src/index.test.tsx
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);
});
});
1 change: 1 addition & 0 deletions src/react-app-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="react-scripts" />
25 changes: 21 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"compilerOptions": {
"outDir": "dist",
"module": "esnext",
"lib": ["dom", "esnext"],
"lib": [
"dom",
"esnext"
],
"moduleResolution": "node",
"jsx": "react",
"sourceMap": true,
Expand All @@ -16,8 +19,22 @@
"noUnusedLocals": true,
"noUnusedParameters": true,
"allowSyntheticDefaultImports": true,
"downlevelIteration": true
"downlevelIteration": true,
"target": "es5",
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true
},
"include": ["src"],
"exclude": ["node_modules", "dist", "example"]
"include": [
"src"
],
"exclude": [
"node_modules",
"dist",
"example"
]
}

0 comments on commit 7dcb946

Please sign in to comment.