-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathtypeOfArguments.spec.js
More file actions
45 lines (38 loc) · 1.85 KB
/
Copy pathtypeOfArguments.spec.js
File metadata and controls
45 lines (38 loc) · 1.85 KB
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
const typeOfArguments = require('../typeOfArguments');
describe('typeOfArguments.js', () => {
describe('supports primitive types', () => {
const expectedArray = ['string', 'number', 'bigint', 'boolean', 'symbol', 'null', 'undefined'];
it('does not throw an error if arguments match.', () => {
expect(() => typeOfArguments(['string', 5, BigInt(5), true, Symbol('foo'), null, undefined], expectedArray)).not.toThrowError();
});
it('throws TypeError if an argument does not match.', () => {
expect(() => typeOfArguments([5, BigInt(5), true, Symbol('foo'), null, undefined, 'string'], expectedArray)).toThrowError(TypeError);
});
});
describe('supports classname checking', () => {
const expectedArray = ['Car', 'Bus'];
class Car {}
class Bus {}
it('does not throw an error if arguments match.', () => {
expect(() => typeOfArguments([new Car(), new Bus()], expectedArray)).not.toThrowError();
});
it('throws TypeError if an argument does not match.', () => {
expect(() => typeOfArguments([new Bus(), new Car()], expectedArray)).toThrowError(TypeError);
});
});
it('supports multiple types in expressions using "|".', () => {
const expectedArray = ['number|string|null'];
expect(() => typeOfArguments([18], expectedArray)).not.toThrowError();
expect(() => typeOfArguments(['string'], expectedArray)).not.toThrowError();
expect(() => typeOfArguments([null], expectedArray)).not.toThrowError();
});
it('accepts "object" in expressions.', () => {
expect(() => typeOfArguments([{}, new Object()], ['object', 'object'])).not.toThrowError(); // eslint-disable-line
});
it('throws TypeError if a required argument is missing.', () => {
function testFn (x, y) {
typeOfArguments([x, y], ['string', 'string']);
}
expect(() => testFn()).toThrowError(TypeError);
});
});