generated from amosproj/amos202Xss0Y-projname
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #251 from amosproj/feat/xd-101
test: Add frontend tests (XD-101)
- Loading branch information
Showing
27 changed files
with
687 additions
and
252 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { ActivatedRoute, NavigationEnd, Router, RouterEvent } from '@angular/router'; | ||
import { AuthenticationService, LocalStorageService } from 'common-frontend-models'; | ||
import { ReplaySubject } from 'rxjs'; | ||
|
||
import { HeaderComponent } from './header.component'; | ||
|
@@ -12,73 +13,100 @@ const HEADER_ROUTES = { | |
} | ||
}, | ||
}, | ||
root: { | ||
snapshot: { | ||
data: { | ||
breadcrumb: 'Layer 1', | ||
}, | ||
}, | ||
firstChild: { | ||
snapshot: { | ||
data: { | ||
breadcrumb: 'Layer 2', | ||
}, | ||
}, | ||
}, | ||
}, | ||
root: { | ||
snapshot: { | ||
data: { | ||
breadcrumb: 'Layer 1', | ||
}, | ||
}, | ||
firstChild: { | ||
snapshot: { | ||
data: { | ||
breadcrumb: 'Layer 2', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
describe('HeaderComponent', () => { | ||
let component: HeaderComponent; | ||
let fixture: ComponentFixture<HeaderComponent>; | ||
const eventsSubject = new ReplaySubject<RouterEvent>(1); | ||
let routerMock: Router; | ||
|
||
beforeEach(async () => { | ||
routerMock = { | ||
events: eventsSubject.asObservable(), | ||
let component: HeaderComponent; | ||
let fixture: ComponentFixture<HeaderComponent>; | ||
const eventsSubject = new ReplaySubject<RouterEvent>(1); | ||
let routerMock: Router; | ||
let localStorageServiceMock: LocalStorageService; | ||
let authenticationServiceMock: AuthenticationService; | ||
|
||
beforeEach(async () => { | ||
routerMock = { | ||
events: eventsSubject.asObservable(), | ||
url: '/Layer1/Layer2', | ||
} as unknown as Router; | ||
|
||
await TestBed.configureTestingModule({ | ||
imports: [ HeaderComponent ], | ||
providers: [ | ||
{ | ||
provide: ActivatedRoute, | ||
useValue: HEADER_ROUTES, | ||
}, | ||
{ | ||
provide: Router, | ||
useValue: routerMock, | ||
}, | ||
], | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(HeaderComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
it('should trigger router events correctly', () => { | ||
eventsSubject.next(new NavigationEnd(1, '', '')); | ||
|
||
const routerEvents = component.routerEvents(); | ||
expect(routerEvents).toEqual({ | ||
id: 1, | ||
type: 1, | ||
url: '', | ||
urlAfterRedirects: '', | ||
} as NavigationEnd); | ||
}); | ||
|
||
it('should compute breadcrumbs correctly', () => { | ||
eventsSubject.next(new NavigationEnd(1, '', '')); | ||
|
||
const breadcrumbs = component.breadcrumbs(); | ||
expect(breadcrumbs).toEqual([ 'Layer 1', 'Layer 2' ]); | ||
}); | ||
navigate: jest.fn() | ||
} as unknown as Router; | ||
|
||
localStorageServiceMock = { | ||
getOrCreate: jest.fn().mockReturnValue(jest.fn().mockReturnValue('theme-classic-dark')), | ||
set: jest.fn() | ||
} as unknown as LocalStorageService; | ||
|
||
authenticationServiceMock = { | ||
getUserMail: jest.fn().mockReturnValue('[email protected]'), | ||
logout: jest.fn() | ||
} as unknown as AuthenticationService; | ||
|
||
await TestBed.configureTestingModule({ | ||
imports: [ HeaderComponent ], | ||
providers: [ | ||
{ provide: ActivatedRoute, useValue: HEADER_ROUTES }, | ||
{ provide: Router, useValue: routerMock }, | ||
{ provide: LocalStorageService, useValue: localStorageServiceMock }, | ||
{ provide: AuthenticationService, useValue: authenticationServiceMock }, | ||
], | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(HeaderComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
it('should trigger router events correctly', () => { | ||
eventsSubject.next(new NavigationEnd(1, '', '')); | ||
|
||
const routerEvents = component.routerEvents(); | ||
expect(routerEvents).toBeInstanceOf(NavigationEnd); | ||
}); | ||
|
||
it('should compute breadcrumbs correctly', () => { | ||
eventsSubject.next(new NavigationEnd(1, '', '')); | ||
|
||
const breadcrumbs = component.breadcrumbs(); | ||
expect(breadcrumbs).toEqual([ 'Layer 1', 'Layer 2' ]); | ||
}); | ||
|
||
it('should determine if it is home page correctly', () => { | ||
eventsSubject.next(new NavigationEnd(1, '', '')); | ||
|
||
const isHomePage = component.isHomePage(); | ||
expect(isHomePage).toBe(true); | ||
}); | ||
|
||
it('should toggle theme mode', () => { | ||
component.toggleMode(); | ||
expect(localStorageServiceMock.set).toHaveBeenCalledWith('theme', 'theme-classic-light'); | ||
}); | ||
|
||
it('should logout and navigate to login', () => { | ||
component.logout(); | ||
expect(authenticationServiceMock.logout).toHaveBeenCalled(); | ||
expect(routerMock.navigate).toHaveBeenCalledWith([ '/account/login' ]); | ||
}); | ||
|
||
it('should cut URL correctly', () => { | ||
const cutUrl = component.cutUrl(1); | ||
expect(cutUrl).toBe('/Layer1'); | ||
}); | ||
}); |
63 changes: 49 additions & 14 deletions
63
libs/account/frontend/view/src/lib/components/login/login.page.spec.ts
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 |
---|---|---|
@@ -1,22 +1,57 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { Router } from '@angular/router'; | ||
import { AuthenticationService } from 'common-frontend-models'; | ||
import { of } from 'rxjs'; | ||
|
||
import { LoginPage } from './login.page'; | ||
|
||
describe('LoginComponent', () => { | ||
let component: LoginPage; | ||
let fixture: ComponentFixture<LoginPage>; | ||
describe('LoginPage', () => { | ||
let component: LoginPage; | ||
let fixture: ComponentFixture<LoginPage>; | ||
let authServiceMock: jest.Mocked<AuthenticationService>; | ||
let routerMock: jest.Mocked<Router>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [ LoginPage ], | ||
}).compileComponents(); | ||
beforeEach(async () => { | ||
authServiceMock = { | ||
login: jest.fn().mockReturnValue(of(true)), | ||
} as unknown as jest.Mocked<AuthenticationService>; | ||
|
||
fixture = TestBed.createComponent(LoginPage); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
routerMock = { | ||
navigate: jest.fn(), | ||
} as unknown as jest.Mocked<Router>; | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
await TestBed.configureTestingModule({ | ||
imports: [ LoginPage ], | ||
providers: [ | ||
{ provide: AuthenticationService, useValue: authServiceMock }, | ||
{ provide: Router, useValue: routerMock }, | ||
], | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(LoginPage); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
describe('onSubmit', () => { | ||
it('should not validate form if email or password is invalid', () => { | ||
component.email = 'invalid-email'; | ||
component.password = ''; | ||
component.onSubmit(); | ||
|
||
expect(routerMock.navigate).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should validate form and login successfully', () => { | ||
component.email = '[email protected]'; | ||
component.password = 'password'; | ||
component.onSubmit(); | ||
|
||
expect(routerMock.navigate).toHaveBeenCalledWith([ '/' ]); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.