mirror of
https://github.com/kingomarnajjar/flex-template-web.git
synced 2026-07-26 06:47:17 +10:00
Add tests to ducks
This commit is contained in:
parent
a5e051a7ee
commit
731db1991d
6 changed files with 128 additions and 14 deletions
|
|
@ -5,7 +5,7 @@ import { Page } from '../../components';
|
|||
import { addFlashNotification } from '../../ducks/FlashNotification';
|
||||
import { addFilter } from './SearchPageDucks';
|
||||
|
||||
export const SearchPage = props => (
|
||||
export const SearchPageComponent = () => (
|
||||
<PageLayout title="Search page">
|
||||
<Link to="/l/Nice+studio-in-Helsinki-345">Nice studio in Helsinki</Link>
|
||||
</PageLayout>
|
||||
|
|
@ -25,4 +25,4 @@ const mapDispatchToProps = function mapDispatchToProps(dispatch) {
|
|||
};
|
||||
};
|
||||
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(SearchPage);
|
||||
export default connect(mapStateToProps, mapDispatchToProps)(SearchPageComponent)
|
||||
|
|
|
|||
|
|
@ -1,16 +1,56 @@
|
|||
import React from 'react';
|
||||
import { BrowserRouter } from 'react-router';
|
||||
import renderer from 'react-test-renderer';
|
||||
import { SearchPage } from './SearchPage';
|
||||
import { SearchPageComponent } from './SearchPage';
|
||||
import reducer, { addFilter } from './SearchPageDucks';
|
||||
|
||||
describe('SearchPage', () => {
|
||||
describe('SearchPageComponent', () => {
|
||||
it('matches snapshot', () => {
|
||||
const component = renderer.create(
|
||||
<BrowserRouter>
|
||||
<SearchPage />
|
||||
</BrowserRouter>,
|
||||
(
|
||||
<BrowserRouter>
|
||||
<SearchPageComponent />
|
||||
</BrowserRouter>
|
||||
),
|
||||
);
|
||||
const tree = component.toJSON();
|
||||
expect(tree).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
|
||||
describe('SearchPageDucs', () => {
|
||||
const ADD_FILTER = 'ADD_FILTER';
|
||||
|
||||
describe('actions', () => {
|
||||
it('should create an action to add a filter', () => {
|
||||
const expectedAction = { type: ADD_FILTER, payload: { location: 'helsinki' } };
|
||||
const serializedExpectations = JSON.stringify(expectedAction);
|
||||
expect(JSON.stringify(addFilter('location', 'helsinki'))).toEqual(serializedExpectations);
|
||||
});
|
||||
});
|
||||
|
||||
describe('reducer', () => {
|
||||
it('should return the initial state', () => {
|
||||
const initial = reducer(undefined, {});
|
||||
expect(initial).toEqual({});
|
||||
});
|
||||
|
||||
it('should handle ADD_FILTER', () => {
|
||||
const filter1 = { location: 'helsinki' };
|
||||
const filter2 = { gears: 3 };
|
||||
const addFilter1 = { type: ADD_FILTER, payload: filter1 };
|
||||
const addFilter2 = { type: ADD_FILTER, payload: filter2 };
|
||||
const reduced = reducer([], addFilter1);
|
||||
const reducedWithInitialContent = reducer({ filters: [filter1] }, addFilter2);
|
||||
expect(reduced).toEqual({ filters: [filter1] });
|
||||
expect(reducedWithInitialContent).toEqual({ filters: [filter1, filter2] });
|
||||
});
|
||||
|
||||
it('should handle duplicates ADD_FILTER', () => {
|
||||
const filter = { location: 'helsinki' };
|
||||
const addFilter = { type: ADD_FILTER, payload: filter };
|
||||
const reducedWithInitialContent = reducer({ filters: [filter] }, addFilter);
|
||||
expect(reducedWithInitialContent).toEqual({ filters: [filter] });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -13,14 +13,14 @@ export default function reducer(state = {}, action = {}) {
|
|||
const { type, payload } = action;
|
||||
switch (type) {
|
||||
case ADD_FILTER:
|
||||
const stateFilters = state.filters || [];
|
||||
return Object.assign({}, state, { filters: unionBy(stateFilters, [ payload ]) });
|
||||
{
|
||||
const stateFilters = state.filters || [];
|
||||
return Object.assign({}, state, { filters: unionBy(stateFilters, [ payload ]) });
|
||||
}
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
// Action Creators
|
||||
export const addFilter = (key, value) => {
|
||||
return { type: ADD_FILTER, payload: { [key]: value } };
|
||||
};
|
||||
export const addFilter = (key, value) => ({ type: ADD_FILTER, payload: { [key]: value } });
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
exports[`SearchPage matches snapshot 1`] = `
|
||||
exports[`SearchPageComponent matches snapshot 1`] = `
|
||||
<div
|
||||
className="">
|
||||
<div
|
||||
|
|
|
|||
74
src/ducks/FlashNotification.test.js
Normal file
74
src/ducks/FlashNotification.test.js
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
import reducer, { addFlashNotification, removeFlashNotification } from './FlashNotification';
|
||||
|
||||
describe('FlashNotification', () => {
|
||||
const ADD_FLASH_NOTIFICATION = 'FLASH::ADD_NOTIFICATION';
|
||||
const REMOVE_FLASH_NOTIFICATION = 'FLASH::REMOVE_NOTIFICATION';
|
||||
|
||||
describe('actions', () => {
|
||||
it('should create an action to add a filter', () => {
|
||||
const content = 'Error message';
|
||||
const type = 'error';
|
||||
const expectedAction = {
|
||||
type: ADD_FLASH_NOTIFICATION,
|
||||
payload: {
|
||||
id: 'note_1',
|
||||
type,
|
||||
content,
|
||||
isRead: false,
|
||||
}
|
||||
};
|
||||
const serializedExpectations = JSON.stringify(expectedAction);
|
||||
const received = JSON.stringify(addFlashNotification(type, content));
|
||||
expect(received).toEqual(serializedExpectations);
|
||||
});
|
||||
|
||||
it('should create an action to remove a notification', () => {
|
||||
const expectedAction = {
|
||||
type: REMOVE_FLASH_NOTIFICATION,
|
||||
payload: { id: 1 },
|
||||
};
|
||||
|
||||
expect(removeFlashNotification(1)).toEqual(expectedAction);
|
||||
});
|
||||
});
|
||||
|
||||
describe('reducer', () => {
|
||||
it('should return the initial state', () => {
|
||||
const initial = reducer(undefined, {});
|
||||
expect(initial).toEqual([]);
|
||||
});
|
||||
|
||||
it('should handle ADD_FLASH_NOTIFICATION', () => {
|
||||
const flashNote1 = {
|
||||
id: 0,
|
||||
type: 'error',
|
||||
content: 'Run the tests',
|
||||
isRead: false,
|
||||
};
|
||||
const flashNote2 = {
|
||||
id: 0,
|
||||
type: 'error',
|
||||
content: 'Run the tests again',
|
||||
isRead: false,
|
||||
};
|
||||
const addFlashNote1 = { type: ADD_FLASH_NOTIFICATION, payload: flashNote1 };
|
||||
const addFlashNote2 = { type: ADD_FLASH_NOTIFICATION, payload: flashNote2 };
|
||||
const reduced = reducer([], addFlashNote1);
|
||||
const reducedWithInitialContent = reducer([flashNote1], addFlashNote2);
|
||||
expect(reduced).toEqual([flashNote1]);
|
||||
expect(reducedWithInitialContent).toEqual([flashNote1, flashNote2]);
|
||||
});
|
||||
|
||||
it('should handle duplicates ADD_FILTER', () => {
|
||||
const flashNote = {
|
||||
id: 0,
|
||||
type: 'error',
|
||||
content: 'Run the tests',
|
||||
isRead: false,
|
||||
};
|
||||
const addFlashNote = { type: ADD_FLASH_NOTIFICATION, payload: flashNote };
|
||||
const reducedWithInitialContent = reducer([flashNote], addFlashNote);
|
||||
expect(reducedWithInitialContent).toEqual([flashNote]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -6,4 +6,4 @@
|
|||
|
||||
import FlashNotification from './FlashNotification';
|
||||
|
||||
export { FlashNotification };
|
||||
export { FlashNotification }; // eslint-disable-line import/prefer-default-export
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue