-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create RouterService to be used centrally in components
- Using RouterService prevents the previously happening circular reference
- Loading branch information
Showing
13 changed files
with
207 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export enum RoutePath { | ||
Home = 'home', | ||
Login = 'login', | ||
Logout = 'logout', | ||
AccountSelection = 'accountselection', | ||
Dashboard = 'account/:name/dashboard', | ||
Overview = 'account/:name/overview', | ||
History = 'account/:name/history', | ||
Plan = 'account/:name/plan', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { RouterService } from './router.service'; | ||
import { Router } from '@angular/router'; | ||
import { RouterTestingModule } from '@angular/router/testing'; | ||
import { RoutePath } from '../enum/routepath'; | ||
|
||
describe('RouterService', () => { | ||
let service: RouterService; | ||
let mockRouter: Router; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [RouterTestingModule.withRoutes([])], | ||
}).compileComponents(); | ||
|
||
mockRouter = TestBed.inject(Router); | ||
service = TestBed.inject(RouterService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
|
||
describe('getUrl', () => { | ||
it('should return the current URL when getURL is called', () => { | ||
const routerService = new RouterService(mockRouter); | ||
const mockUrl = '/home'; | ||
jest.spyOn(mockRouter, 'url', 'get').mockReturnValue(mockUrl); | ||
const result = routerService.getURL(); | ||
expect(result).toBe(mockUrl); | ||
}); | ||
}); | ||
|
||
describe('navigateToRoute', () => { | ||
it('should navigate to home route', () => { | ||
const routerService = new RouterService(mockRouter); | ||
const navigateSpy = jest.spyOn(routerService.router, 'navigate'); | ||
routerService.navigateToRoute(RoutePath.Home); | ||
expect(navigateSpy).toHaveBeenCalledWith([RoutePath.Home]); | ||
}); | ||
}); | ||
|
||
describe('navigateToRouteForAccountName', () => { | ||
it('should navigate to the correct route', () => { | ||
const routerService = new RouterService(mockRouter); | ||
const mockPath = RoutePath.Dashboard; | ||
const mockAccountName = 'testAccount'; | ||
const mockTarget = 'account/testAccount/dashboard'; | ||
const navigateSpy = jest.spyOn(mockRouter, 'navigate'); | ||
|
||
routerService.navigateToRouteForAccountName( | ||
mockPath, | ||
mockAccountName | ||
); | ||
|
||
expect(navigateSpy).toHaveBeenCalledWith([mockTarget]); | ||
}); | ||
|
||
it('should not allow an empty account name', () => { | ||
const routerService = new RouterService(mockRouter); | ||
const mockPath = RoutePath.Dashboard; | ||
const mockAccountName = ''; | ||
|
||
expect(() => { | ||
routerService.navigateToRouteForAccountName( | ||
mockPath, | ||
mockAccountName | ||
); | ||
}).toThrow(); | ||
}); | ||
}); | ||
|
||
describe('navigateToDashboard', () => { | ||
it('should navigate to dashboard route for a given account name', () => { | ||
const routerService = new RouterService(mockRouter); | ||
const mockAccountName = 'testAccount'; | ||
|
||
const navigateSpy = jest.spyOn( | ||
routerService, | ||
'navigateToRouteForAccountName' | ||
); | ||
|
||
routerService.navigateToDashboard(mockAccountName); | ||
|
||
expect(navigateSpy).toHaveBeenCalledWith( | ||
RoutePath.Dashboard, | ||
mockAccountName | ||
); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.