Skip to content

Commit

Permalink
remaining users component tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bobcaprice committed Jan 24, 2024
1 parent ebee234 commit be41b71
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 13 deletions.
78 changes: 66 additions & 12 deletions ui/src/app/user/users.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<UsersComponent>
Expand Down Expand Up @@ -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 },
Expand All @@ -61,17 +62,9 @@ describe('UsersComponent', () => {
accountService = TestBed.inject(AccountService) as jasmine.SpyObj<AccountService>
eventService = TestBed.inject(EventService) as jasmine.SpyObj<EventService>
alertService = TestBed.inject(AlertService) as jasmine.SpyObj<AlertService>
})

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,
Expand All @@ -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('')
})
})
2 changes: 1 addition & 1 deletion ui/src/app/user/users.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class UsersComponent implements OnInit, OnDestroy {
totalItems: any
itemsPerPage: any
page: any
sortColumn: any
sortColumn: string = 'id'

Check failure on line 39 in ui/src/app/user/users.component.ts

View workflow job for this annotation

GitHub Actions / format

Type string trivially inferred from a string literal, remove type annotation
ascending: any
itemCount: string | null | undefined = null
searchTerm: string | null = null
Expand Down

0 comments on commit be41b71

Please sign in to comment.