Skip to content

Commit

Permalink
feat(todos): updated to run javascript unit tests, added reducer unit…
Browse files Browse the repository at this point in the history
… tests
  • Loading branch information
dcpesses committed Feb 27, 2024
1 parent 8c5c342 commit 8190b4e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/features/todos/__snapshots__/todosSlice.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`todos reducer > should handle initial state 1`] = `
[
{
"completed": false,
"id": 1708982768968,
"text": "Hit the gym",
},
{
"completed": true,
"id": 1708982943626,
"text": "Meet George",
},
]
`;
47 changes: 47 additions & 0 deletions src/features/todos/todosSlice.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* eslint-env jest */
import { vi } from 'vitest';
import todosReducer, { addTodo, deleteTodo, editTodo, toggleComplete } from './todosSlice';

describe('todos reducer', () => {
let initialState;
let date;

beforeEach(() => {
initialState = [
{ id: 1708982768968, text: 'Hit the gym', completed: false },
{ id: 1708982943626, text: 'Meet George', completed: true }
];
date = new Date(2022, 2, 22);

vi.useFakeTimers();
vi.setSystemTime(date);
});
afterEach(() => {
vi.useRealTimers();
});

test('should handle initial state', () => {
expect(todosReducer(undefined, { type: 'unknown' })).toMatchSnapshot();
});

test('should handle addTodo', () => {
const actual = todosReducer(initialState, addTodo('Newly added item message'));
expect(actual[2].text).toEqual('Newly added item message');
});

test('should handle deleteTodo', () => {
const actual = todosReducer(initialState, deleteTodo(1708982768968));
expect(actual.length).toEqual(1);
expect(actual[0].text).toBe('Meet George');
});

test('should handle editTodo', () => {
const actual = todosReducer(initialState, editTodo({id: 1708982768968, text: 'Hit the library'}));
expect(actual[0].text).toBe('Hit the library');
});

test('should handle toggleComplete', () => {
const actual = todosReducer(initialState, toggleComplete(1708982768968));
expect(actual[0].completed).toBeTruthy();
});
});
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

/* allow the incremential inclusion of JavaScript files */
"allowJs": true,
"checkJs": false,
"noImplicitAny": true,

"baseUrl": ".",
Expand Down
1 change: 0 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export default defineConfig({
'**/*.{test,spec}.?(c|m)[jt]s?(x)',
],
exclude: [
'**/*.test.js',
'**/node_modules/**',
'**/dist/**',
'**/coverage/**',
Expand Down

0 comments on commit 8190b4e

Please sign in to comment.