-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
explicitNull.jsx
59 lines (47 loc) · 2.28 KB
/
explicitNull.jsx
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
import { expect } from 'chai';
import React from 'react';
import { explicitNull } from '..';
import callValidator from './_callValidator';
describe('explicitNull', () => {
it('returns a function', () => {
expect(typeof explicitNull()).to.equal('function');
});
function assertPasses(validator, element, propName) {
expect(callValidator(validator, element, propName, '"explicitNull" test')).to.equal(null);
}
function assertFails(validator, element, propName) {
expect(callValidator(validator, element, propName, '"explicitNull" test')).to.be.instanceOf(Error);
}
it('passes on null/undefined/missing', () => {
const validator = explicitNull();
assertPasses(validator, (<div foo={null} />), 'foo');
assertPasses(validator.isRequired, (<div foo={null} />), 'foo');
assertPasses(validator, (<div foo={undefined} />), 'foo');
assertPasses(validator, (<div />), 'foo');
});
it('fails on undefined/missing when required', () => {
const validator = explicitNull();
assertFails(validator.isRequired, <div foo={undefined} />, 'foo');
assertFails(validator.isRequired, <div />, 'foo');
});
it('fails on any other value', () => {
assertFails(explicitNull(), (<div foo={false} />), 'foo');
assertFails(explicitNull(), (<div foo={NaN} />), 'foo');
assertFails(explicitNull(), (<div foo={[]} />), 'foo');
assertFails(explicitNull(), (<div foo={{}} />), 'foo');
assertFails(explicitNull(), (<div foo="" />), 'foo');
assertFails(explicitNull(), (<div foo="foo" />), 'foo');
assertFails(explicitNull(), (<div foo={() => {}} />), 'foo');
assertFails(explicitNull(), (<div foo={/a/g} />), 'foo');
});
it('fails on any other value when required', () => {
assertFails(explicitNull().isRequired, (<div foo={false} />), 'foo');
assertFails(explicitNull().isRequired, (<div foo={NaN} />), 'foo');
assertFails(explicitNull().isRequired, (<div foo={[]} />), 'foo');
assertFails(explicitNull().isRequired, (<div foo={{}} />), 'foo');
assertFails(explicitNull().isRequired, (<div foo="" />), 'foo');
assertFails(explicitNull().isRequired, (<div foo="foo" />), 'foo');
assertFails(explicitNull().isRequired, (<div foo={() => {}} />), 'foo');
assertFails(explicitNull().isRequired, (<div foo={/a/g} />), 'foo');
});
});