From be41b719a8f18dc5607a676454c21a3a60f1607f Mon Sep 17 00:00:00 2001 From: George Nash Date: Wed, 24 Jan 2024 08:27:37 +0000 Subject: [PATCH] remaining users component tests --- ui/src/app/user/users.component.spec.ts | 78 +++++++++++++++++++++---- ui/src/app/user/users.component.ts | 2 +- 2 files changed, 67 insertions(+), 13 deletions(-) diff --git a/ui/src/app/user/users.component.spec.ts b/ui/src/app/user/users.component.spec.ts index 31b89d7a1..88daeb1ee 100644 --- a/ui/src/app/user/users.component.spec.ts +++ b/ui/src/app/user/users.component.spec.ts @@ -17,6 +17,7 @@ import { EventService } from '../shared/service/event.service' import { LocalizePipe } from '../shared/pipe/localize' import { EventType } from 'src/app/app.constants' import { Event } from '../shared/model/event.model' +import { RouterModule } from '@angular/router' describe('UsersComponent', () => { let component: UsersComponent let fixture: ComponentFixture @@ -46,7 +47,7 @@ describe('UsersComponent', () => { TestBed.configureTestingModule({ declarations: [UsersComponent, HasAnyAuthorityDirective, LocalizePipe], - imports: [ReactiveFormsModule, RouterTestingModule, FormsModule], + imports: [ReactiveFormsModule, RouterModule.forRoot([{ path: 'users', component: UsersComponent }]), FormsModule], providers: [ { provide: UserService, useValue: userServiceSpy }, { provide: AccountService, useValue: accountServiceSpy }, @@ -61,17 +62,9 @@ describe('UsersComponent', () => { accountService = TestBed.inject(AccountService) as jasmine.SpyObj eventService = TestBed.inject(EventService) as jasmine.SpyObj alertService = TestBed.inject(AlertService) as jasmine.SpyObj - }) - fit('should create', () => { - expect(component).toBeTruthy() - }) - - fit('should call load all on init', fakeAsync(() => { - const headers = new HttpHeaders().append('link', 'link;link') userService.query.and.returnValue(of(new UserPage([new User('123')], 1))) - accountService.hasAnyAuthority.and.returnValue(true) - eventService.on.and.returnValue(EMPTY) + accountService.getAccountData.and.returnValue( of({ activated: true, @@ -88,11 +81,72 @@ describe('UsersComponent', () => { mfaEnabled: false, }) ) - fixture.detectChanges() - tick() + + accountService.hasAnyAuthority.and.returnValue(true) + eventService.on.and.returnValue(EMPTY) + }) + + it('should create', () => { + expect(component).toBeTruthy() + }) + + it('should call load all on init', fakeAsync(() => { component.ngOnInit() expect(userService.query).toHaveBeenCalled() expect(component.users![0]).toEqual(jasmine.objectContaining({ id: '123' })) })) + + it('should load a page', () => { + component.page = 1 + component.loadPage() + + expect(userService.query).toHaveBeenCalled() + expect(component.users![0]).toEqual(jasmine.objectContaining({ id: '123' })) + }) + + it('sort should be id,desc by default', () => { + const result = component.sort() + expect(result).toEqual(['id,desc']) + }) + + it('direction should be desc and id should be secondary sort column by default', () => { + component.sortColumn = 'name' + const result = component.sort() + expect(result).toEqual(['name,desc', 'id']) + }) + + it('updating sort column to different value should maintain sort direction', () => { + component.sortColumn = 'name' + let result = component.sort() + expect(result).toEqual(['name,desc', 'id']) + + component.updateSort('email') + result = component.sort() + expect(result).toEqual(['email,desc', 'id']) + }) + + it('updating sort column with same value should flip sort direction', () => { + component.sortColumn = 'name' + let result = component.sort() + expect(result).toEqual(['name,desc', 'id']) + + component.updateSort('name') + result = component.sort() + expect(result).toEqual(['name,asc', 'id']) + }) + + it('clear should reset page to zero', () => { + component.page = 10 + component.clear() + expect(component.page).toEqual(0) + }) + + it('reset search should clear search term', () => { + component.searchTerm = 'what the user typed' + component.submittedSearchTerm = 'what the user typed' + component.resetSearch() + expect(component.searchTerm).toEqual('') + expect(component.submittedSearchTerm).toEqual('') + }) }) diff --git a/ui/src/app/user/users.component.ts b/ui/src/app/user/users.component.ts index e0ed9896f..a8d7150de 100644 --- a/ui/src/app/user/users.component.ts +++ b/ui/src/app/user/users.component.ts @@ -36,7 +36,7 @@ export class UsersComponent implements OnInit, OnDestroy { totalItems: any itemsPerPage: any page: any - sortColumn: any + sortColumn: string = 'id' ascending: any itemCount: string | null | undefined = null searchTerm: string | null = null