-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
intl-tel-input.component.spec.ts
211 lines (168 loc) · 6.35 KB
/
intl-tel-input.component.spec.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule, NgForm } from '@angular/forms';
import { By } from '@angular/platform-browser';
import { IntlTelInputComponent } from './intl-tel-input.component';
describe('IntlTelInputComponent', () => {
let component: IntlTelInputComponent;
let fixture: ComponentFixture<IntlTelInputComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [
IntlTelInputComponent,
],
imports: [
FormsModule,
],
providers: [
NgForm,
]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(IntlTelInputComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should convert phone number to E164 format', () => {
component.options = {
initialCountry: 'ch',
preferredCountries: ['ch'],
onlyCountries: ['ch', 'fr']
};
component.ngAfterViewInit();
component.phoneNumber = '0797703808';
expect(component.E164PhoneNumber).toBe('+41797703808');
});
it('should re-set E164 phone number on countryChange', () => {
component.options = {
initialCountry: 'ch',
preferredCountries: ['ch'],
onlyCountries: ['ch', 'fr']
};
component.ngAfterViewInit();
component.phoneNumber = '0797703808';
expect(component.E164PhoneNumber).toBe('+41797703808');
component.phoneNumber = '0681215656';
component.intlTelInput.setCountry('fr');
expect(component.E164PhoneNumber).toBe('+33681215656');
});
it('should add a label tag if label attribute is set', () => {
const labelText = 'label text';
component.label = labelText;
fixture.detectChanges();
const element = fixture
.debugElement
.query(By.css('label'))
.nativeElement;
expect(element).not.toBe(null);
expect(element.innerText).toBe(labelText);
});
it('should not add a label by default', () => {
const element = fixture
.debugElement
.query(By.css('label'));
expect(element).toBeNull();
});
it('should not have a css class by default for the label', () => {
component.label = 'label';
fixture.detectChanges();
const element = fixture
.debugElement
.query(By.css('label'))
.nativeElement;
expect(element.className).toBeFalsy();
});
it('should be possible to specify a css class for the label', () => {
component.label = 'label';
component.labelCssClass = 'label-css-class';
fixture.detectChanges();
const element = fixture
.debugElement
.query(By.css('label'))
.nativeElement;
expect(element.className).toContain(component.labelCssClass);
});
it('should set both required and aria-required if specified', () => {
component.required = true;
fixture.detectChanges();
const element: HTMLElement = fixture
.debugElement
.query(By.css('input'))
.nativeElement;
expect(element.getAttribute('required')).not.toBeNull();
expect(element.getAttribute('aria-required')).toBe('true');
});
it('should set a default name attribute if not specified', () => {
const element: HTMLElement = fixture
.debugElement
.query(By.css('input'))
.nativeElement;
expect(element.getAttribute('name')).toBe(component.name);
expect(element.getAttribute('name')).toBe(element.getAttribute('id'));
});
it('should set name and id to the same value', () => {
component.name = 'custom-name';
fixture.detectChanges();
const element: HTMLElement = fixture
.debugElement
.query(By.css('input'))
.nativeElement;
expect(element.getAttribute('name')).toBe(component.name);
expect(element.getAttribute('name')).toBe(element.getAttribute('id'));
});
it('should allow specifying a css class', () => {
const cssClass = 'my-css-class';
component.cssClass = cssClass;
fixture.detectChanges();
const element: HTMLElement = fixture
.debugElement
.query(By.css('input'))
.nativeElement;
expect(element.className).toContain(cssClass);
});
it('should be possible to set preferredCountries option', () => {
component.options = {
countrySearch: false,
preferredCountries: ['ch'],
onlyCountries: ['ch']
};
component.ngAfterViewInit();
fixture.detectChanges();
const element = fixture
.debugElement
.query(By.css('#intl-tel-input-name'))
.nativeElement
.parentNode
.querySelector('.iti__preferred');
expect(element.getAttribute('data-country-code')).toBe(component.options.onlyCountries?.[0]);
});
it('should be possible to set i18n option', () => {
const localizedCountryName = 'Suisse';
component.options = {
preferredCountries: ['ch'],
i18n: { ch: localizedCountryName },
onlyCountries: ['ch']
};
component.ngAfterViewInit();
fixture.detectChanges();
const element = fixture
.debugElement
.query(By.css('#intl-tel-input-name'))
.nativeElement
.parentNode
.querySelector('.iti__country-name');
expect(element.innerHTML).toBe(localizedCountryName);
});
});