Skip to content

Instantly share code, notes, and snippets.

@KVeitch
Last active December 3, 2019 15:43
Show Gist options
  • Save KVeitch/98b407ae31ea84013044820dee6a1b54 to your computer and use it in GitHub Desktop.
Save KVeitch/98b407ae31ea84013044820dee6a1b54 to your computer and use it in GitHub Desktop.

Action Creators Tests:

import { setType } from './index'


describe('Action Creators', () => {
    it('setType should return the correct object', () => {
        const returnState = dataThatGoesHere;
        const expected = {
            type: 'SET_TYPE',
            returnState
    };
    const results = setCityName(cityName);
    expect(results).toEqual(expected);
});

Testing Reducers:

describe('reducerName reducer', () => {
    it('should return the default state as an empty string', () => {
        const result = reducerName(undefined, {});
        expect(result).toEqual('');
    });
    
    it('reducerName should return new state when the action type is SET_CITY_NAME', () => {
        const mockAction = {
                type: 'SET_REDUCER_NAME',
                reducerName: 'Susan'
            };
        const results = reducerName('', mockAction);
        expect(results).toEqual(mockAction.re);
    });
});

Testing mapStateToProps:

import { componentName, mapStateToProps} from './ComponentName';



 describe('componentName Redux', () => {
    it('mapStateToProps give all the needed data from state', () => {
        const mockState = {
            state1,
            state2,
            state3
        };
 
        const expected = {
            state1,
            state2
        };

        const mappedProps = mapStateToProps(mockState);

        expect(mappedProps).toEqual(expected);
    });
});

Testing mapDispatchToProps:

import * as actions from '../../actions';
import { componentName, mapDispatchToProps} from './ComponentName';


describe('MainForm mapDispatchToProps', () => {
 it('should call dispatch with setDestination action when setDestination is called', () => {
      const mockDispatch = jest.fn();
      const mockDestination = 'DEN';
      const actionToDispatch = actions.setDestination('SET_DESTINATION',mockDestination);
      const mappedProps = mapDispatchToProps(mockDispatch);
      mappedProps.setDestination('SET_DESTINATION',mockDestination);
      expect(mockDispatch).toHaveBeenCalledWith(actionToDispatch);
      });
   });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment