-
Notifications
You must be signed in to change notification settings - Fork 459
Expand file tree
/
Copy pathuseFormattedPhoneNumber.test.ts
More file actions
126 lines (105 loc) · 3.92 KB
/
Copy pathuseFormattedPhoneNumber.test.ts
File metadata and controls
126 lines (105 loc) · 3.92 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
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
import { renderHook, waitFor } from '@testing-library/react';
import { afterEach, beforeAll, describe, expect, it } from 'vitest';
import { loadCountryCodeData } from '../countryCodeDataLoader';
import { useFormattedPhoneNumber } from '../useFormattedPhoneNumber';
beforeAll(async () => {
await loadCountryCodeData();
});
describe('useFormattedPhoneNumber', () => {
afterEach(() => {
// Empty the localStorage used within the hook
window.localStorage.clear();
});
it('should parse and split the init phone value', () => {
const { result } = renderHook(() =>
useFormattedPhoneNumber({
initPhoneWithCode: '+71111111111',
locationBasedCountryIso: undefined,
}),
);
const { number, iso, formattedNumber, numberWithCode } = result.current;
expect(number).toBe('1111111111');
expect(iso).toBe('ru');
expect(numberWithCode).toBe('+71111111111');
expect(formattedNumber).toBe('111 111-11-11');
});
it('should return the correct iso for the number even if location based iso is wrong', () => {
const { result } = renderHook(() =>
useFormattedPhoneNumber({
initPhoneWithCode: '+1 (202) 123 1234',
locationBasedCountryIso: 'gr',
}),
);
const { number, iso, formattedNumber, numberWithCode } = result.current;
expect(number).toBe('2021231234');
expect(iso).toBe('us');
expect(numberWithCode).toBe('+12021231234');
expect(formattedNumber).toBe('(202) 123-1234');
});
it('should correctly handle initialing with an empty value', async () => {
const { result } = renderHook(() =>
useFormattedPhoneNumber({
initPhoneWithCode: '',
locationBasedCountryIso: undefined,
}),
);
const { number, iso, formattedNumber, numberWithCode } = result.current;
await waitFor(() => {
expect(number).toBe('');
expect(iso).toBe('us');
expect(numberWithCode).toBe('');
expect(formattedNumber).toBe('');
});
});
it('should return the correct number and iso when pasting a number containing a country code', async () => {
const { result, rerender, unmount } = renderHook(() =>
useFormattedPhoneNumber({
initPhoneWithCode: '+71111111111',
locationBasedCountryIso: undefined,
}),
);
expect(result.current.numberWithCode).toBe('+71111111111');
expect(result.current.iso).toBe('ru');
// change number, keep iso
await waitFor(() => {
result.current.setNumberAndIso('+1 (202) 123 1234');
rerender();
});
expect(result.current.number).toBe('2021231234');
expect(result.current.iso).toBe('us');
expect(result.current.numberWithCode).toBe('+12021231234');
expect(result.current.formattedNumber).toBe('(202) 123-1234');
unmount();
});
it('should return the correct iso for the number even if location based iso is wrong', async () => {
const { result, rerender, unmount } = renderHook(() =>
useFormattedPhoneNumber({
initPhoneWithCode: '',
locationBasedCountryIso: 'gr',
}),
);
expect(result.current.number).toBe('');
expect(result.current.iso).toBe('gr');
expect(result.current.numberWithCode).toBe('');
expect(result.current.formattedNumber).toBe('');
// change number, keep iso
await waitFor(() => {
result.current.setNumber('6949595959');
rerender();
});
expect(result.current.number).toBe('6949595959');
expect(result.current.iso).toBe('gr');
expect(result.current.numberWithCode).toBe('+306949595959');
expect(result.current.formattedNumber).toBe('694 9595959');
// change iso, keep number
await waitFor(() => {
result.current.setIso('us');
rerender();
});
expect(result.current.number).toBe('6949595959');
expect(result.current.iso).toBe('us');
expect(result.current.numberWithCode).toBe('+16949595959');
expect(result.current.formattedNumber).toBe('(694) 959-5959');
unmount();
});
});