├── .eslintrc ├── .gitignore ├── .npmignore ├── .travis.yml ├── license ├── package.json ├── readme.md ├── src ├── __tests__ │ └── indexSpec.ts ├── index.ts └── utils │ ├── DispatchMock.ts │ ├── StoreMock.ts │ └── __tests__ │ ├── DispatchMockSpec.ts │ └── StoreMockSpec.ts ├── tsconfig.json └── yarn.lock /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "browser": true, 4 | "jest": true 5 | }, 6 | "extends": "plugin:@typescript-eslint/recommended", 7 | "parser": "@typescript-eslint/parser" 8 | } 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist 3 | .idea 4 | npm-debug.log -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | src 2 | 3 | .eslintrc 4 | 5 | .gitignore 6 | 7 | .npmignore 8 | 9 | .idea 10 | 11 | wallaby.js 12 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '12' 4 | - '10' 5 | - '8' 6 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Dennis Axelsson (github.com/knegusen) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "redux-test-utils", 3 | "version": "1.0.2", 4 | "main": "dist/index.js", 5 | "files": [ 6 | "dist" 7 | ], 8 | "description": "Test utils to simplify mocking for redux.", 9 | "engines": { 10 | "node": ">=8" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/Knegusen/redux-test-utils.git" 15 | }, 16 | "keywords": [ 17 | "dispatch", 18 | "redux", 19 | "store", 20 | "test" 21 | ], 22 | "types": "./dist/index.d.ts", 23 | "author": "Dennis Axelsson ", 24 | "dependencies": { 25 | "fast-deep-equal": "^2.0.1" 26 | }, 27 | "peerDependencies": { 28 | "redux": "^4.0.0" 29 | }, 30 | "devDependencies": { 31 | "@babel/core": "^7.4.5", 32 | "@babel/preset-env": "^7.4.5", 33 | "@babel/preset-typescript": "^7.6.0", 34 | "@types/jest": "^24.0.20", 35 | "@typescript-eslint/eslint-plugin": "^2.5.0", 36 | "@typescript-eslint/parser": "^2.5.0", 37 | "enzyme": "^3.10.0", 38 | "eslint": "^6.6.0", 39 | "jest": "^24.8.0", 40 | "np": "^5.0.3", 41 | "prettier": "^1.18.2", 42 | "redux": "^4.0.4", 43 | "rimraf": "^2.6.3", 44 | "typescript": "^3.6.4" 45 | }, 46 | "scripts": { 47 | "clean": "rimraf dist", 48 | "build-js": "yarn clean && babel src --out-dir dist --ignore **/__tests__/**", 49 | "build": "yarn clean && yarn tsc", 50 | "prepublish": "yarn build", 51 | "lint": "eslint './src/**/*.ts*'", 52 | "lint-fix": "eslint './src/**/*.ts*' --fix", 53 | "test": "yarn lint && jest" 54 | }, 55 | "licenses": [ 56 | { 57 | "type": "MIT" 58 | } 59 | ], 60 | "babel": { 61 | "presets": [ 62 | "@babel/preset-env", 63 | "@babel/preset-typescript" 64 | ] 65 | }, 66 | "prettier": { 67 | "printWidth": 100, 68 | "singleQuote": true, 69 | "trailingComma": "all" 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # redux-test-utils [![Build Status](https://travis-ci.org/knegusen/redux-test-utils.svg?branch=master)](https://travis-ci.org/knegusen/redux-test-utils) 2 | 3 | Test utils to simplify testing of containers in redux. 4 | 5 | ## Install 6 | 7 | In the terminal execute the following command: 8 | 9 | ``` 10 | $ npm install redux-test-utils --save-dev 11 | ``` 12 | 13 | ## How to use 14 | 15 | ### createMockStore 16 | 17 | ```js 18 | 19 | import { createMockStore } from 'redux-test-utils'; 20 | 21 | describe('example', () => { 22 | it('works', () => { 23 | const state = 'state'; 24 | const store = createMockStore(state); 25 | const action = { 26 | type: 'type', 27 | data: 'data' 28 | }; 29 | store.dispatch(action); 30 | 31 | expect(store.getAction(action.type)).toEqual(action); 32 | expect(store.getActions()).toEqual([action]); 33 | expect(store.isActionDispatched(action)).toBe(true); 34 | expect(store.isActionTypeDispatched(action.type)).toBe(true); 35 | expect(store.getState()).toBe(state); 36 | }); 37 | }); 38 | 39 | ``` 40 | 41 | ### createMockDispatch 42 | 43 | ```js 44 | 45 | import { createMockDispatch } from 'redux-test-utils'; 46 | 47 | describe('example', () => { 48 | it('works', () => { 49 | const state = 'state'; 50 | const dispatchMock = createMockDispatch(); 51 | const action = { 52 | type: 'type', 53 | data: 'data', 54 | }; 55 | dispatchMock.dispatch(action); 56 | 57 | expect(dispatchMock.getAction(action.type)).toEqual(action); 58 | expect(dispatchMock.getActions()).toEqual([action]); 59 | expect(dispatchMock.isActionDispatched(action)).toBe(true); 60 | expect(dispatchMock.isActionTypeDispatched(action.type)).toBe(true); 61 | }); 62 | }); 63 | 64 | ``` -------------------------------------------------------------------------------- /src/__tests__/indexSpec.ts: -------------------------------------------------------------------------------- 1 | import { createMockStore, createMockDispatch } from '../index'; 2 | import * as StoreMock from '../utils/StoreMock'; 3 | import * as DispatchMock from '../utils/DispatchMock'; 4 | 5 | describe('createStore', () => { 6 | it('works', () => { 7 | spyOn(StoreMock, 'createMockStore'); 8 | const state = 'state'; 9 | createMockStore(state); 10 | expect(StoreMock.createMockStore).toHaveBeenCalledWith(state); 11 | }); 12 | 13 | describe('example', () => { 14 | it('works', () => { 15 | const state = 'state'; 16 | const store = createMockStore(state); 17 | const action = { 18 | type: 'type', 19 | data: 'data', 20 | }; 21 | store.dispatch(action); 22 | 23 | expect(store.getAction(action.type)).toEqual(action); 24 | expect(store.getActions()).toEqual([action]); 25 | expect(store.isActionDispatched(action)).toBe(true); 26 | expect(store.isActionTypeDispatched(action.type)).toBe(true); 27 | expect(store.getState()).toBe(state); 28 | }); 29 | }); 30 | }); 31 | 32 | describe('createMockDispatch', () => { 33 | it('works', () => { 34 | spyOn(DispatchMock, 'createMockDispatch'); 35 | createMockDispatch(); 36 | expect(DispatchMock.createMockDispatch).toHaveBeenCalledWith(); 37 | }); 38 | 39 | describe('example', () => { 40 | it('works', () => { 41 | const dispatchMock = createMockDispatch(); 42 | const action = { 43 | type: 'type', 44 | data: 'data', 45 | }; 46 | dispatchMock.dispatch(action); 47 | 48 | expect(dispatchMock.getAction(action.type)).toEqual(action); 49 | expect(dispatchMock.getActions()).toEqual([action]); 50 | expect(dispatchMock.isActionDispatched(action)).toBe(true); 51 | expect(dispatchMock.isActionTypeDispatched(action.type)).toBe(true); 52 | }); 53 | }); 54 | }); 55 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | export { createMockStore } from './utils/StoreMock'; 2 | export { createMockDispatch } from './utils/DispatchMock'; 3 | -------------------------------------------------------------------------------- /src/utils/DispatchMock.ts: -------------------------------------------------------------------------------- 1 | import deepEqual from 'fast-deep-equal'; 2 | import { AnyAction, Dispatch } from 'redux'; 3 | 4 | export interface MockDispatch { 5 | dispatch: Dispatch; 6 | getActions: () => AnyAction[]; 7 | getAction: (type: any) => AnyAction | undefined; 8 | isActionTypeDispatched: (type: any) => boolean; 9 | isActionDispatched: (action: AnyAction) => boolean; 10 | } 11 | 12 | export const createMockDispatch = (): MockDispatch => { 13 | const actions: AnyAction[] = []; 14 | return { 15 | dispatch(action: A): A { 16 | actions.push(action); 17 | return action; 18 | }, 19 | 20 | getActions(): AnyAction[] { 21 | return actions; 22 | }, 23 | 24 | getAction(type): AnyAction | undefined { 25 | for (let i = 0; i < actions.length; i += 1) { 26 | if (actions[i].type === type) { 27 | return actions[i]; 28 | } 29 | } 30 | return undefined; 31 | }, 32 | 33 | isActionTypeDispatched(type): boolean { 34 | for (let i = 0; i < actions.length; i += 1) { 35 | if (actions[i].type === type) { 36 | return true; 37 | } 38 | } 39 | return false; 40 | }, 41 | 42 | isActionDispatched(action): boolean { 43 | for (let i = 0; i < actions.length; i += 1) { 44 | if (actions[i].type === action.type) { 45 | if (deepEqual(actions[i], action)) { 46 | return true; 47 | } 48 | } 49 | } 50 | return false; 51 | }, 52 | }; 53 | }; 54 | -------------------------------------------------------------------------------- /src/utils/StoreMock.ts: -------------------------------------------------------------------------------- 1 | import { AnyAction, Store, Unsubscribe } from 'redux'; 2 | import { createMockDispatch, MockDispatch } from './DispatchMock'; 3 | 4 | export interface MockStore 5 | extends Omit, 'Symbol.observable'>, 6 | MockDispatch {} 7 | 8 | const isFunction = (arg: any): boolean => typeof arg === 'function'; 9 | 10 | export const emptyStore = (): Omit, 'Symbol.observable'> => ({ 11 | dispatch(action: A): A { 12 | return action; 13 | }, 14 | getState(): {} { 15 | return {}; 16 | }, 17 | subscribe(): () => void { 18 | return (): void => { 19 | // Dummy 20 | }; 21 | }, 22 | replaceReducer(): void { 23 | // Dummy 24 | }, 25 | }); 26 | 27 | export const createMockStore = (state: S): MockStore => { 28 | const dispatchMock = createMockDispatch(); 29 | const subscribers: (() => void)[] = []; 30 | 31 | return { 32 | getState(): S { 33 | return state; 34 | }, 35 | replaceReducer(): void { 36 | // Do nothing since it is not needed in tests 37 | }, 38 | subscribe(subscriber: () => void): Unsubscribe { 39 | if (isFunction(subscriber)) { 40 | subscribers.push(subscriber); 41 | } 42 | return subscriber; 43 | }, 44 | dispatch: (action: A): A => { 45 | for (let i = 0; i < subscribers.length; i++) { 46 | subscribers[i](); 47 | } 48 | return dispatchMock.dispatch(action); 49 | }, 50 | getActions: dispatchMock.getActions, 51 | getAction: dispatchMock.getAction, 52 | isActionDispatched: dispatchMock.isActionDispatched, 53 | isActionTypeDispatched: dispatchMock.isActionTypeDispatched, 54 | }; 55 | }; 56 | -------------------------------------------------------------------------------- /src/utils/__tests__/DispatchMockSpec.ts: -------------------------------------------------------------------------------- 1 | import { AnyAction } from 'redux'; 2 | import { createMockDispatch } from '../DispatchMock'; 3 | 4 | describe('DispatchMock', () => { 5 | describe('getActions', () => { 6 | it('returns all dispatched actions', () => { 7 | const mock = createMockDispatch(); 8 | const action1: AnyAction = { 9 | type: 'action1', 10 | }; 11 | const action2: AnyAction = { 12 | type: 'action2', 13 | }; 14 | mock.dispatch(action1); 15 | mock.dispatch(action2); 16 | 17 | expect(mock.getActions()).toEqual([action1, action2]); 18 | }); 19 | }); 20 | 21 | describe('getAction', () => { 22 | describe('when action is not dispatched', () => { 23 | it('returns undefined', () => { 24 | const mock = createMockDispatch(); 25 | expect(mock.getAction('action name')).toBe(undefined); 26 | }); 27 | }); 28 | 29 | describe('when action is dispatched', () => { 30 | it('is returned', () => { 31 | const mock = createMockDispatch(); 32 | const type = 'action type'; 33 | const action = { 34 | type, 35 | }; 36 | mock.dispatch(action); 37 | expect(mock.getAction(type)).toEqual(action); 38 | }); 39 | }); 40 | 41 | describe('when several actions is dispatched', () => { 42 | it('returns action with given type', () => { 43 | const mock = createMockDispatch(); 44 | const type = 'action type'; 45 | const action = { 46 | type, 47 | }; 48 | mock.dispatch({ type: 'random action 1' }); 49 | mock.dispatch(action); 50 | mock.dispatch({ type: 'random action 2' }); 51 | 52 | expect(mock.getAction(type)).toEqual(action); 53 | }); 54 | }); 55 | }); 56 | 57 | describe('isActionTypeDispatched', () => { 58 | describe('when action has not been dispatched', () => { 59 | it('returns false', () => { 60 | const mock = createMockDispatch(); 61 | expect(mock.isActionTypeDispatched('not dispatched action type')).toBe(false); 62 | }); 63 | }); 64 | 65 | describe('when actions has been dispatched', () => { 66 | it('returns true', () => { 67 | const mock = createMockDispatch(); 68 | const type = 'action type'; 69 | const action = { 70 | type, 71 | }; 72 | mock.dispatch(action); 73 | expect(mock.isActionTypeDispatched(type)).toBe(true); 74 | }); 75 | }); 76 | }); 77 | 78 | describe('isActionDispatched', () => { 79 | describe('when actions has not been dispatched', () => { 80 | it('returns false', () => { 81 | const mock = createMockDispatch(); 82 | const action = { 83 | type: 'not dispatched action type', 84 | }; 85 | expect(mock.isActionDispatched(action)).toBe(false); 86 | }); 87 | }); 88 | 89 | describe('when action has been dispatched', () => { 90 | describe('and all fields in action matches', () => { 91 | describe('when all subFields matches', () => { 92 | it('returns true', () => { 93 | const mock = createMockDispatch(); 94 | const action = { 95 | type: 'type', 96 | field1: { 97 | subField1: 'subField1', 98 | subField2: 'subField2', 99 | }, 100 | field2: 'field2', 101 | }; 102 | const expectedAction = { 103 | type: 'type', 104 | field1: { 105 | subField1: 'subField1', 106 | subField2: 'subField2', 107 | }, 108 | field2: 'field2', 109 | }; 110 | mock.dispatch(action); 111 | expect(mock.isActionDispatched(expectedAction)).toBe(true); 112 | }); 113 | }); 114 | 115 | describe('when all subFields does not match', () => { 116 | it('returns false', () => { 117 | const mock = createMockDispatch(); 118 | const action = { 119 | type: 'type', 120 | field1: { 121 | subField1: 'subField1', 122 | subField2: 'subField2', 123 | }, 124 | field2: 'field2', 125 | }; 126 | const expectedAction = { 127 | type: 'type', 128 | field1: { 129 | subField3: 'subField3', 130 | }, 131 | field2: 'field2', 132 | }; 133 | mock.dispatch(action); 134 | expect(mock.isActionDispatched(expectedAction)).toBe(false); 135 | }); 136 | }); 137 | }); 138 | 139 | describe('and not all fields in action matches', () => { 140 | it('returns false', () => { 141 | const mock = createMockDispatch(); 142 | const action = { 143 | type: 'type', 144 | field1: 'field1', 145 | }; 146 | const notDispatchAction = { 147 | type: action.type, 148 | field1: `not ${action.field1}`, 149 | }; 150 | mock.dispatch(action); 151 | expect(mock.isActionDispatched(notDispatchAction)).toBe(false); 152 | }); 153 | }); 154 | }); 155 | }); 156 | }); 157 | -------------------------------------------------------------------------------- /src/utils/__tests__/StoreMockSpec.ts: -------------------------------------------------------------------------------- 1 | import * as DispatchMock from '../DispatchMock'; 2 | import { createMockStore, emptyStore } from '../StoreMock'; 3 | 4 | const dispatch = jest.fn(); 5 | const getActions = jest.fn(); 6 | const getAction = jest.fn(); 7 | const isActionDispatched = jest.fn(); 8 | const isActionTypeDispatched = jest.fn(); 9 | 10 | beforeEach(() => { 11 | jest.spyOn(DispatchMock, 'createMockDispatch').mockReturnValue({ 12 | dispatch, 13 | getActions, 14 | getAction, 15 | isActionDispatched, 16 | isActionTypeDispatched, 17 | }); 18 | }); 19 | 20 | afterEach(() => { 21 | jest.resetAllMocks(); 22 | }); 23 | 24 | describe('StoreMock', () => { 25 | describe('emptyStore', () => { 26 | const store = emptyStore(); 27 | 28 | describe('getState()', () => { 29 | it('returns empty state object', () => { 30 | expect(store.getState()).toEqual({}); 31 | }); 32 | }); 33 | 34 | describe('subscribe', () => { 35 | it('is defined', () => { 36 | expect(typeof store.subscribe).toBe('function'); 37 | }); 38 | }); 39 | }); 40 | 41 | describe('createMockStore', () => { 42 | describe('state', () => { 43 | describe('when not provided', () => { 44 | it('returns empty object', () => { 45 | expect(createMockStore({}).getState()).toEqual({}); 46 | }); 47 | }); 48 | describe('when provided', () => { 49 | it('returns that state', () => { 50 | const state = 'state'; 51 | expect(createMockStore(state).getState()).toEqual(state); 52 | }); 53 | }); 54 | }); 55 | 56 | describe('subscribe', () => { 57 | it('works', () => { 58 | const subscriber = jest.fn(); 59 | const mockStore = createMockStore({}); 60 | mockStore.subscribe(subscriber); 61 | const action = { type: 'action' }; 62 | mockStore.dispatch(action); 63 | expect(subscriber).toHaveBeenCalled(); 64 | }); 65 | }); 66 | 67 | describe('dispatch', () => { 68 | it('uses dispatchMock', () => { 69 | const store = createMockStore({}); 70 | store.dispatch({ type: '' }); 71 | expect(dispatch).toHaveBeenCalled(); 72 | }); 73 | 74 | describe('getDispatchActions', () => { 75 | it('uses dispatchMock', () => { 76 | const store = createMockStore({}); 77 | expect(store.getActions).toBe(getActions); 78 | }); 79 | }); 80 | 81 | describe('getDispatchAction', () => { 82 | it('uses dispatchMock', () => { 83 | const store = createMockStore({}); 84 | expect(store.getAction).toBe(getAction); 85 | }); 86 | }); 87 | 88 | describe('isActionTypeDispatched', () => { 89 | it('uses dispatchMock', () => { 90 | const store = createMockStore({}); 91 | expect(store.isActionTypeDispatched).toBe(isActionTypeDispatched); 92 | }); 93 | }); 94 | 95 | describe('isActionDispatched', () => { 96 | it('uses dispatchMock', () => { 97 | const store = createMockStore({}); 98 | expect(store.isActionDispatched).toBe(isActionDispatched); 99 | }); 100 | }); 101 | }); 102 | }); 103 | }); 104 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "declaration": true, 4 | "esModuleInterop": true, 5 | "module": "commonjs", 6 | "outDir": "./dist", 7 | "rootDir": "./src", 8 | "strict": true, 9 | "target": "es5" 10 | }, 11 | "include": [ 12 | "./src/**/*" 13 | ], 14 | "exclude": [ 15 | "./src/**/*Spec*" 16 | ] 17 | } 18 | --------------------------------------------------------------------------------