-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathphone-assert_test.js
More file actions
93 lines (75 loc) · 2.43 KB
/
phone-assert_test.js
File metadata and controls
93 lines (75 loc) · 2.43 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* Module dependencies.
*/
import PhoneAssert from '../../src/asserts/phone-assert';
import should from 'should';
import { Assert as BaseAssert, Violation } from 'validator.js';
/**
* Extend `Assert` with `Phone`.
*/
const Assert = BaseAssert.extend({
Phone: PhoneAssert
});
/**
* Test `Phone`.
*/
describe('Phone', () => {
it('should throw an error if the allowed types are invalid', () => {
['foobar', ['foobar']].forEach(types => {
try {
new Assert().Phone({ allowedTypes: types }).validate('+1 415 555 2671');
should.fail();
} catch (e) {
e.should.be.instanceOf(Error);
e.message.should.equal('Phone types specified are not valid.');
}
});
});
it('should throw an error if the input value is not a string', () => {
[{}, [], 123].forEach(choice => {
try {
new Assert().Phone({ countryCode: 'US' }).validate(choice);
should.fail();
} catch (e) {
e.should.be.instanceOf(Violation);
e.violation.value.should.equal('must_be_a_string');
}
});
});
it('should throw an error if the phone is not valid', () => {
try {
new Assert().Phone().validate('+35191234567890');
should.fail();
} catch (e) {
e.should.be.instanceOf(Violation);
e.show().assert.should.equal('Phone');
}
});
it('should throw an error if the phone does not belong to the given country', () => {
try {
new Assert().Phone({ countryCode: 'US' }).validate('912345578');
should.fail();
} catch (e) {
e.should.be.instanceOf(Violation);
e.show().assert.should.equal('Phone');
}
});
it('should throw an error if the phone does not have one of the given allowed types', () => {
try {
new Assert().Phone({ allowedTypes: ['FIXED_LINE', 'MOBILE'] }).validate('+1 415 555 2671');
should.fail();
} catch (e) {
e.should.be.instanceOf(Violation);
e.show().assert.should.equal('Phone');
}
});
it('should accept a valid phone in the e164 format', () => {
new Assert().Phone().validate('+1 415 555 2671');
});
it('should accept a phone in the e164 format that belongs to the given country', () => {
new Assert().Phone({ countryCode: 'US' }).validate('+1 415 555 2671');
});
it('should accept a phone in the national format that belongs to the given country', () => {
new Assert().Phone({ countryCode: 'US' }).validate('415 555 2671');
});
});