Skip to content

Commit 07809f5

Browse files
author
Abigail Cabascango
committed
refactor: TTA-115 create unit test for function checkTypeError in status network service
1 parent 4665966 commit 07809f5

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

src/app/app.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ import { FastDirective} from './modules/internet-connection-status/internet-conn
100100
import { SlowDirective} from './modules/internet-connection-status/internet-connection-directives/slow.directive';
101101
import { OfflineDirective} from './modules/internet-connection-status/internet-connection-directives/offline.directive';
102102
import { ConnectionDirective } from './modules/internet-connection-status/internet-connection-directives/connection.directive';
103-
103+
import { ToastrService } from 'ngx-toastr';
104104

105105
const maskConfig: Partial<IConfig> = {
106106
validation: false,
@@ -219,6 +219,7 @@ const maskConfig: Partial<IConfig> = {
219219
},
220220
DatePipe,
221221
CookieService,
222+
ToastrService,
222223
{provide: Window, useValue: window}
223224
],
224225
bootstrap: [AppComponent],
Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { TestBed } from '@angular/core/testing';
22
import * as assert from 'assert';
33
import { catchError, map, mergeMap } from 'rxjs/operators';
4-
4+
import { IndividualConfig, ToastrService } from 'ngx-toastr';
55
import { StatusNetworkService } from './status-network.service';
66

77
export interface ErrorType {
@@ -11,14 +11,31 @@ export interface ErrorType {
1111
}
1212
describe('StatusNetworkService', () => {
1313
let service: StatusNetworkService;
14-
const errorType: ErrorType = {error: catchError, message: 'javascript', isError: false};
14+
const toastrServiceStub = {
15+
error: (message?: string, title?: string, override?: Partial<IndividualConfig>) => { },
16+
warning: (message?: string, title?: string, override?: Partial<IndividualConfig>) => { }
17+
};
1518

1619
beforeEach(() => {
17-
TestBed.configureTestingModule({});
20+
TestBed.configureTestingModule({
21+
providers: [ToastrService, {
22+
provide: ToastrService, useValue: toastrServiceStub
23+
}]
24+
});
1825
service = TestBed.inject(StatusNetworkService);
1926
});
2027

28+
fit('checkTypeError is error', () => {
29+
const errorType: ErrorType = {error: catchError, message: 'javascript', isError: true};
30+
spyOn(toastrServiceStub, 'error');
31+
service.checkTypeError(errorType)
32+
expect(toastrServiceStub.error).toHaveBeenCalled();
33+
});
34+
2135
fit('checkTypeError is warning', () => {
22-
assert(service.checkTypeError(errorType) == 'is warning');
36+
const errorType: ErrorType = {error: catchError, message: 'javascript', isError: false};
37+
spyOn(toastrServiceStub, 'warning');
38+
service.checkTypeError(errorType)
39+
expect(toastrServiceStub.warning).toHaveBeenCalled();
2340
});
2441
});

src/app/modules/shared/services/status-network.service.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,12 @@ export class StatusNetworkService {
2222
if ((effectiveTypenetwork.effectiveType != '2g')){
2323
if(!isError){
2424
this.toastrService.warning(message);
25-
return 'is warning';
2625
}
2726
if(isError){
28-
const errorMessa = error.error && error.error.message? error.error.message: 'There is an error with the server, your request have not be completed';
27+
const errorMessa = error.error && error.error.message? error.error.message: 'There is an error with the server, your request have not be completed';
2928
this.toastrService.error(errorMessa);
30-
return 'is error';
3129
}
3230
}
3331
this.toastrService.warning('Your request was not completed, your connection is slow');
34-
return 'is warning';
3532
}
3633
}

0 commit comments

Comments
 (0)