Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: TTA-115 create unit test for function checkTypeError in sta…
…tus network service
  • Loading branch information
Abigail Cabascango committed Oct 6, 2022
commit 07809f536fb5c715c24ec510a8e25994605ac0d3
3 changes: 2 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ import { FastDirective} from './modules/internet-connection-status/internet-conn
import { SlowDirective} from './modules/internet-connection-status/internet-connection-directives/slow.directive';
import { OfflineDirective} from './modules/internet-connection-status/internet-connection-directives/offline.directive';
import { ConnectionDirective } from './modules/internet-connection-status/internet-connection-directives/connection.directive';

import { ToastrService } from 'ngx-toastr';

const maskConfig: Partial<IConfig> = {
validation: false,
Expand Down Expand Up @@ -219,6 +219,7 @@ const maskConfig: Partial<IConfig> = {
},
DatePipe,
CookieService,
ToastrService,
{provide: Window, useValue: window}
],
bootstrap: [AppComponent],
Expand Down
25 changes: 21 additions & 4 deletions src/app/modules/shared/services/status-network.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TestBed } from '@angular/core/testing';
import * as assert from 'assert';
import { catchError, map, mergeMap } from 'rxjs/operators';

import { IndividualConfig, ToastrService } from 'ngx-toastr';
import { StatusNetworkService } from './status-network.service';

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

beforeEach(() => {
TestBed.configureTestingModule({});
TestBed.configureTestingModule({
providers: [ToastrService, {
provide: ToastrService, useValue: toastrServiceStub
}]
});
service = TestBed.inject(StatusNetworkService);
});

fit('checkTypeError is error', () => {
const errorType: ErrorType = {error: catchError, message: 'javascript', isError: true};
spyOn(toastrServiceStub, 'error');
service.checkTypeError(errorType)
expect(toastrServiceStub.error).toHaveBeenCalled();
});

fit('checkTypeError is warning', () => {
assert(service.checkTypeError(errorType) == 'is warning');
const errorType: ErrorType = {error: catchError, message: 'javascript', isError: false};
spyOn(toastrServiceStub, 'warning');
service.checkTypeError(errorType)
expect(toastrServiceStub.warning).toHaveBeenCalled();
});
});
5 changes: 1 addition & 4 deletions src/app/modules/shared/services/status-network.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@ export class StatusNetworkService {
if ((effectiveTypenetwork.effectiveType != '2g')){
if(!isError){
this.toastrService.warning(message);
return 'is warning';
}
if(isError){
const errorMessa = error.error && error.error.message? error.error.message: 'There is an error with the server, your request have not be completed';
const errorMessa = error.error && error.error.message? error.error.message: 'There is an error with the server, your request have not be completed';
this.toastrService.error(errorMessa);
return 'is error';
}
}
this.toastrService.warning('Your request was not completed, your connection is slow');
return 'is warning';
}
}