Skip to content

Commit 4665966

Browse files
author
Abigail Cabascango
committed
refactor: TTA-115 the if clausses in status network service and connection directive and their component
1 parent da0522a commit 4665966

File tree

4 files changed

+39
-18
lines changed

4 files changed

+39
-18
lines changed

src/app/modules/internet-connection-status/internet-connection-directives/connection.directive.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,21 @@ export class ConnectionDirective implements OnInit {
1515

1616
ngOnInit() {
1717
const { effectiveType } = navigator.connection;
18-
let src;
18+
let networkSatus;
19+
if (!(/\fast-5g|3g|4g/.test(effectiveType)) || !(/\slow-2g|2g/.test(effectiveType))){
20+
networkSatus = this.offlineSrc;
21+
return
22+
}
23+
1924
if (/\fast-5g|3g|4g/.test(effectiveType)) {
20-
src = this.fastSrc;
21-
}else if (/\slow-2g|2g/.test(effectiveType)) {
22-
src = this.slowSrc;
23-
}else {
24-
src = this.offlineSrc;
25+
networkSatus = this.fastSrc;
2526
}
26-
this.host.nativeElement.setAttribute('src', src);
27+
28+
if (/\slow-2g|2g/.test(effectiveType)) {
29+
networkSatus = this.slowSrc;
30+
}
31+
32+
this.host.nativeElement.setAttribute('src', networkSatus);
2733
}
2834

2935
}

src/app/modules/internet-connection-status/internet-connection-status.component.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,19 @@ export class InternetConnectionStatusComponent implements OnInit {
6868
.subscribe((effectiveType: string) => {
6969

7070
this.connectionType = effectiveType;
71+
72+
if (!(/\fast-5g|3g|4g/.test(effectiveType)) || !(/\slow-2g|2g/.test(effectiveType))){
73+
this.toastrService.error('Your request was not completed, you are offline');
74+
this.isFast = false;
75+
}
7176

7277
if (/\fast-5g|3g|4g/.test(effectiveType)){
7378
this.isFast = true;
74-
}else if (/\slow-2g|2g/.test(effectiveType)) {
79+
}
80+
81+
if (/\slow-2g|2g/.test(effectiveType)) {
7582
this.toastrService.warning('Caution your connection is slow');
7683
this.isFast = false;
77-
} else {
78-
this.toastrService.error('Your request was not completed, you are offline');
79-
this.isFast = false;
8084
}
8185
});
8286
}
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
import { TestBed } from '@angular/core/testing';
2+
import * as assert from 'assert';
3+
import { catchError, map, mergeMap } from 'rxjs/operators';
24

35
import { StatusNetworkService } from './status-network.service';
46

7+
export interface ErrorType {
8+
error: any;
9+
message?: string;
10+
isError?: boolean;
11+
}
512
describe('StatusNetworkService', () => {
613
let service: StatusNetworkService;
14+
const errorType: ErrorType = {error: catchError, message: 'javascript', isError: false};
715

816
beforeEach(() => {
917
TestBed.configureTestingModule({});
1018
service = TestBed.inject(StatusNetworkService);
1119
});
1220

13-
fit('should be created', () => {
14-
expect(service).toBeTruthy();
21+
fit('checkTypeError is warning', () => {
22+
assert(service.checkTypeError(errorType) == 'is warning');
1523
});
1624
});

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@ export class StatusNetworkService {
1919
checkTypeError(dataError: ErrorType){
2020
const { isError = false, message = 'The server is disconnected', error} = dataError;
2121
const effectiveTypenetwork = navigator.connection;
22-
if(effectiveTypenetwork.effectiveType === '2g'){
23-
this.toastrService.warning('Your request was not completed, your connection is slow');
24-
}else{
22+
if ((effectiveTypenetwork.effectiveType != '2g')){
23+
if(!isError){
24+
this.toastrService.warning(message);
25+
return 'is warning';
26+
}
2527
if(isError){
2628
const errorMessa = error.error && error.error.message? error.error.message: 'There is an error with the server, your request have not be completed';
2729
this.toastrService.error(errorMessa);
28-
}else{
29-
this.toastrService.warning(message);
30+
return 'is error';
3031
}
3132
}
33+
this.toastrService.warning('Your request was not completed, your connection is slow');
34+
return 'is warning';
3235
}
3336
}

0 commit comments

Comments
 (0)