-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathPhoneNumber.test.ts
157 lines (132 loc) · 5.12 KB
/
PhoneNumber.test.ts
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* global describe, test, expect */
import { Kind } from 'graphql/language';
import { GraphQLPhoneNumber } from '../src/scalars/PhoneNumber.js';
function PhoneNumberError(value: string) {
return `Invalid phone number: ${value}. Please ensure it's in a valid format. The country code is optional, and Spaces and dashes are allowed. Examples: +1 (123) 456-7890, +44 (20) 2121 2222, or 123 456-7890.`
}
describe('PhoneNumber', () => {
describe('valid formats', () => {
describe('with country code', () => {
test('serialize', () => {
expect(GraphQLPhoneNumber.serialize('+16075551234')).toBe('+16075551234');
});
test('parseValue', () => {
expect(GraphQLPhoneNumber.parseValue('+16075551234')).toBe('+16075551234');
});
test('parseLiteral', () => {
expect(
GraphQLPhoneNumber.parseLiteral({ value: '+16075551234', kind: Kind.STRING }, {}),
).toBe('+16075551234');
});
});
describe('without country code', () => {
test('serialize', () => {
expect(() => GraphQLPhoneNumber.serialize('7895551234')).not.toThrow()
});
test('parseValue', () => {
expect(() => GraphQLPhoneNumber.parseValue('123 456-7890')).not.toThrow()
});
test('parseLiteral', () => {
expect(() =>
GraphQLPhoneNumber.parseLiteral({ value: '789 555 1234', kind: Kind.STRING }, {}),
).not.toThrow();
});
});
describe('different formatting', () => {
test('serialize', () => {
expect(() => GraphQLPhoneNumber.serialize('62-(21)-9175-5194')).not.toThrow()
});
test('serialize', () => {
expect(() => GraphQLPhoneNumber.serialize('+622191755194')).not.toThrow()
});
test('parseValue', () => {
expect(() => GraphQLPhoneNumber.parseValue('+62 (21) 9175 5194')).not.toThrow()
});
test('parseLiteral', () => {
expect(() =>
GraphQLPhoneNumber.parseLiteral({ value: '+1 (123) 456-7890', kind: Kind.STRING }, {}),
).not.toThrow();
});
});
});
describe('invalid case', () => {
describe('contains Non-Numeric Characters', () => {
test('serialize', () => {
expect(() => GraphQLPhoneNumber.serialize('98aaa333')).toThrow(PhoneNumberError('98aaa333'));
});
test('parseValue', () => {
expect(() => GraphQLPhoneNumber.parseValue('98aaa333ppp')).toThrow(PhoneNumberError('98aaa333ppp'));
});
test('parseLiteral', () => {
expect(() =>
GraphQLPhoneNumber.parseLiteral(
{ value: '98aa', kind: Kind.STRING },
{},
),
).toThrow(PhoneNumberError('98aa'));
});
});
describe('not a string', () => {
test('serialize', () => {
expect(() => GraphQLPhoneNumber.serialize(123)).toThrow(/Value is not string/);
});
test('parseValue', () => {
expect(() => GraphQLPhoneNumber.parseValue(123)).toThrow(/Value is not string/);
});
test('parseLiteral', () => {
expect(() => GraphQLPhoneNumber.parseLiteral({ value: '123', kind: Kind.INT }, {})).toThrow(
/Can only validate strings as phone numbers but got a/,
);
});
});
describe('wrong formate', () => {
test('serialize', () => {
expect(() => GraphQLPhoneNumber.serialize('+17 89- 5')).toThrow(PhoneNumberError('+17 89- 5'));
});
test('serialize', () => {
expect(() => GraphQLPhoneNumber.serialize('+1 ( 123 ) 456-7890')).toThrow(PhoneNumberError('+1 ( 123 ) 456-7890'));
});
test('serialize', () => {
expect(() => GraphQLPhoneNumber.serialize('+1[123]456 7890')).toThrow(PhoneNumberError('+1[123]456 7890'));
});
test('parseValue', () => {
expect(() => GraphQLPhoneNumber.parseValue('+(178)95 55 5678')).toThrow(PhoneNumberError('+(178)95 55 5678'));
});
test('parseLiteral', () => {
expect(() =>
GraphQLPhoneNumber.parseLiteral({ value: '1789 [555] 1234', kind: Kind.STRING }, {}),
).toThrow(PhoneNumberError('1789 [555] 1234'));
});
});
describe('too small', () => {
test('serialize', () => {
expect(() => GraphQLPhoneNumber.serialize('+12')).toThrow(PhoneNumberError('+12'));
});
test('parseValue', () => {
expect(() => GraphQLPhoneNumber.parseValue('+12')).toThrow(PhoneNumberError('+12'));
});
test('parseLiteral', () => {
expect(() =>
GraphQLPhoneNumber.parseLiteral({ value: '+12', kind: Kind.STRING }, {}),
).toThrow(PhoneNumberError('+12'));
});
});
describe('support more countries', () => {
test('support Singapore numbers - 10 digits', () => {
expect(() => {
GraphQLPhoneNumber.parseValue('+6569701665');
}).not.toThrow();
});
test('support Solomon Islands numbers - 8 digits', () => {
expect(() => {
GraphQLPhoneNumber.parseValue('+67734700');
}).not.toThrow();
});
test('support Niue numbers - 7 digits', () => {
expect(() => {
GraphQLPhoneNumber.parseValue('+6834999');
}).not.toThrow();
});
});
});
});