Skip to content

Commit

Permalink
Add tests for redux actions (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimbo authored Sep 24, 2018
1 parent b92edb8 commit 2d4189c
Show file tree
Hide file tree
Showing 35 changed files with 1,106 additions and 189 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { createElement } from 'react';

export const mockRequest = jest.fn();

export const RestApi = {
Magento2: {
request: mockRequest
}
};

/**
* the Price component from @magento/peregrine
* has browser-specific functionality and cannot
Expand Down
7 changes: 7 additions & 0 deletions packages/venia-concept/src/__mocks__/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const initialState = {};

export const addReducer = jest.fn();
export const dispatch = jest.fn();
export const getState = jest.fn(() => initialState);

export default { addReducer, dispatch, getState };
30 changes: 0 additions & 30 deletions packages/venia-concept/src/actions/app.js

This file was deleted.

20 changes: 20 additions & 0 deletions packages/venia-concept/src/actions/app/__tests__/actions.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import actions from '../actions';

const payload = 'FOO';
const error = new Error('BAR');

test('toggleDrawer.toString() returns the proper action type', () => {
expect(actions.toggleDrawer.toString()).toBe('APP/TOGGLE_DRAWER');
});

test('toggleDrawer() returns a proper action object', () => {
expect(actions.toggleDrawer(payload)).toEqual({
type: 'APP/TOGGLE_DRAWER',
payload
});
expect(actions.toggleDrawer(error)).toEqual({
type: 'APP/TOGGLE_DRAWER',
payload: error,
error: true
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { dispatch, getState } from 'src/store';
import actions from '../actions';
import { closeDrawer, toggleDrawer } from '../asyncActions';

jest.mock('src/store');

const thunkArgs = [dispatch, getState];

afterEach(() => {
dispatch.mockClear();
});

test('toggleDrawer() to return a thunk', () => {
expect(toggleDrawer()).toBeInstanceOf(Function);
});

test('toggleDrawer thunk returns undefined', async () => {
const payload = 'FOO';
const result = await toggleDrawer(payload)(...thunkArgs);

expect(result).toBeUndefined();
});

test('toggleDrawer thunk dispatches actions', async () => {
const payload = 'FOO';
await toggleDrawer(payload)(...thunkArgs);

expect(dispatch).toHaveBeenCalledWith(actions.toggleDrawer(payload));
expect(dispatch).toHaveBeenCalledTimes(1);
});

test('closeDrawer() to return a thunk ', () => {
expect(closeDrawer()).toBeInstanceOf(Function);
});

test('closeDrawer thunk returns undefined', async () => {
const result = await closeDrawer()(...thunkArgs);

expect(result).toBeUndefined();
});

test('closeDrawer thunk dispatches actions', async () => {
await closeDrawer()(...thunkArgs);

expect(dispatch).toHaveBeenCalledWith(actions.toggleDrawer(null));
expect(dispatch).toHaveBeenCalledTimes(1);
});
6 changes: 6 additions & 0 deletions packages/venia-concept/src/actions/app/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createActions } from 'redux-actions';

const prefix = 'APP';
const actionTypes = ['TOGGLE_DRAWER'];

export default createActions(...actionTypes, { prefix });
7 changes: 7 additions & 0 deletions packages/venia-concept/src/actions/app/asyncActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import actions from './actions';

export const toggleDrawer = name => async dispatch =>
dispatch(actions.toggleDrawer(name));

export const closeDrawer = () => async dispatch =>
dispatch(actions.toggleDrawer(null));
2 changes: 2 additions & 0 deletions packages/venia-concept/src/actions/app/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default } from './actions';
export * from './asyncActions';
93 changes: 93 additions & 0 deletions packages/venia-concept/src/actions/cart/__tests__/actions.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import actions from '../actions';

const payload = 'PAYLOAD';
const error = new Error('ERROR');

test('addItem.request.toString() returns the proper action type', () => {
expect(actions.addItem.request.toString()).toBe('CART/ADD_ITEM/REQUEST');
});

test('addItem.request() returns a proper action object', () => {
expect(actions.addItem.request(payload)).toEqual({
type: 'CART/ADD_ITEM/REQUEST',
payload
});
});

test('addItem.receive.toString() returns the proper action type', () => {
expect(actions.addItem.receive.toString()).toBe('CART/ADD_ITEM/RECEIVE');
});

test('addItem.receive() returns a proper action object', () => {
expect(actions.addItem.receive(payload)).toEqual({
type: 'CART/ADD_ITEM/RECEIVE',
payload
});
expect(actions.addItem.receive(error)).toEqual({
type: 'CART/ADD_ITEM/RECEIVE',
payload: error,
error: true
});
});

test('getGuestCart.request.toString() returns the proper action type', () => {
expect(actions.getGuestCart.request.toString()).toBe(
'CART/GET_GUEST_CART/REQUEST'
);
});

test('getGuestCart.request() returns a proper action object', () => {
expect(actions.getGuestCart.request(payload)).toEqual({
type: 'CART/GET_GUEST_CART/REQUEST',
payload
});
});

test('getGuestCart.receive.toString() returns the proper action type', () => {
expect(actions.getGuestCart.receive.toString()).toBe(
'CART/GET_GUEST_CART/RECEIVE'
);
});

test('getGuestCart.receive() returns a proper action object', () => {
expect(actions.getGuestCart.receive(payload)).toEqual({
type: 'CART/GET_GUEST_CART/RECEIVE',
payload
});
expect(actions.getGuestCart.receive(error)).toEqual({
type: 'CART/GET_GUEST_CART/RECEIVE',
payload: error,
error: true
});
});

test('getDetails.request.toString() returns the proper action type', () => {
expect(actions.getDetails.request.toString()).toBe(
'CART/GET_DETAILS/REQUEST'
);
});

test('getDetails.request() returns a proper action object', () => {
expect(actions.getDetails.request(payload)).toEqual({
type: 'CART/GET_DETAILS/REQUEST',
payload
});
});

test('getDetails.receive.toString() returns the proper action type', () => {
expect(actions.getDetails.receive.toString()).toBe(
'CART/GET_DETAILS/RECEIVE'
);
});

test('getDetails.receive() returns a proper action object', () => {
expect(actions.getDetails.receive(payload)).toEqual({
type: 'CART/GET_DETAILS/RECEIVE',
payload
});
expect(actions.getDetails.receive(error)).toEqual({
type: 'CART/GET_DETAILS/RECEIVE',
payload: error,
error: true
});
});
Loading

0 comments on commit 2d4189c

Please sign in to comment.