diff --git a/lib/utils/index.js b/lib/utils/index.js index 860af4b56..882de625b 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -26,3 +26,9 @@ export const classNames = (...args) => { }; export const mockState = (state, action) => ({ ...state, ...action }); + +export const isUndefined = v => typeof v === 'undefined'; + +export const isNull = v => v === null; + +export const exists = v => !isNull(v) && !isUndefined(v); diff --git a/lib/utils/index.test.js b/lib/utils/index.test.js index 93f46d989..0d04ad8a0 100644 --- a/lib/utils/index.test.js +++ b/lib/utils/index.test.js @@ -1,8 +1,7 @@ -import { classNames, mockState } from './'; +import { classNames, mockState, exists, isUndefined, isNull } from './'; describe('utils', () => { describe('classNames(...classes)', () => { - it('should merge arguments into a list of css classes', () => { expect(classNames('test', 'secondTest')).toBe('test secondTest'); }); @@ -36,7 +35,6 @@ describe('utils', () => { expect(classNames('test', ['secondTest'], true)) .toBe('test secondTest'); }); - }); describe('mockState(state, action)', () => { @@ -45,4 +43,46 @@ describe('utils', () => { .toMatchObject({ foo: 'test' }); }); }); + + describe('isNull(value)', () => { + it('should return true when value is null', () => { + expect(isNull(null)).toBe(true); + }); + + it('should return false when value is undefined', () => { + expect(isNull()).toBe(false); + }); + + it('should return false when value is defined and not null', () => { + expect(isNull(true)).toBe(false); + }); + }); + + describe('isUndefined(value)', () => { + it('should return true when value is undefined', () => { + expect(isUndefined()).toBe(true); + }); + + it('should return false when value is null', () => { + expect(isUndefined(null)).toBe(false); + }); + + it('should return false when value is defined and not null', () => { + expect(isUndefined(true)).toBe(false); + }); + }); + + describe('exists(value)', () => { + it('should return true when value is defined and not null', () => { + expect(exists(0)).toBe(true); + }); + + it('should return false when value is null', () => { + expect(exists(null)).toBe(false); + }); + + it('should return false when value is not defined', () => { + expect(exists()).toBe(false); + }); + }); });