From 4674909ecbc3136d016065d37df39207e0b1e56a Mon Sep 17 00:00:00 2001 From: George Lima Date: Wed, 28 Nov 2018 13:12:42 -0300 Subject: [PATCH] feature: add sample tests --- __tests__/actions/Todo.test.js | 28 ++++++++ __tests__/components/Sidebar.test.js | 16 +++++ .../__snapshots__/Sidebar.test.js.snap | 69 +++++++++++++++++++ __tests__/reducers/Todo.test.js | 26 +++++++ 4 files changed, 139 insertions(+) create mode 100644 __tests__/actions/Todo.test.js create mode 100644 __tests__/components/Sidebar.test.js create mode 100644 __tests__/components/__snapshots__/Sidebar.test.js.snap create mode 100644 __tests__/reducers/Todo.test.js diff --git a/__tests__/actions/Todo.test.js b/__tests__/actions/Todo.test.js new file mode 100644 index 0000000..d89158f --- /dev/null +++ b/__tests__/actions/Todo.test.js @@ -0,0 +1,28 @@ +import configureStore from 'redux-mock-store'; + +import { ADD_TODO } from '../../app/constants/actions'; + +import { addTodo } from '../../app/actions/add-todo'; + +const store = configureStore()(); + +describe('Todo Actions', () => { + beforeEach(() => store.clearActions()); + + test('should create an action to add a new todo', () => { + const text = 'Hello World!'; + + store.dispatch(addTodo(text)); + expect(store.getActions()[0]).toEqual( + expect.objectContaining({ + type: ADD_TODO, + payload: { + text, + id: expect.any(String), + editing: false, + createdAt: expect.any(Number), + }, + }), + ); + }); +}); diff --git a/__tests__/components/Sidebar.test.js b/__tests__/components/Sidebar.test.js new file mode 100644 index 0000000..f2da9ae --- /dev/null +++ b/__tests__/components/Sidebar.test.js @@ -0,0 +1,16 @@ +import React from 'react'; +import { shallow } from 'enzyme'; +import toJson from 'enzyme-to-json'; + +import { SidebarComponent } from '../../app/components/sidebar/index'; + +describe('', () => { + describe('render()', () => { + test('should render correctly', () => { + const wrapper = shallow(); + const component = wrapper.dive(); + + expect(toJson(component)).toMatchSnapshot(); + }); + }); +}); diff --git a/__tests__/components/__snapshots__/Sidebar.test.js.snap b/__tests__/components/__snapshots__/Sidebar.test.js.snap new file mode 100644 index 0000000..43f068d --- /dev/null +++ b/__tests__/components/__snapshots__/Sidebar.test.js.snap @@ -0,0 +1,69 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[` render() should render correctly 1`] = ` + + + Dashboard + + + Send + + + Receive + + + Settings + + +`; diff --git a/__tests__/reducers/Todo.test.js b/__tests__/reducers/Todo.test.js new file mode 100644 index 0000000..3ff57d1 --- /dev/null +++ b/__tests__/reducers/Todo.test.js @@ -0,0 +1,26 @@ +import todoReducer from '../../app/reducers/todo'; +import { ADD_TODO } from '../../app/constants/actions'; + +describe('Todo Reducer', () => { + test('should return the valid initial state', () => { + const action = { type: 'UNKNOWN_ACTION' }; + const initialState = []; + + expect(todoReducer(undefined, action)).toEqual(initialState); + }); + + test('should add a new todo', () => { + const action = { + type: ADD_TODO, + payload: { + id: 'abc123', + text: 'Hello World!', + editing: false, + createdAt: new Date().getTime(), + }, + }; + const expectedState = [action.payload]; + + expect(todoReducer(undefined, action)).toEqual(expectedState); + }); +});