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

100% Statements 54/54
100% Branches 4/4
100% Functions 13/13
100% Lines 51/51
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   25× 25×                                                  
import expect from 'expect';
import isPristine from '../isPristine';
 
const tryBothWays = (aValue, bValue, result) => {
  expect(isPristine(aValue, bValue)).toBe(result);
  expect(isPristine(bValue, aValue)).toBe(result);
};
 
describe('isPristine', () => {
  it('should return true if the values are ===', () => {
    const aValue = {foo: 'bar'};
    const bValue = ['foo', 'baz'];
    const cValue = 7;
    expect(isPristine(aValue, aValue)).toBe(true);
    expect(isPristine(bValue, bValue)).toBe(true);
    expect(isPristine(cValue, cValue)).toBe(true);
  });
 
  it('should return false if one value is an object and the other is not', () => {
    tryBothWays({}, 3, false);
    tryBothWays({}, 'foo', false);
    tryBothWays({}, undefined, false);
    tryBothWays({}, null, false);
  });
 
  it('should return true when comparing null and undefined and empty string', () => {
    tryBothWays('', null, true);
    tryBothWays(undefined, null, true);
    tryBothWays(undefined, '', true);
  });
 
  it('should return false when key values are different types', () => {
    tryBothWays({foo: null}, {foo: 'bar'}, false);
    tryBothWays({foo: undefined}, {foo: 'bar'}, false);
    tryBothWays({foo: 69}, {foo: 'bar'}, false);
  });
 
  it('should return false when key values are different', () => {
    tryBothWays({foo: 'bar'}, {foo: 'baz'}, false);
    tryBothWays({foo: 7, bar: 8}, {foo: 7, bar: 9}, false);
    const date1 = new Date();
    const date2 = new Date(date1.getTime() + 1);
    tryBothWays({date: date1}, {date: date2}, false);
 
  });
 
  it('should return false when the number of keys is different', () => {
    tryBothWays({foo: 'bar'}, {}, false);
    tryBothWays([1], [1, 2], false);
  });
 
  it('should return true when matching key values are null, undefined, or empty string', () => {
    tryBothWays({foo: ''}, {foo: null}, true);
    tryBothWays({foo: ''}, {foo: undefined}, true);
    tryBothWays({foo: null}, {foo: undefined}, true);
  });
 
  it('should return false when comparing false to other falsy values', () => {
    tryBothWays(false, null, false);
    tryBothWays(false, undefined, false);
    tryBothWays(false, '', false);
  });
 
  it('should return false when number of keys is different', () => {
    tryBothWays({foo: 'bar'}, {}, false);
    tryBothWays([1], [1, 2], false);
  });
 
  it('should return true when key values are equal', () => {
    const date = new Date();
    tryBothWays({foo: 'bar', when: date}, {foo: 'bar', when: date}, true);
    tryBothWays({foo: 7, bar: 9, when: date}, {foo: 7, bar: 9, when: date}, true);
  });
 
});