-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphone-number.spec.js
79 lines (66 loc) · 2.6 KB
/
phone-number.spec.js
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
import { PhoneNumber } from './phone-number';
describe('PhoneNumber()', () => {
test('cleans the number', () => {
const phone = new PhoneNumber('(223) 456-7890');
expect(phone.number()).toEqual('2234567890');
});
test('cleans numbers with dots', () => {
const phone = new PhoneNumber('223.456.7890');
expect(phone.number()).toEqual('2234567890');
});
test('cleans numbers with multiple spaces', () => {
const phone = new PhoneNumber('223 456 7890 ');
expect(phone.number()).toEqual('2234567890');
});
test('invalid when 9 digits', () => {
const phone = new PhoneNumber('223456789');
expect(phone.number()).toEqual(null);
});
test('invalid when 11 digits does not start with a 1', () => {
const phone = new PhoneNumber('22234567890');
expect(phone.number()).toEqual(null);
});
test('valid when 11 digits and starting with 1', () => {
const phone = new PhoneNumber('12234567890');
expect(phone.number()).toEqual('2234567890');
});
test('valid when 11 digits and starting with 1 even with punctuation', () => {
const phone = new PhoneNumber('+1 (223) 456-7890');
expect(phone.number()).toEqual('2234567890');
});
test('invalid when 12 digits', () => {
const phone = new PhoneNumber('322234567890');
expect(phone.number()).toEqual(null);
});
test('invalid with letters', () => {
const phone = new PhoneNumber('223-abc-7890');
expect(phone.number()).toEqual(null);
});
test('invalid with punctuations', () => {
const phone = new PhoneNumber('223-@:!-7890');
expect(phone.number()).toEqual(null);
});
test('invalid if area code starts with 0 or 1', () => {
const phone1 = new PhoneNumber('(023) 456-7890');
const phone2 = new PhoneNumber('(123) 456-7890');
expect(phone1.number()).toEqual(null);
expect(phone2.number()).toEqual(null);
});
test('invalid if exchange code starts with 0 or 1', () => {
const phone1 = new PhoneNumber('(223) 056-7890');
const phone2 = new PhoneNumber('(223) 156-7890');
expect(phone1.number()).toEqual(null);
expect(phone2.number()).toEqual(null);
});
test('invalid when 11 digits starting with 1, '
+ 'but invalid area/exchange code first digits', () => {
const phone1 = new PhoneNumber('1 (023) 456-7890');
const phone2 = new PhoneNumber('1 (123) 456-7890');
const phone3 = new PhoneNumber('1 (223) 056-7890');
const phone4 = new PhoneNumber('1 (223) 156-7890');
expect(phone1.number()).toEqual(null);
expect(phone2.number()).toEqual(null);
expect(phone3.number()).toEqual(null);
expect(phone4.number()).toEqual(null);
});
});