Skip to content

Commit

Permalink
feat(utils): add isNull, isUndefined & exists helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
dackmin committed Jun 1, 2020
1 parent bf0f679 commit 0d61e08
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
6 changes: 6 additions & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
46 changes: 43 additions & 3 deletions lib/utils/index.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
Expand Down Expand Up @@ -36,7 +35,6 @@ describe('utils', () => {
expect(classNames('test', ['secondTest'], true))
.toBe('test secondTest');
});

});

describe('mockState(state, action)', () => {
Expand All @@ -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);
});
});
});

0 comments on commit 0d61e08

Please sign in to comment.