Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
auumgn committed Oct 6, 2023
1 parent 363bb6e commit b428eb8
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 23 deletions.
155 changes: 138 additions & 17 deletions ui/src/app/account/login/login.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,144 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { LoginComponent } from './login.component';
import {
ComponentFixture,
TestBed,
tick,
fakeAsync,
} from '@angular/core/testing'
import { LoginComponent } from './login.component'
import { ReactiveFormsModule } from '@angular/forms'
import { RouterTestingModule } from '@angular/router/testing'
import { LoginService } from '../service/login.service'
import { StateStorageService } from '../service/state-storage.service'
import { AccountService } from '../service/account.service'
import { of, throwError } from 'rxjs'

describe('LoginComponent', () => {
let component: LoginComponent;
let fixture: ComponentFixture<LoginComponent>;
let component: LoginComponent
let fixture: ComponentFixture<LoginComponent>
let loginService: jasmine.SpyObj<LoginService>
let stateStorageService: jasmine.SpyObj<StateStorageService>
let accountService: jasmine.SpyObj<AccountService>

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ LoginComponent ]
})
.compileComponents();
beforeEach(() => {
const loginServiceSpy = jasmine.createSpyObj('LoginService', [
'login',
'logout',
])
const stateStorageServiceSpy = jasmine.createSpyObj('StateStorageService', [
'getUrl',
'storeUrl',
])
const accountServiceSpy = jasmine.createSpyObj('AccountService', [
'getAccountData',
])

fixture = TestBed.createComponent(LoginComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
TestBed.configureTestingModule({
declarations: [LoginComponent],
imports: [ReactiveFormsModule, RouterTestingModule],
providers: [
{ provide: LoginService, useValue: loginServiceSpy },
{ provide: StateStorageService, useValue: stateStorageServiceSpy },
{ provide: AccountService, useValue: accountServiceSpy },
],
}).compileComponents()

fixture = TestBed.createComponent(LoginComponent)
component = fixture.componentInstance
loginService = TestBed.inject(LoginService) as jasmine.SpyObj<LoginService>
stateStorageService = TestBed.inject(
StateStorageService
) as jasmine.SpyObj<StateStorageService>
accountService = TestBed.inject(
AccountService
) as jasmine.SpyObj<AccountService>
})

it('should create', () => {
expect(component).toBeTruthy();
});
});
expect(component).toBeTruthy()
})

it('should call loginService.login and handle successful login', fakeAsync(() => {
const mockLoginResult = { mfaRequired: false }
loginService.login.and.returnValue(of(mockLoginResult))
accountService.getAccountData.and.returnValue(
of({
activated: true,
authorities: ['test', 'test'],
email: '[email protected]',
firstName: 'name',
langKey: 'en',
lastName: 'surname',
imageUrl: 'url',
salesforceId: 'sfid',
loggedAs: false,
loginAs: 'sfid',
mainContact: false,
mfaEnabled: false,
})
)

component.loginForm.patchValue({
username: 'testuser',
password: 'testpassword',
mfaCode: '',
})

component.login()

tick() // Wait for Observable to emit

expect(component.showMfa).toBe(false)
expect(component.authenticationError).toBe(false)
}))

it('should handle MFA required', fakeAsync(() => {
const mockLoginResult = { mfaRequired: true }
loginService.login.and.returnValue(of(mockLoginResult))

component.loginForm.patchValue({
username: 'testuser',
password: 'testpassword',
mfaCode: '',
})

component.login()

tick() // Wait for Observable to emit

expect(component.showMfa).toBe(true)
}))

it('should handle MFA code error', fakeAsync(() => {
const mockLoginResult = { mfaRequired: true }
loginService.login.and.returnValue(of(mockLoginResult))

component.loginForm.patchValue({
username: 'testuser',
password: 'testpassword',
mfaCode: 'invalidCode',
})

component.login()

tick() // Wait for Observable to emit

expect(component.showMfa).toBe(true)
expect(component.mfaError).toBe(true)
}))

it('should handle login error', fakeAsync(() => {
loginService.login.and.returnValue(throwError('Error'))

component.loginForm.patchValue({
username: 'testuser',
password: 'testpassword',
mfaCode: '',
})

component.login()

tick() // Wait for Observable to emit

expect(component.authenticationError).toBe(true)
}))
})
7 changes: 1 addition & 6 deletions ui/src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,5 @@ describe('AppComponent', () => {
expect(app.title).toEqual('gateway');
});

it('should render title', () => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.nativeElement as HTMLElement;
expect(compiled.querySelector('.content span')?.textContent).toContain('gateway app is running!');
});

});

0 comments on commit b428eb8

Please sign in to comment.