Nucleus Powered Mitochondria
Miss any of our Open RFC calls?Watch the recordings here! »

object-invariant-test-helper

0.1.1 • Public • Published

object-invariant-test-helper

A module that is useful for testing whether you've mutated an object. Use it with redux when testing your reducers to ensure you don't ever mutate an object in a reducer but always return a new object.

This module began life as a fork of redux-immutable-state-invariant by Leonardo Andres Garcia Crespo (@leoasis).

Setup

This lib is intended for use only during development, specifically testing.

Don't use this in production!

npm install --save-dev object-invariant-test-helper

Usage

import objectInvariantTestHelper from '../src/index';
 
const toAdd = 2;
const expectedState = {num: 3};
 
describe('goodReducer', () => {
  it('should *not* mutate state', () => {
    let initialState = {num: 1};
    const tracked = objectInvariantTestHelper.trackObj(initialState);
 
    let finalState = goodReducer(initialState, {
      type: 'ADD',
      payload: { toAdd }
    });
 
    expect(finalState).toEqual(expectedState);
    expect(objectInvariantTestHelper.hasMutated(tracked)).toEqual(false);
  });
});

If you want to provide your own isImmutable function, you may:

import { objectInvariantTestHelperMkr } from '../src/index';
let objectInvariantTestHelper = objectInvariantTestHelperMkr(isImmutable);

Keywords

none

Install

npm i object-invariant-test-helper

DownloadsWeekly Downloads

118

Version

0.1.1

License

MIT

Last publish

Collaborators

  • avatar