Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryT-CG committed Dec 30, 2024
1 parent 3bd1b55 commit 40a4375
Show file tree
Hide file tree
Showing 14 changed files with 318 additions and 258 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,16 @@ describe('WorkspaceDetailComponent', () => {
{ provide: UserService, useValue: mockUserService }
]
}).compileComponents()
// to spy data: reset
locationSpy.back.calls.reset()
msgServiceSpy.success.calls.reset()
msgServiceSpy.error.calls.reset()
apiServiceSpy.getWorkspaceByName.calls.reset()
apiServiceSpy.deleteWorkspace.calls.reset()
apiServiceSpy.exportWorkspaces.calls.reset()
apiServiceSpy.updateWorkspace.calls.reset()
locationSpy.back.calls.reset()
// to spy data: refill with neutral data
apiServiceSpy.getWorkspaceByName.and.returnValue(of({}))
}))

function initializeComponent(): void {
Expand Down Expand Up @@ -128,35 +131,15 @@ describe('WorkspaceDetailComponent', () => {
})

component.onTabChange(event, component.workspace)

expect(component.selectedTabIndex).toEqual(1)
})

it('should set workspace for roles', () => {
const event = { index: 3 }

component.onTabChange(event, component.workspace)

component.onTabChange({ index: 3 }, component.workspace)
expect(component.workspaceForRoles).toBe(workspace)
})

it('should set workspace for slots', () => {
const event = {
index: 4
}

component.onTabChange(event, component.workspace)

component.onTabChange({ index: 4 }, component.workspace)
expect(component.workspaceForSlots).toBe(workspace)
})

it('should set workspace for products', () => {
const event = {
index: 5
}

component.onTabChange(event, component.workspace)

component.onTabChange({ index: 5 }, component.workspace)
expect(component.workspaceForProducts).toBe(workspace)
})
})
Expand Down Expand Up @@ -391,15 +374,17 @@ describe('WorkspaceDetailComponent', () => {
})

describe('update workspace data', () => {
it('it should display error on update workspace', (done) => {
it('it should display success on update workspace', (done) => {
apiServiceSpy.updateWorkspace.and.returnValue(of(workspace))
spyOn(console, 'error')
component.selectedTabIndex = 99
component.ngOnInit()
let actions: any = []
component.actions$!.subscribe((act) => (actions = act))

actions[3].actionCallback()

expect(console.error).toHaveBeenCalledWith("Couldn't assign tab to component")
expect(msgServiceSpy.success).toHaveBeenCalledWith({ summaryKey: 'ACTIONS.EDIT.MESSAGE.CHANGE_OK' })
component.workspace$.subscribe((data) => {
expect(data).toEqual(workspace)
Expand All @@ -412,10 +397,10 @@ describe('WorkspaceDetailComponent', () => {
apiServiceSpy.updateWorkspace.and.returnValue(throwError(() => errorResponse))
component.selectedTabIndex = 99
spyOn(console, 'error')

component.ngOnInit()
let actions: any = []
component.actions$!.subscribe((act) => (actions = act))

actions[3].actionCallback()

expect(msgServiceSpy.error).toHaveBeenCalledWith({ summaryKey: 'ACTIONS.EDIT.MESSAGE.CHANGE_NOK' })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,15 @@ describe('WorkspaceRolesComponent', () => {
component = fixture.componentInstance
component.workspace = workspace
fixture.detectChanges()
// to spy data: reset
wRoleServiceSpy.searchWorkspaceRoles.calls.reset()
wRoleServiceSpy.createWorkspaceRole.calls.reset()
wRoleServiceSpy.deleteWorkspaceRole.calls.reset()
iamRoleServiceSpy.searchAvailableRoles.calls.reset()
msgServiceSpy.success.calls.reset()
msgServiceSpy.error.calls.reset()
// to spy data: refill with neutral data
wRoleServiceSpy.searchWorkspaceRoles.and.returnValue(of({}))
})

it('should create', () => {
Expand Down Expand Up @@ -161,6 +164,7 @@ describe('WorkspaceRolesComponent', () => {
it('should display error on ws search', () => {
const errorResponse = { status: 404, statusText: 'Workspace roles not found' }
wRoleServiceSpy.searchWorkspaceRoles.and.returnValue(throwError(() => errorResponse))
spyOn(console, 'error')
const changes = {
['workspace']: {
previousValue: 'ws0',
Expand All @@ -172,10 +176,11 @@ describe('WorkspaceRolesComponent', () => {

component.ngOnChanges(changes as unknown as SimpleChanges)

expect(console.error).toHaveBeenCalledWith('searchAvailableRoles', errorResponse)
expect(component.exceptionKey).toEqual('EXCEPTIONS.HTTP_STATUS_' + errorResponse.status + '.WS_ROLES')
})

it('should populate iamRoles on search', () => {
it('should populate IAM roles on search', () => {
iamRoleServiceSpy.searchAvailableRoles.and.returnValue(of({ stream: [{ name: 'role' }] as IAMRolePageResult }))
const changes = {
['workspace']: {
Expand Down Expand Up @@ -228,9 +233,10 @@ describe('WorkspaceRolesComponent', () => {
expect(component.roles[0]).toBeUndefined()
})

it('should display error on iam search', () => {
it('should display error on IAM search', () => {
const errorResponse = { status: 404, statusText: 'IAM roles not found' }
iamRoleServiceSpy.searchAvailableRoles.and.returnValue(throwError(() => errorResponse))
spyOn(console, 'error')
const changes = {
['workspace']: {
previousValue: 'ws0',
Expand All @@ -242,6 +248,7 @@ describe('WorkspaceRolesComponent', () => {

component.ngOnChanges(changes as unknown as SimpleChanges)

expect(console.error).toHaveBeenCalledWith('searchAvailableRoles', errorResponse)
expect(component.exceptionKey).toEqual('EXCEPTIONS.HTTP_STATUS_' + errorResponse.status + '.IAM_ROLES')
})

Expand Down Expand Up @@ -282,7 +289,7 @@ describe('WorkspaceRolesComponent', () => {

component.onAddRole(mockEvent, wRole)

expect(msgServiceSpy.success).toHaveBeenCalledWith({ summaryKey: 'ACTIONS.CREATE.ROLE.MESSAGE_OK' })
expect(msgServiceSpy.success).toHaveBeenCalledWith({ summaryKey: 'ACTIONS.CREATE.ROLE_OK' })
})

it('should display error when creating a role onAddRole', () => {
Expand All @@ -293,7 +300,7 @@ describe('WorkspaceRolesComponent', () => {

component.onAddRole(mockEvent, wRole)

expect(msgServiceSpy.error).toHaveBeenCalledWith({ summaryKey: 'ACTIONS.CREATE.ROLE.MESSAGE_NOK' })
expect(msgServiceSpy.error).toHaveBeenCalledWith({ summaryKey: 'ACTIONS.CREATE.ROLE_NOK' })
expect(console.error).toHaveBeenCalledWith('createWorkspaceRole', errorResponse)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,18 @@ describe('WorkspaceSlotsComponent', () => {
{ provide: UserService, useValue: mockUserService }
]
}).compileComponents()
msgServiceSpy.success.calls.reset()
msgServiceSpy.error.calls.reset()
// to spy data: reset
wProductServiceSpy.getProductsByWorkspaceId.calls.reset()
slotServiceSpy.getSlotsForWorkspace.calls.reset()
slotServiceSpy.createSlot.calls.reset()
productServiceSpy.searchAvailableProducts.calls.reset()
msgServiceSpy.success.calls.reset()
msgServiceSpy.error.calls.reset()
// to spy data: refill with neutral data
wProductServiceSpy.getProductsByWorkspaceId.and.returnValue(of({}))
slotServiceSpy.getSlotsForWorkspace.and.returnValue(of({}))
slotServiceSpy.createSlot.and.returnValue(of({}))
productServiceSpy.searchAvailableProducts.and.returnValue(of({}))
}))

function initializeComponent(): void {
Expand Down Expand Up @@ -156,15 +162,17 @@ describe('WorkspaceSlotsComponent', () => {
})

it('should display error when product names cannot be loaded', () => {
const err = { status: '404' }
wProductServiceSpy.getProductsByWorkspaceId.and.returnValue(throwError(() => err))
const errorResponse = { status: '404', statusText: 'Not found' }
wProductServiceSpy.getProductsByWorkspaceId.and.returnValue(throwError(() => errorResponse))
spyOn(component as any, 'declareWorkspaceSlots').and.callFake(() => {})
spyOn(component as any, 'declarePsSlots').and.callFake(() => {})
spyOn(console, 'error')
component.workspace = workspace

component.loadData()

expect(component.exceptionKey).toBe('EXCEPTIONS.HTTP_STATUS_' + err.status + '.PRODUCTS')
expect(console.error).toHaveBeenCalledWith('getProductsByWorkspaceId', errorResponse)
expect(component.exceptionKey).toBe('EXCEPTIONS.HTTP_STATUS_' + errorResponse.status + '.PRODUCTS')
})

it('should get ws slots', () => {
Expand All @@ -183,14 +191,16 @@ describe('WorkspaceSlotsComponent', () => {
})

it('should display error when ws slots cannot be loaded', () => {
const err = { status: '404' }
slotServiceSpy.getSlotsForWorkspace.and.returnValue(throwError(() => err))
const errorResponse = { status: '404', statusText: 'Not found' }
slotServiceSpy.getSlotsForWorkspace.and.returnValue(throwError(() => errorResponse))
spyOn(component as any, 'declareWorkspaceProducts').and.callFake(() => {})
spyOn(component as any, 'declarePsSlots').and.callFake(() => {})
spyOn(console, 'error')

component.loadData()

expect(component.exceptionKey).toBe('EXCEPTIONS.HTTP_STATUS_' + err.status + '.SLOTS')
expect(console.error).toHaveBeenCalledWith('getSlotsForWorkspace', errorResponse)
expect(component.exceptionKey).toBe('EXCEPTIONS.HTTP_STATUS_' + errorResponse.status + '.SLOTS')
})

it('should get ps slots', () => {
Expand Down Expand Up @@ -246,14 +256,16 @@ describe('WorkspaceSlotsComponent', () => {
})

it('should display error when ps slots cannot be loaded', () => {
const err = { status: '404' }
productServiceSpy.searchAvailableProducts.and.returnValue(throwError(() => err))
const errorResponse = { status: '404', statusText: 'Not found' }
productServiceSpy.searchAvailableProducts.and.returnValue(throwError(() => errorResponse))
spyOn(component as any, 'declareWorkspaceProducts').and.callFake(() => {})
spyOn(component as any, 'declareWorkspaceSlots').and.callFake(() => {})
spyOn(console, 'error')

component.loadData()

expect(component.exceptionKey).toBe('EXCEPTIONS.HTTP_STATUS_' + err.status + '.PRODUCTS')
expect(console.error).toHaveBeenCalledWith('searchAvailableProducts', errorResponse)
expect(component.exceptionKey).toBe('EXCEPTIONS.HTTP_STATUS_' + errorResponse.status + '.PRODUCTS')
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class WorkspaceSlotsComponent implements OnInit, OnChanges, OnDestroy {
}),
catchError((err) => {
this.exceptionKey = 'EXCEPTIONS.HTTP_STATUS_' + err.status + '.PRODUCTS'
console.error('getProductsByWorkspaceId():', err)
console.error('getProductsByWorkspaceId', err)
return of([] as string[])
}),
finalize(() => (this.wpLoading = false))
Expand Down Expand Up @@ -170,7 +170,7 @@ export class WorkspaceSlotsComponent implements OnInit, OnChanges, OnDestroy {
}),
catchError((err) => {
this.exceptionKey = 'EXCEPTIONS.HTTP_STATUS_' + err.status + '.SLOTS'
console.error('searchSlots():', err)
console.error('getSlotsForWorkspace', err)
return of([])
})
)
Expand Down Expand Up @@ -263,7 +263,7 @@ export class WorkspaceSlotsComponent implements OnInit, OnChanges, OnDestroy {
}),
catchError((err) => {
this.exceptionKey = 'EXCEPTIONS.HTTP_STATUS_' + err.status + '.PRODUCTS'
console.error('searchAvailableProducts():', err)
console.error('searchAvailableProducts', err)
return of([])
}),
finalize(() => (this.sLoading = false))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ export class MenuPreviewComponent implements OnChanges {
}

public onHierarchyViewChange(event: TreeTableNodeExpandEvent): void {
this.stateService.getState().treeExpansionState.set(event.node.key ?? '', event.node.expanded ?? false)
if (event.node.key)
this.stateService.getState().treeExpansionState.set(event.node.key, event.node.expanded === true)
}

public onLanguagesPreviewChange(lang: string) {
Expand Down
3 changes: 2 additions & 1 deletion src/app/workspace/workspace-menu/menu.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@
type="button"
[id]="'ws_menu_table_header_roles_action_filter_clear'"
class="p-button-rounded p-button-text p-button p-component p-button-icon-only"
(click)="onToggleRoleFilter()"
(click)="onResetRoleFilter()"
aria-hidden="true"
[attr.aria-label]="'DIALOG.MENU.ROLE.FILTER.CLEAR' | translate"
[pTooltip]="'DIALOG.MENU.ROLE.FILTER.CLEAR' | translate"
Expand Down Expand Up @@ -294,6 +294,7 @@
<!-- BODY -->
<ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns">
<tr [ttRow]="rowNode" [id]="'ws_menu_table_row_' + rowData.key" [attr.level]="rowData.level">
<!-- menu item -->
<td class="p-0 pr-4">
<div class="flex flex-row flex-nowrap column-gap-1 align-items-center">
<p-treeTableToggler [rowNode]="rowNode"></p-treeTableToggler>
Expand Down
Loading

0 comments on commit 40a4375

Please sign in to comment.