forked from jebaGem/AngularRedux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch-customer.component.spec.ts
226 lines (204 loc) · 8.01 KB
/
search-customer.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import {
async,
ComponentFixture,
TestBed
} from '@angular/core/testing';
import { SearchCustomerComponent } from './search-customer.component';
import { FormBuilder, ReactiveFormsModule, FormGroup, FormControl } from '@angular/forms';
import { TranslationKeys } from '../shared/translation/translation-keys';
import { HttpClientModule, HttpClient } from '@angular/common/http';
import {
TranslateModule,
TranslateLoader,
TranslateService
} from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { BrowserModule } from '@angular/platform-browser';
import { NgReduxTestingModule, MockNgRedux } from '@angular-redux/store/testing';
import { NgReduxModule } from '@angular-redux/store';
import {
EuppConfigureTranslateService
} from '../shared/translation/eupp-configure-translate.service';
import { SearchCustomerActions } from './search-customer.actions';
import { MockTranslateService } from '../shared/mocks/translate-service.mock';
import { SharedModule } from '../shared/shared.module';
import { SearchLoadingState } from './search-customer.state';
import { IRootState } from '../store/root.state';
import { StringConstants } from '../shared/string-constants';
const using = require('jasmine-data-provider');
export function createTranslateLoader(http: HttpClient) {
return new TranslateHttpLoader(http, './assets/data/translations/', '.json');
}
describe('SearchCustomerComponent', () => {
let component: SearchCustomerComponent;
let fixture: ComponentFixture<SearchCustomerComponent>;
beforeEach(
async(() => {
const euppConfigureTranslateServiceStub = {
getTranslateKeyValue(key: string): string {
return 'translated';
}
};
const actionSpies = jasmine.createSpyObj('SearchCustomerActions', [
'setSearchResults'
]);
const translateServiceStub = new MockTranslateService();
const translationKeysStub = {};
const searchModel = {
key: null,
by: { value: 'id', disabled: true },
customer: '',
consentChecked: false,
houseNumber: null
};
TestBed.configureTestingModule({
declarations: [SearchCustomerComponent],
imports: [
ReactiveFormsModule,
HttpClientModule,
BrowserModule,
NgReduxModule,
NgReduxTestingModule,
SharedModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: createTranslateLoader,
deps: [HttpClient]
}
})
],
providers: [
{ provide: TranslationKeys, useValue: translationKeysStub },
{ provide: TranslateService, useValue: translateServiceStub },
{ provide: EuppConfigureTranslateService, useValue: euppConfigureTranslateServiceStub },
FormBuilder,
SearchCustomerActions
]
}).compileComponents()
.then(() => {
fixture = TestBed.createComponent(SearchCustomerComponent);
component = fixture.componentInstance;
component.searchForm = new FormGroup({
'key': new FormControl(searchModel.key),
'by': new FormControl(searchModel.by),
'customer': new FormControl(searchModel.customer),
'houseNumber': new FormControl(searchModel.houseNumber),
'consentChecked': new FormControl(searchModel.consentChecked)
});
component.searchForm.patchValue({ by: 'id' });
// Redux clean up
MockNgRedux.reset();
MockNgRedux.getSelectorStub((state: IRootState) => state.appReducer.locale)
.next('se');
MockNgRedux.getSelectorStub((state: IRootState) => state.appReducer.site)
.next('se');
MockNgRedux.getSelectorStub((state: IRootState) => state.searchCustomerReducer)
.next({
searchKey: '',
searchType: 'id',
searchResult: [],
showSelectCustomerTooltip: false,
consentSelected: false,
showConsentToolTip: false,
loadingState: SearchLoadingState.loading
});
MockNgRedux.getSelectorStub((state: IRootState) => state.appReducer.accountData)
.next({
id: String(),
name: String(),
upn: String()
});
MockNgRedux.getSelectorStub((state: IRootState) =>
state.searchCustomerReducer.loadingState)
.next(SearchLoadingState.complete);
// mock for the cosent tooltip
MockNgRedux.getSelectorStub((state: IRootState) =>
state.searchCustomerReducer.consentSelected)
.next(false);
MockNgRedux.getSelectorStub((state: IRootState) =>
state.searchCustomerReducer.showConsentToolTip).next(false);
fixture.detectChanges();
});
})
);
it('should create', () => {
expect(component).toBeTruthy();
});
it('should have locale se', () => {
component.locale$.subscribe(locale => {
expect(component.locale).toEqual('se');
});
});
using(['12345678', '12345678901'], function (COC) {
it('should render the form invalid if the COC number is invalid', function () {
// act
component.searchForm.patchValue({ key: COC });
// assert
component.searchKey$.subscribe(key => {
expect(component.searchForm.valid).toBe(false);
});
});
});
using(['1234567890', '123456789012', '123456-7890 ', '12345678-9012'], function (COC) {
it('should render the form valid if the COC number is valid', function () {
// act
component.searchForm.patchValue({ key: COC });
// assert
component.searchKey$.subscribe(key => {
expect(component.searchForm.valid).toBe(true);
});
});
});
it('should return a validation rule if the COC number is invalid', () => {
// act
component.searchForm.patchValue({ key: '12345678' });
// assert
component.searchKey$.subscribe(key => {
expect(component.key.errors.inputLength).toBeTruthy();
expect(component.key.errors.inputLength).toContain('10');
expect(component.key.errors.inputLength).toContain('12');
});
});
it('should display a warning message if the entered COC is invalid', function () {
// arrange
fixture.detectChanges();
const compiled = fixture.nativeElement;
const searchInput: HTMLInputElement = compiled.querySelector('.search-input input');
// act
component.searchForm.patchValue({ key: '12345678901' });
searchInput.dispatchEvent(new Event('blur'));
fixture.detectChanges();
// assert
component.searchKey$.subscribe(key => {
expect(compiled.querySelector('.validation')).toBeTruthy();
});
});
it('should display a warning message if the entered Post Code is invalid', function () {
// arrange
fixture.detectChanges();
const compiled = fixture.nativeElement;
const searchInput: HTMLInputElement = compiled.querySelector('.search-input input');
component.searchForm.patchValue({ by: 'postalCode' });
component.searchForm.patchValue({ houseNumber: '1234' });
// act
component.searchForm.patchValue({ key: '1067' });
searchInput.dispatchEvent(new Event('blur'));
fixture.detectChanges();
// assert
component.searchKey$.subscribe(key => {
expect(compiled.querySelector('.validation')).toBeTruthy();
});
});
using(['1034LH', '5089LH', '5089LH ', '5643MT'], function (postalCode) {
it('Check the valid postcode', function () {
// act
component.searchForm.patchValue({ by: 'postalCode' });
component.searchForm.patchValue({ key: postalCode });
// assert
component.searchKey$.subscribe(key => {
expect(component.searchForm.valid).toBe(true);
});
});
});
});