Skip to content

Commit

Permalink
Add typecheck.js test (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmeritz authored Jun 13, 2020
1 parent 3754bd9 commit 1014688
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
1 change: 0 additions & 1 deletion lib/typecheck.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// istanbul ignore file
'use strict';

var includes = require('lodash/includes');
Expand Down
35 changes: 35 additions & 0 deletions test/typecheck.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

var check = require('../lib/typecheck');
var isNumber = require('lodash/isNumber');

describe('check', function() {
var errorMessage = 'It did not check correctly';

it('checks if it passes the function to check', function() {
var checkFn = check(function(n) {
return n === 5;
}, errorMessage);
expect(checkFn(5).pass).toBe(true);
expect(checkFn(5).error).toBeUndefined();
expect(checkFn(1).pass).toBe(false);
expect(checkFn(1).error).toBe(errorMessage);
});

it('checks it is one of', function() {
var checkIsOneOfFn = check.isOneOf([10, 30]);
expect(checkIsOneOfFn(10)).toBe(true);
expect(checkIsOneOfFn(30)).toBe(true);
expect(checkIsOneOfFn(33)).toBe(false);
expect(checkIsOneOfFn('ten')).toBe(false);
});

it('checks it is array of', function() {
var checkIsArrayOfNumberFn = check.isArrayOf(isNumber);
expect(checkIsArrayOfNumberFn([])).toBe(true);
expect(checkIsArrayOfNumberFn({})).toBe(false);
expect(checkIsArrayOfNumberFn('55,22')).toBe(false);
expect(checkIsArrayOfNumberFn([55, 22])).toBe(true);
expect(checkIsArrayOfNumberFn([55, '5', 22])).toBe(false);
});
});

0 comments on commit 1014688

Please sign in to comment.