Skip to content

Instantly share code, notes, and snippets.

@ksummerlin
Created December 21, 2016 17:47
Show Gist options
  • Save ksummerlin/ab35ffed4907f94f65ba55c62ac7cd89 to your computer and use it in GitHub Desktop.
Save ksummerlin/ab35ffed4907f94f65ba55c62ac7cd89 to your computer and use it in GitHub Desktop.
Unit test ImmutableJs collections using newState.equals(expectState) not expect(newState).toEqual(expectState)
import expect from 'expect';
import whynotEqual from 'is-equal/why';
import appLayoutReducer from '../reducer';
import {
hideLeftMenuIcon,
showLeftMenuIcon,
} from '../actions';
import { fromJS } from 'immutable';
describe('appLayoutReducer', () => {
let state;
beforeEach(() => {
state = fromJS({
showLeftMenuIcon: false,
});
});
it('should return the initial state', () => {
const expectedResult = state;
expect(appLayoutReducer(undefined, {})).toEqual(expectedResult);
});
it('should handle the showLeftMenuIcon action correctly', () => {
const expectedResult = state
.set('showLeftMenuIcon', true);
expect(appLayoutReducer(state, showLeftMenuIcon())).toEqual(expectedResult);
});
it('should handle the hideLeftMenuIcon action correctly', () => {
const initState = state
.set('showLeftMenuIcon', true);
const expectedResult = state
.set('showLeftMenuIcon', false);
const newState = appLayoutReducer(initState, hideLeftMenuIcon());
expect(newState).toEqual(expectedResult, whynotEqual(newState, expectedResult));
expect(newState.equals(expectedResult)).toEqual(true);
});
});
@ksummerlin
Copy link
Author

ksummerlin commented Dec 21, 2016

Notice in the last test, that the test in line 40 fails, but line 41 works. The failure from line 40 is described by whyNotEqual() as:

FAILED TESTS:
  appLayoutReducer
    × should handle the hideLeftMenuIcon action correctly
      PhantomJS 2.1.1 (Windows 8 0.0.0)
      PhantomJS 2.1.1 (Windows 8 0.0.0)
    value at key "_root" differs: value at key "ownerID" differs: undefined !== [object Object]
    assert@webpack:///~/expect/lib/assert.js:29:0 <- test-bundler.js:34515:27
    toEqual@webpack:///~/expect/lib/Expectation.js:81:0 <- test-bundler.js:59032:30
    webpack:///app/containers/AppLayout/tests/reducer.tests.js:40:29 <- test-bundler.js:104182:69

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment