Skip to content

Instantly share code, notes, and snippets.

@gusgard
Created August 22, 2017 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gusgard/1d411cb1d9ec400448d33b7eb0234c1b to your computer and use it in GitHub Desktop.
Save gusgard/1d411cb1d9ec400448d33b7eb0234c1b to your computer and use it in GitHub Desktop.
Actions test
import { expect } from 'chai';
import configureMockStore from 'redux-mock-store';
import nock from 'nock';
import thunk from 'redux-thunk';
import { API_URL } from 'App/src/config/environment';
import realmStore from 'App/src/store';
import actions from '../actions';
import {
LOADING_HISTORY,
REQUEST_HISTORY_FAIL,
REQUEST_HISTORY,
} from '../constants';
import HistoryFacade from '../entities/HistoryFacade';
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
describe('request history', () => {
let store;
beforeEach(() => {
store = mockStore();
realmStore.write(() => realmStore.deleteAll());
});
afterEach(() => nock.cleanAll());
afterAll(() => realmStore.close());
const monthsReply = [
{
month: 'JUNE',
transactions: [
{
name: 'Drink xxx',
amount: 60,
date: '05/20/2016',
},
{
name: 'Drink yyy',
amount: 50,
date: '05/22/2016',
},
],
},
];
it('should handle request history correctly', async () => {
nock(`${API_URL}/transactions`)
.get()
.reply(200, monthsReply);
const expectedMonths = await HistoryFacade.requestMonths();
const expectedActions = [
{ type: LOADING_HISTORY, payload: { isLoading: true } },
{ type: REQUEST_HISTORY, payload: { months: expectedMonths } },
{ type: LOADING_HISTORY, payload: { isLoading: false } },
];
await store.dispatch(actions.requestHistory());
const storeActions = store.getActions();
expect(storeActions).to.deep.equal(expectedActions);
});
it('should handle network errors', async () => {
nock(`${API_URL}/transactions`)
.get()
.reply(500);
const expectedActions = [
{ type: LOADING_HISTORY, payload: { isLoading: true } },
{ type: REQUEST_HISTORY_FAIL },
{ type: LOADING_HISTORY, payload: { isLoading: false } },
];
await store.dispatch(actions.requestHistory());
const storeActions = store.getActions();
expect(storeActions).to.deep.equal(expectedActions);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment