Skip to content

Commit

Permalink
write actions tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Easybuoy committed Nov 10, 2019
1 parent 8a1e833 commit 38e38c3
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 61 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"react-scripts": "3.2.0",
"react-toastify": "^5.4.0",
"redux": "^4.0.4",
"redux-mock-store": "^1.5.3",
"redux-thunk": "^2.3.0",
"styled-components": "^4.4.1"
},
Expand Down Expand Up @@ -59,6 +60,7 @@
},
"devDependencies": {
"enzyme-to-json": "^3.4.3",
"moxios": "^0.4.0",
"sinon": "^7.5.0"
}
}
34 changes: 34 additions & 0 deletions src/actions/__snapshots__/actions.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Actions dispatches LOADING when getCharacter request is successful 1`] = `
Array [
Object {
"type": "LOADING",
},
]
`;

exports[`Actions dispatches LOADING when getCharacter request is successful 2`] = `
Array [
Object {
"payload": Array [],
"type": "SELECT_MOVIE",
},
]
`;

exports[`Actions dispatches LOADING when getMovies request is successful 1`] = `
Array [
Object {
"type": "LOADING",
},
]
`;

exports[`Actions dispatches LOADING when getmovie request is successful 1`] = `
Array [
Object {
"type": "LOADING",
},
]
`;
146 changes: 85 additions & 61 deletions src/actions/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,97 @@ import configureStore from 'redux-mock-store';
import moxios from 'moxios';
import thunk from 'redux-thunk';

import { } from './index';
import { } from '../../src/actions/types';
import { getMovies, getMovie, getCharacter, selectMovie } from './index';
import { LOADING, SELECT_MOVIE } from './types';
import mock from '../__mocks__/mock';

describe('authActions', () => {
const mockStore = configureStore([thunk]);
describe('Actions', () => {
const mockStore = configureStore([thunk]);
const store = mockStore({});

beforeEach(() => {
moxios.install();
});
afterEach(() => {
moxios.uninstall();
beforeEach(() => {
moxios.install();
store.clearActions();
});
afterEach(() => {
moxios.uninstall();
});

it(`dispatches LOADING when getMovies request is successful`, done => {
moxios.wait(() => {
const request = moxios.requests.mostRecent();
request.respondWith({
status: 200,
response: { data: mock.setCurrentUserMock }
});
it('should set the current user', () => {
const data = { email: 'ezekiel', password: 1234};
const response = setCurrentUser(data);
expect(response.type).toEqual(SET_CURRENT_USER);
expect(response.payload).toEqual(data);
});
const expectedActions = [
{
type: LOADING
}
];

// it(`dispatches SET_ERRORS and SET_CURRENT_USER when request is successful`, (done) => {
// moxios.wait(() => {
// const request = moxios.requests.mostRecent();
// request.respondWith({
// status: 200,
// response: {data: mock.setCurrentUserMock},
// });
// });
// const expectedActions = [
// {
// type: SET_ERRORS,
// },
// {
// type: SET_CURRENT_USER,
// payload: {"exp": 1553899374, "iat": 1553895774, "id": "3e26f411-45f4-40b8-8018-099c99a89aa1", "type": 1, "userImage": "assets/uploads/users/default-avatar.png"}
// }
// ];
// const store = mockStore({});
// return store.dispatch(signIn({email: '', password: '1234'})).then(() => {
// expect(store.getActions()).toEqual(expectedActions);
// done();
// });
// });
store.dispatch(getMovies());
expect(store.getActions()).toEqual(expectedActions);
expect(store.getActions()).toMatchSnapshot();
done();
});

// it(`dispatches GET_ERRORS when request fails`, (done) => {
// moxios.wait(() => {
// const request = moxios.requests.mostRecent();
// request.respondWith({
// status: 400,
// response: {data: mock.getErrorsMock},
// });
// });
// const expectedActions = [
// {
// type: GET_ERRORS,
// payload: mock.getErrorsMock
// },
// {
// type: SET_ERRORS,
// }
// ];
// const store = mockStore({});
// return store.dispatch(signIn()).then(() => {
// expect(store.getActions()).toEqual(expectedActions);
// done();
// });
// });
it(`dispatches LOADING when getmovie request is successful`, done => {
moxios.wait(() => {
const request = moxios.requests.mostRecent();
request.respondWith({
status: 200,
response: { data: mock.setCurrentUserMock }
});
});
const expectedActions = [
{
type: LOADING
}
];
store.dispatch(getMovie('google.com'));
expect(store.getActions()).toEqual(expectedActions);
expect(store.getActions()).toMatchSnapshot();
done();
});


it(`dispatches LOADING when getCharacter request is successful`, done => {
moxios.wait(() => {
const request = moxios.requests.mostRecent();
request.respondWith({
status: 200,
response: { data: mock.setCurrentUserMock }
});
});
const expectedActions = [
{
type: LOADING
}
];
store.dispatch(getCharacter([]));
expect(store.getActions()).toEqual(expectedActions);
expect(store.getActions()).toMatchSnapshot();
done();
});

it(`dispatches LOADING when getCharacter request is successful`, done => {
moxios.wait(() => {
const request = moxios.requests.mostRecent();
request.respondWith({
status: 200,
response: { data: mock.setCurrentUserMock }
});
});
const expectedActions = [
{
type: SELECT_MOVIE,
payload: []
}
];
store.dispatch(selectMovie([]));
expect(store.getActions()).toEqual(expectedActions);
expect(store.getActions()).toMatchSnapshot();
done();
});
});

0 comments on commit 38e38c3

Please sign in to comment.