all files / src/__tests__/ wrapMapStateToProps.spec.js

100% Statements 52/52
100% Branches 4/4
100% Functions 12/12
100% Lines 43/43
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76                                                                    
import expect, {createSpy} from 'expect';
import wrapMapStateToProps from '../wrapMapStateToProps';
 
describe('wrapMapStateToProps', () => {
  it('should save form if no mapStateToProps given', () => {
    const getForm = createSpy().andReturn('foo');
    const result = wrapMapStateToProps(undefined, getForm);
    expect(result).toBeA('function');
    expect(result.length).toBe(1);
    const mapped = result('bar');
    expect(getForm)
      .toHaveBeenCalled()
      .toHaveBeenCalledWith('bar');
    expect(mapped).toEqual({form: 'foo'});
  });
 
  it('should throw error when mapStateToProps is not a function', () => {
    const getForm = createSpy();
    expect(() => wrapMapStateToProps(true, getForm)).toThrow('mapStateToProps must be a function');
    expect(() => wrapMapStateToProps(42, getForm)).toThrow('mapStateToProps must be a function');
    expect(() => wrapMapStateToProps({}, getForm)).toThrow('mapStateToProps must be a function');
    expect(() => wrapMapStateToProps([], getForm)).toThrow('mapStateToProps must be a function');
    expect(getForm).toNotHaveBeenCalled();
  });
 
  it('should call mapStateToProps when one-param function given', () => {
    const getForm = createSpy().andReturn('foo');
    const mapStateToPropsSpy = createSpy().andReturn({a: 42, b: true, c: 'baz'});
    const mapStateToProps = state => mapStateToPropsSpy(state);
    expect(mapStateToProps.length).toBe(1);
 
    const result = wrapMapStateToProps(mapStateToProps, getForm);
    expect(result).toBeA('function');
    expect(result.length).toBe(1);
    const mapped = result('bar');
    expect(mapStateToPropsSpy)
      .toHaveBeenCalled()
      .toHaveBeenCalledWith('bar');
    expect(getForm)
      .toHaveBeenCalled()
      .toHaveBeenCalledWith('bar');
 
    expect(mapped).toEqual({
      a: 42,
      b: true,
      c: 'baz',
      form: 'foo'
    });
  });
 
  it('should call mapStateToProps when two-param function given', () => {
    const getForm = createSpy().andReturn('foo');
    const mapStateToPropsSpy = createSpy().andReturn({a: 42, b: true, c: 'baz'});
    const mapStateToProps = (state, ownProps) => mapStateToPropsSpy(state, ownProps);
    expect(mapStateToProps.length).toBe(2);
 
    const result = wrapMapStateToProps(mapStateToProps, getForm);
    expect(result).toBeA('function');
    expect(result.length).toBe(2);
    const mapped = result('bar', 'dog');
    expect(mapStateToPropsSpy)
      .toHaveBeenCalled()
      .toHaveBeenCalledWith('bar', 'dog');
    expect(getForm)
      .toHaveBeenCalled()
      .toHaveBeenCalledWith('bar');
 
    expect(mapped).toEqual({
      a: 42,
      b: true,
      c: 'baz',
      form: 'foo'
    });
  });
});