-
Notifications
You must be signed in to change notification settings - Fork 15
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 #1884 from rodekruis/test.unit-login-form-component
Test.unit login form component
- Loading branch information
Showing
4 changed files
with
120 additions
and
6 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -5,16 +5,20 @@ import { | |
import { provideHttpClientTesting } from '@angular/common/http/testing'; | ||
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; | ||
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; | ||
import { FormsModule } from '@angular/forms'; | ||
import { FormsModule, NgForm } from '@angular/forms'; | ||
import { RouterModule } from '@angular/router'; | ||
import { IonicModule } from '@ionic/angular'; | ||
import { PopoverController } from '@ionic/angular'; | ||
import { TranslateModule } from '@ngx-translate/core'; | ||
import { AuthService } from 'src/app/auth/auth.service'; | ||
import { ForgotPasswordPopoverComponent } from 'src/app/components/forgot-password-popover/forgot-password-popover.component'; | ||
import { LoginFormComponent } from 'src/app/components/login-form/login-form.component'; | ||
|
||
describe('LoginFormComponent', () => { | ||
let component: LoginFormComponent; | ||
let fixture: ComponentFixture<LoginFormComponent>; | ||
let authService: AuthService; | ||
let popoverController: PopoverController; | ||
|
||
beforeEach(waitForAsync(() => { | ||
TestBed.configureTestingModule({ | ||
|
@@ -27,18 +31,105 @@ describe('LoginFormComponent', () => { | |
TranslateModule.forRoot(), | ||
], | ||
providers: [ | ||
{ provide: AuthService }, | ||
{ | ||
provide: AuthService, | ||
useValue: { | ||
login: jasmine.createSpy('login').and.returnValue({ | ||
add: jasmine | ||
.createSpy('add') | ||
.and.callFake((callback: () => void) => { | ||
callback(); | ||
}), | ||
}), | ||
}, | ||
}, | ||
{ | ||
provide: PopoverController, | ||
useValue: { | ||
create: jasmine | ||
.createSpy('create') | ||
.and.resolveTo({ present: jasmine.createSpy('present') }), | ||
}, | ||
}, | ||
provideHttpClient(withInterceptorsFromDi()), | ||
provideHttpClientTesting(), | ||
], | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(LoginFormComponent); | ||
component = fixture.componentInstance; | ||
authService = TestBed.inject(AuthService); | ||
popoverController = TestBed.inject(PopoverController); | ||
fixture.detectChanges(); | ||
})); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
expect(component.model.email).toBe(''); | ||
expect(component.model.password).toBe(''); | ||
expect(component.inputType).toBe('password'); | ||
expect(component.labelShow).toBe('Show password'); | ||
expect(component.labelHide).toBe('Hide password'); | ||
}); | ||
|
||
it('should call authService.login on onSubmit', () => { | ||
component.model.email = '[email protected]'; | ||
component.model.password = 'password'; | ||
component.onSubmit(); | ||
|
||
expect(authService.login).toHaveBeenCalledWith( | ||
'[email protected]', | ||
'password', | ||
); | ||
}); | ||
|
||
it('should reset form on successful login', () => { | ||
component.loginForm = { | ||
resetForm: jasmine.createSpy('resetForm') as () => void, | ||
} as NgForm; | ||
component.model.email = '[email protected]'; | ||
component.model.password = 'password'; | ||
component.onSubmit(); | ||
|
||
expect(component.loginForm.resetForm).toHaveBeenCalledWith(); | ||
}); | ||
|
||
it('should toggle inputType on toggleInputType', () => { | ||
component.inputType = 'password'; | ||
component.toggleInputType(); | ||
|
||
expect(component.inputType).toBe('text'); | ||
component.toggleInputType(); | ||
|
||
expect(component.inputType).toBe('password'); | ||
}); | ||
|
||
it('should present popover on presentPopover', async () => { | ||
await component.presentPopover(); | ||
const popoverOptions = { | ||
component: ForgotPasswordPopoverComponent, | ||
animated: true, | ||
cssClass: 'ibf-popover ibf-popover-normal', | ||
translucent: true, | ||
showBackdrop: true, | ||
}; | ||
|
||
expect(popoverController.create).toHaveBeenCalledWith(popoverOptions); | ||
|
||
const popover = await popoverController.create(popoverOptions); | ||
|
||
expect(popover.present).toHaveBeenCalledWith(); | ||
}); | ||
|
||
it('should return true if inputType is password', () => { | ||
component.inputType = 'password'; | ||
|
||
expect(component.isPassword()).toBeTrue(); | ||
}); | ||
|
||
it('should return false if inputType is not password', () => { | ||
component.inputType = 'text'; | ||
|
||
expect(component.isPassword()).toBeFalse(); | ||
}); | ||
}); |