1 | 'use strict';
|
2 |
|
3 | var undefinedOr = require('../src/undefinedOr'),
|
4 | assert = require('chai').assert;
|
5 |
|
6 | describe('undefinedOr', function() {
|
7 | var PREDICATE = function(value) {
|
8 | return typeof value === 'string';
|
9 | },
|
10 | VALID_VALUE = 'string';
|
11 | it('returns a function if only one argument provided', function() {
|
12 | assert.ok(undefinedOr(PREDICATE) instanceof Function);
|
13 | });
|
14 |
|
15 | it('throws TypeError if predicate is not a function', function() {
|
16 | var NOT_PREDICATE = 'definitely not predicate';
|
17 | assert.throws(function() {
|
18 | undefinedOr(NOT_PREDICATE)(VALID_VALUE);
|
19 | }, TypeError, /must be a function/);
|
20 |
|
21 | assert.throws(function() {
|
22 | undefinedOr(NOT_PREDICATE);
|
23 | }, TypeError, /must be a function/);
|
24 |
|
25 | assert.throws(function() {
|
26 | undefinedOr(NOT_PREDICATE, VALID_VALUE);
|
27 | }, TypeError, /must be a function/);
|
28 |
|
29 | assert.doesNotThrow(function() {
|
30 | undefinedOr(PREDICATE, VALID_VALUE);
|
31 | });
|
32 | });
|
33 |
|
34 | it('calls the predicate in the same context and forward value arguments', function() {
|
35 | var exampleContext = {example: 'context'},
|
36 | predicate = function() {
|
37 | assert.deepEqual(exampleContext, this);
|
38 | assert.strictEqual(arguments[0], 1);
|
39 | assert.strictEqual(arguments[1], '2');
|
40 | assert.strictEqual(arguments[2], null);
|
41 | assert.strictEqual(arguments[3], undefined);
|
42 | };
|
43 |
|
44 | undefinedOr(predicate).call(exampleContext, 1, '2', null);
|
45 | undefinedOr.call(exampleContext, predicate, 1, '2', null);
|
46 | });
|
47 |
|
48 | it('does not call the predicate if value is undefined', function() {
|
49 | var predicate = function() {
|
50 | throw new Error('Predicate should not be called');
|
51 | };
|
52 | undefinedOr(predicate, undefined);
|
53 | undefinedOr(predicate)();
|
54 | undefinedOr(predicate)(undefined);
|
55 | });
|
56 |
|
57 | it('checks whether value is undefined or satisfies given predicate', function() {
|
58 | assert.ok(undefinedOr(PREDICATE, VALID_VALUE));
|
59 | assert.ok(undefinedOr(PREDICATE)(VALID_VALUE));
|
60 | assert.ok(undefinedOr(PREDICATE, undefined));
|
61 | assert.ok(undefinedOr(PREDICATE)(undefined));
|
62 | assert.ok(undefinedOr(PREDICATE)());
|
63 | assert.ok(undefinedOr(PREDICATE, 1) === false);
|
64 | assert.ok(undefinedOr(PREDICATE)(1) === false);
|
65 | });
|
66 | });
|