Skip to content

Commit

Permalink
Improve code coverage #1548 - Added tests to hash service
Browse files Browse the repository at this point in the history
  • Loading branch information
HarelM committed Aug 3, 2023
1 parent 75e8bc3 commit 7dc99db
Showing 1 changed file with 106 additions and 12 deletions.
118 changes: 106 additions & 12 deletions IsraelHiking.Web/src/application/services/hash.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Router } from "@angular/router";
import { Router, UrlTree } from "@angular/router";
import { NgxsModule, Store } from "@ngxs/store";
import { TestBed, inject } from "@angular/core/testing";
import { RouterTestingModule } from "@angular/router/testing";
import { Subject } from "rxjs";

import { HashService } from "./hash.service";
import { HashService, RouteStrings } from "./hash.service";
import { Urls } from "../urls";
import { MapService } from "./map.service";

Expand All @@ -19,15 +19,109 @@ describe("HashService", () => {
imports: [RouterTestingModule, NgxsModule.forRoot([])],
providers: [
{ provide: Router, useValue: routerMock },
{ provide: MapService, useValue: null },
{ provide: MapService, useValue: {} },
HashService
]
});
});

it("Should not reset address bar if sidebar is open", inject([HashService, Router, Store],
(service: HashService, routerMock: Router, store: Store) => {
const spy = jasmine.createSpy();
routerMock.navigate = spy;
store.reset({
poiState: {
isSidebarOpen: true
}
});

service.resetAddressbar();

expect(spy).not.toHaveBeenCalled();
}));

it("Should navigate to share url if it stored in the state", inject([HashService, Router, Store],
(service: HashService, routerMock: Router, store: Store) => {
const spy = jasmine.createSpy();
routerMock.navigate = spy;
store.reset({
poiState: {
isSidebarOpen: false
},
inMemoryState: {
shareUrl: {}
}
});

service.resetAddressbar();

expect(spy).toHaveBeenCalled();
expect(spy.calls.all()[0].args[0][0]).toBe(RouteStrings.ROUTE_SHARE);
}));

it("Should navigate to file url if it stored in the state", inject([HashService, Router, Store],
(service: HashService, routerMock: Router, store: Store) => {
const spy = jasmine.createSpy();
routerMock.navigate = spy;
store.reset({
poiState: {
isSidebarOpen: false
},
inMemoryState: {
fileUrl: {},
baseLayer: "baseLayer"
}
});

service.resetAddressbar();

expect(spy).toHaveBeenCalled();
expect(spy.calls.all()[0].args[0][0]).toBe(RouteStrings.ROUTE_URL);
}));

it("Should not navigate to location if the map is moving", inject([HashService, Router, Store, MapService],
(service: HashService, routerMock: Router, store: Store, mapService: MapService) => {
const spy = jasmine.createSpy();
routerMock.navigate = spy;
store.reset({
poiState: {
isSidebarOpen: false
},
inMemoryState: {}
});
mapService.map = { isMoving: () => true } as any;

service.resetAddressbar();

expect(spy).not.toHaveBeenCalled();
}));

it("Should navigate to location if the map is not moving", inject([HashService, Router, Store, MapService],
(service: HashService, routerMock: Router, store: Store, mapService: MapService) => {
const spy = jasmine.createSpy();
routerMock.navigate = spy;
store.reset({
poiState: {
isSidebarOpen: false
},
inMemoryState: {},
locationState: {
zoom: 1,
latitude: 2,
longitude: 3
}
});
mapService.map = { isMoving: () => false } as any;

service.resetAddressbar();

expect(spy).toHaveBeenCalled();
expect(spy.calls.all()[0].args[0][0]).toBe(RouteStrings.ROUTE_MAP);
}));

it("Should return map address",
inject([HashService, Router, Store], (hashService: HashService, routerMock: any, store: Store) => {
routerMock.createUrlTree = (arr: []) => arr.join("/");
inject([HashService, Router, Store], (service: HashService, routerMock: Router, store: Store) => {
routerMock.createUrlTree = (arr: []) => arr.join("/") as any as UrlTree;
store.reset({
inMemoryState: {},
locationState: {
Expand All @@ -37,29 +131,29 @@ describe("HashService", () => {
}
});

const href = hashService.getHref();
const href = service.getHref();

expect(href).toBe(Urls.baseAddress + "map/2.00/2.000000/3.000000");
}));

it("Should return share url",
inject([HashService, Router, Store], (hashService: HashService, routerMock: any, store: Store) => {
inject([HashService, Router, Store], (service: HashService, routerMock: Router, store: Store) => {

routerMock.createUrlTree = () => "share-address";
routerMock.createUrlTree = () => "share-address" as any as UrlTree;
store.reset({
inMemoryState: {
shareUrl: { id: "1" }
}
});

const href = hashService.getHref();
const href = service.getHref();

expect(href).toBe(Urls.baseAddress + "share-address");
}));

it("Should return external url",
inject([HashService, Router, Store], (hashService: HashService, routerMock: any, store: Store) => {
routerMock.createUrlTree = (array: [], options: any) => "file-address?" + options.queryParams.baselayer;
inject([HashService, Router, Store], (service: HashService, routerMock: Router, store: Store) => {
routerMock.createUrlTree = (array: [], options: any) => "file-address?" + options.queryParams.baselayer as any as UrlTree;
store.reset({
inMemoryState: {
fileUrl: "fileUrl"
Expand All @@ -68,7 +162,7 @@ describe("HashService", () => {
selectedBaseLayerKey: "base-layer"
}
});
const href = hashService.getHref();
const href = service.getHref();

expect(href).toBe(Urls.baseAddress + "file-address?base-layer");
}));
Expand Down

0 comments on commit 7dc99db

Please sign in to comment.