1 | import expect from 'expect';
|
2 | import React from 'react';
|
3 |
|
4 | const nodeInstance = <div></div>;
|
5 |
|
6 | import { check, types, createCustomChecker } from './';
|
7 |
|
8 | const {
|
9 | any,
|
10 | array,
|
11 | arrayOf,
|
12 | bool,
|
13 | exactShape,
|
14 | func,
|
15 | instanceOf,
|
16 | node,
|
17 | number,
|
18 | object,
|
19 | objectOf,
|
20 | oneOf,
|
21 | oneOfType,
|
22 | shape,
|
23 | string,
|
24 | } = types;
|
25 |
|
26 | var zan = require('./');
|
27 |
|
28 | describe('zan', () => {
|
29 | it('is curry-able', () => {
|
30 | expect( check(number)(123) ).toBeFalsy();
|
31 | });
|
32 |
|
33 | it('can do optional checks',() => {
|
34 | expect( check(number.isOptional, 123) ).toBeFalsy();
|
35 | expect( check(number.isOptional, undefined) ).toBeFalsy();
|
36 | expect( check(number.isOptional, null) ).toBeFalsy();
|
37 | expect( check(number.isOptional, 'x') ).toBeAn(Error);
|
38 | });
|
39 |
|
40 | it('can do optional shapes',() => {
|
41 | expect( check(shape({num: number}), {num: 22} )).toBeFalsy();
|
42 | expect( check(shape({num: number}), {num: 'x'} )).toBeAn(Error);
|
43 | expect( check(shape({num: number}), null )).toBeAn(Error);
|
44 | expect( check(shape({num: number}).isOptional, null )).toBeFalsy();
|
45 | expect( check(exactShape({num: number}), null )).toBeAn(Error);
|
46 | });
|
47 |
|
48 | describe('simple types', () => {
|
49 |
|
50 | it('can check if a value is a boolean', () => {
|
51 | expect(check(bool, true)).toBeFalsy();
|
52 | expect(check(bool, 1)).toBeAn(Error);
|
53 | });
|
54 |
|
55 | it('can check if a value is a function', () => {
|
56 | expect(check(func, () => {})).toBeFalsy();
|
57 | expect(check(func, 'str')).toBeAn(Error);
|
58 | expect(check(func.isOptional, null)).toBeFalsy();
|
59 | });
|
60 |
|
61 | it('can check if a value is a number', () => {
|
62 | expect(check(number, 123)).toBeFalsy();
|
63 | expect(check(number, '123')).toBeAn(Error);
|
64 | });
|
65 |
|
66 | it('can check if a value is a node', () => {
|
67 | expect(check(node, nodeInstance)).toBeFalsy();
|
68 | expect(check(node, {})).toBeAn(Error);
|
69 | });
|
70 |
|
71 | it('can check if a value is a string', () => {
|
72 | expect(check(string, 'foo')).toBeFalsy();
|
73 | expect(check(string, /foo/)).toBeAn(Error);
|
74 | });
|
75 |
|
76 | it('is able to produce custom checkers', function() {
|
77 | var customValidator = createCustomChecker(value => /(foo|bar)g?/.test(value) );
|
78 | expect(check(customValidator, 'foo')).toBeFalsy();
|
79 | expect(check(customValidator, 'foog')).toBeFalsy();
|
80 | expect(check(customValidator, 'bar')).toBeFalsy();
|
81 | expect(check(customValidator, 'barg')).toBeFalsy();
|
82 | expect(check(customValidator, 'fog')).toBeAn(Error);
|
83 | });
|
84 |
|
85 | });
|
86 |
|
87 | describe('complex types', function() {
|
88 |
|
89 | describe('has a type `array` that', function() {
|
90 | it('can check if an object is an array', function() {
|
91 | expect(check(array, ['a', 22])).toBeFalsy();
|
92 | expect(check(array, {a: 22})).toBeAn(Error)
|
93 | });
|
94 | });
|
95 |
|
96 | describe('has a type `object` that', function() {
|
97 | it('can check if an object is an object', function() {
|
98 | expect(check(object, {a: 22})).toBeFalsy();
|
99 | expect(check(object, ['a', 22])).toBeAn(Error)
|
100 | });
|
101 | });
|
102 |
|
103 | describe('has a type `objectOf` that', function() {
|
104 | it('can check if an object is an objectOf', function() {
|
105 | expect(check(objectOf(number), {id: 22})).toBeFalsy();
|
106 | expect(check(objectOf(number), {id: '22'})).toBeAn(Error)
|
107 | });
|
108 | });
|
109 |
|
110 | describe('has a type `arrayOf` that', function() {
|
111 | it('can check if an object is an arrayOf', function() {
|
112 | expect(check(arrayOf(number), [22])).toBeFalsy();
|
113 | expect(check(arrayOf(number), ['22'])).toBeAn(Error)
|
114 | expect(check(arrayOf(number), [22, '22'])).toBeAn(Error)
|
115 | });
|
116 | });
|
117 |
|
118 | describe('has a type `instanceOf` that', function() {
|
119 | it('can check if an object is an instanceOf', function() {
|
120 | var C = function C() {};
|
121 | var c = new C();
|
122 | expect(check(instanceOf(C), c)).toBeFalsy();
|
123 | expect(check(instanceOf(C), {})).toBeAn(Error)
|
124 | });
|
125 | });
|
126 |
|
127 | describe('has a type `oneOf` that', function() {
|
128 | it('can check if an object is oneOf a list of specific value', function() {
|
129 | expect(check(oneOf(['foo', 'bar']), 'bar')).toBeFalsy();
|
130 | expect(check(oneOf(['foo', 'bar']), 'baz')).toBeAn(Error)
|
131 | });
|
132 | });
|
133 |
|
134 | describe('has a type `any` that', function() {
|
135 | it('can check if an object anything', function() {
|
136 | expect(check(any, 'bar')).toBeFalsy();
|
137 | expect(check(any, null)).toBeAn(Error)
|
138 | });
|
139 | });
|
140 |
|
141 | describe('has a type `oneOfType` that', function() {
|
142 | it('can check if an object is oneOfType a list of specific value', function() {
|
143 | var stringOrFunction = oneOfType([string, func]);
|
144 | expect(check(stringOrFunction, 'foo')).toBeFalsy();
|
145 | expect(check(stringOrFunction, function() {})).toBeFalsy();
|
146 | expect(check(stringOrFunction, 123)).toBeAn(Error)
|
147 | });
|
148 | });
|
149 |
|
150 | describe('has a type `oneOfType` that', function() {
|
151 | it('can check if an object is oneOfType a list of specific value', function() {
|
152 | var stringOrFunction = oneOfType([string, func]);
|
153 | expect(check(stringOrFunction, 'foo')).toBeFalsy();
|
154 | expect(check(stringOrFunction, function() {})).toBeFalsy();
|
155 | expect(check(stringOrFunction, 123)).toBeAn(Error)
|
156 | });
|
157 | });
|
158 |
|
159 | describe('has a type `shape` that', function() {
|
160 | it('can check if an object matches a shape', function() {
|
161 | var myShape = shape({
|
162 | name: string,
|
163 | age: number,
|
164 | address: shape({
|
165 | street: string
|
166 | })
|
167 | });
|
168 | expect(check(myShape, {extra: 123, name: 'Bob', age: 99, address: {street: '2nd Ave'}})).toBeFalsy();
|
169 | expect(check(myShape, {extra: 123, name: 'Bob', age: 99, address: {street: 2}})).toBeAn(Error)
|
170 | expect(check(shape({name: string.isOptional}), {})).toBeFalsy();
|
171 | });
|
172 | });
|
173 |
|
174 | });
|
175 |
|
176 | describe('has a type `exactShape` that', function() {
|
177 | it('can check if an object exactly matches a exactShape', function() {
|
178 | var myExactShape = exactShape({
|
179 | name: string,
|
180 | age: number,
|
181 | address: exactShape({
|
182 | street: string
|
183 | })
|
184 | });
|
185 | expect(check(myExactShape, {name: 'Bob', age: 99, address: {street: '2nd Ave'}})).toBeFalsy();
|
186 | expect(check(myExactShape, {extra: 123, name: 'Bob', age: 99, address: {street: '2nd Ave'}})).toBeAn(Error)
|
187 | expect(check(myExactShape, {age: 99, address: {street: '2nd Ave'}})).toBeAn(Error)
|
188 | expect(check(myExactShape, {name: 'Bob', age: 99, address: {street: 2}})).toBeAn(Error)
|
189 | });
|
190 | });
|
191 |
|
192 |
|
193 |
|
194 | });
|