Skip to content

Commit

Permalink
Fix tests for Observable use (#162)
Browse files Browse the repository at this point in the history
* fix: detail tests

* fix: search tests

* fix: delete fdescribe

* fix: add async object in html

* fix: type problem in annct banner after merge

* fix: usedWorkspaces and usedProducts observables

* fix: search tests

---------

Co-authored-by: Christian Badura <[email protected]>
Co-authored-by: Henry Täschner <[email protected]>
  • Loading branch information
3 people authored Jul 30, 2024
1 parent cee9c01 commit e0e5512
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const announcement: Announcement = {
endDate: '2023-01-03'
}

xdescribe('AnnouncementDetailComponent', () => {
describe('AnnouncementDetailComponent', () => {
let component: AnnouncementDetailComponent
let fixture: ComponentFixture<AnnouncementDetailComponent>

Expand Down Expand Up @@ -210,22 +210,14 @@ xdescribe('AnnouncementDetailComponent', () => {
})
})

describe('submitFormValues', () => {
describe(': submitFormValues', () => {
it('should prevent non-existing workspace name from being saved', () => {
component.formGroup = formGroup
component.formGroup.patchValue({ workspaceName: 'All Workspaces', productName: 'all' })
component.formGroup.patchValue({ workspaceName: 'all', productName: 'all' })

const result = (component as any).submitFormValues()

expect(result.workspaceName).toBeUndefined()
})

it('should prevent non-existing product name from being saved', () => {
component.formGroup = formGroup
component.formGroup.patchValue({ workspaceName: 'all', productName: 'All Applications' })

const result = (component as any).submitFormValues()

expect(result.productName).toBeUndefined()
})
})
Expand Down Expand Up @@ -296,14 +288,14 @@ xdescribe('AnnouncementDetailComponent', () => {
* Language tests
*/
it('should set a German date format', () => {
expect(component.dateFormat).toEqual('dd.MM.yyyy HH:mm')
expect(component.dateFormat).toEqual('dd.mm.yy')
})

it('should set default date format', () => {
mockUserService.lang$.getValue.and.returnValue('en')
fixture = TestBed.createComponent(AnnouncementDetailComponent)
component = fixture.componentInstance
fixture.detectChanges()
expect(component.dateFormat).toEqual('M/d/yy, h:mm a')
expect(component.dateFormat).toEqual('mm/dd/yy')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ export class AnnouncementDetailComponent implements OnChanges {
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private submitFormValues(): any {
const announcement: Announcement = { ...this.formGroup.value }
console.log('submitFormValues', announcement)
if (announcement.workspaceName === 'all') announcement.workspaceName = undefined
if (announcement.productName === 'all') announcement.productName = undefined
return announcement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class AnnouncementCriteriaComponent implements OnInit {
public displayCreateDialog = false
public announcementCriteria!: FormGroup<AnnouncementCriteriaForm>
public dateFormatForRange: string
public filteredTitles: any[] = []
public filteredTitles = []
public type$: Observable<SelectItem[]> = of([])
public statusOptions$: Observable<SelectItem[]> = of([])
public priorityType$: Observable<SelectItem[]> = of([])
Expand Down Expand Up @@ -66,7 +66,7 @@ export class AnnouncementCriteriaComponent implements OnInit {
'ENUMS.ANNOUNCEMENT_TYPE.' + AnnouncementType.SystemMaintenance
])
.pipe(
map((data: any) => {
map((data) => {
return [
{ label: data['ENUMS.ANNOUNCEMENT_TYPE.' + AnnouncementType.Event], value: AnnouncementType.Event },
{ label: data['ENUMS.ANNOUNCEMENT_TYPE.' + AnnouncementType.Info], value: AnnouncementType.Info },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<ocx-portal-page permission="ANNOUNCEMENT#SEARCH" helpArticleId="PAGE_ANNOUNCEMENT_SEARCH">
<ocx-portal-page
*ngIf="{ value: allMetaData$ | async } as allMetaData"
permission="ANNOUNCEMENT#SEARCH"
helpArticleId="PAGE_ANNOUNCEMENT_SEARCH"
>
<app-announcement-criteria
[actions]="(actions$ | async) ?? []"
[workspaces]="usedWorkspaces"
[products]="usedProducts"
[workspaces]="(usedWorkspaces$ | async) ?? []"
[products]="(usedProducts$ | async) ?? []"
(criteriaEmitter)="search($event)"
(resetSearchEmitter)="onReset()"
></app-announcement-criteria>

<ocx-page-content styleClass="px-3" *ngIf="allMataData$ | async">
<ocx-page-content styleClass="px-3">
<p-table
#announcementTable
id="announcement_search_table"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NO_ERRORS_SCHEMA } from '@angular/core'
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'
import { ComponentFixture, fakeAsync, TestBed, waitForAsync } from '@angular/core/testing'
import { HttpClient } from '@angular/common/http'
import { HttpClientTestingModule } from '@angular/common/http/testing'
import { TranslateLoader, TranslateModule } from '@ngx-translate/core'
Expand Down Expand Up @@ -57,7 +57,7 @@ const announcementData: any = [
}
]

xdescribe('AnnouncementSearchComponent', () => {
describe('AnnouncementSearchComponent', () => {
let component: AnnouncementSearchComponent
let fixture: ComponentFixture<AnnouncementSearchComponent>

Expand Down Expand Up @@ -125,11 +125,9 @@ xdescribe('AnnouncementSearchComponent', () => {
{ field: 'title', header: 'TITLE', active: false },
{ field: 'workspaceName', header: 'WORKSPACE', active: true }
]
spyOn(component, 'search')

component.ngOnInit()

expect(component.search).toHaveBeenCalled()
expect(component.filteredColumns[0].field).toEqual('workspaceName')
})

Expand Down Expand Up @@ -281,7 +279,7 @@ xdescribe('AnnouncementSearchComponent', () => {
it('should delete an announcement item with and without workspace assignment', () => {
const ev: MouseEvent = new MouseEvent('type')
apiServiceSpy.deleteAnnouncementById.and.returnValue(of({}))
component.usedWorkspaces = [{ label: 'workspace', value: 'workspace' }]
// component.usedWorkspaces = [{ label: 'workspace', value: 'workspace' }]
component.announcements = [
{ id: 'a1', title: 'a1' },
{ id: 'a2', title: 'a2', workspaceName: 'workspace' }
Expand Down Expand Up @@ -363,51 +361,65 @@ xdescribe('AnnouncementSearchComponent', () => {
/**
* test workspaces: fetching used ws and all ws
*/
it('should get all workspaces announcements are assigned to', () => {
it('should get all announcements assigned to workspaces', (done) => {
const assignments: AnnouncementAssignments = { productNames: [], workspaceNames: ['w1'] }
apiServiceSpy.getAllAnnouncementAssignments.and.returnValue(of(assignments))
component.usedWorkspaces = []
component.allWorkspaces = [{ label: 'Workspace1', value: 'w1' }]

component.ngOnInit()
component['getUsedWorkspacesAndProducts']()

expect(component.usedWorkspaces).toContain({ label: 'w1', value: 'w1' })
component.usedWorkspaces$?.subscribe({
next: (ws) => {
expect(ws).toContain({ label: 'Workspace1', value: 'w1' })
done()
}
})
})

it('should display error msg if that call fails', () => {
it('should display error msg if getAllAnnouncementAssignments call fails', (done) => {
const err = { status: '400' }
apiServiceSpy.getAllAnnouncementAssignments.and.returnValue(throwError(() => err))
spyOn(console, 'error')

component.ngOnInit()
component['getUsedWorkspacesAndProducts']()

expect(msgServiceSpy.error).toHaveBeenCalledWith({
summaryKey: 'GENERAL.ASSIGNMENTS.NOT_FOUND',
detailKey: 'EXCEPTIONS.HTTP_STATUS_' + err.status + '.ASSIGNMENTS'
component.usedWorkspaces$?.subscribe({
next: () => {
expect(console.error).toHaveBeenCalledWith('getUsedWorkspacesAndProducts', err)
done()
}
})
})

it('should get all existing workspaces', () => {
const workspaceNames = [{ name: 'w1' }, { name: 'w2' }]
it('should get all existing workspaces', (done) => {
const workspaceNames = [{ name: 'ws', displayName: 'Workspace' }]
apiServiceSpy.getAllWorkspaceNames.and.returnValue(of(workspaceNames))
component.allWorkspaces = []

component.ngOnInit()
component['searchWorkspaces']()

expect(component.allWorkspaces).toContain({ label: 'ANNOUNCEMENT.EVERY_WORKSPACE', value: 'all' })
component.allWorkspaces$.subscribe({
next: (workspaces) => {
expect(workspaces.length).toBe(1)
expect(workspaces[0].displayName).toEqual('Workspace')
done()
}
})
})

it('should log error if that fails', () => {
it('should log error getting all existing wss fails', fakeAsync(() => {
const err = { status: '400' }
apiServiceSpy.getAllWorkspaceNames.and.returnValue(throwError(() => err))
spyOn(console, 'error')

component.ngOnInit()
component['searchWorkspaces']()

expect(msgServiceSpy.error).toHaveBeenCalledWith({
summaryKey: 'GENERAL.WORKSPACES.NOT_FOUND',
detailKey: 'EXCEPTIONS.HTTP_STATUS_' + err.status + '.WORKSPACES'
component.allWorkspaces$.subscribe({
next: () => {
expect(console.error).toHaveBeenCalledWith('getAllWorkspaceNames():', err)
}
})
})
}))

it('should verify a workspace to be one of all workspaces', () => {
const workspaces = [{ label: 'w1', value: 'w1' }]
Expand Down Expand Up @@ -442,49 +454,70 @@ xdescribe('AnnouncementSearchComponent', () => {
/**
* test products: fetching used products and all products
*/
it('should get products announcements are assigned to', () => {
it('should get announcements assigned to products', (done) => {
const assignments: AnnouncementAssignments = { workspaceNames: [], productNames: ['prod1'] }
apiServiceSpy.getAllAnnouncementAssignments.and.returnValue(of(assignments))
component.usedProducts = []
component.allProducts = [{ label: 'Product1', value: 'prod1' }]

component.ngOnInit()
component['getUsedWorkspacesAndProducts']()

expect(component.usedProducts).toContain({ label: 'prod1', value: 'prod1' })
component.usedProducts$?.subscribe({
next: (p) => {
expect(p).toContain({ label: 'Product1', value: 'prod1' })
done()
}
})
})

it('should display error if that call fails', () => {
it('should display error if getting used products fails', (done) => {
const err = { status: '400' }
apiServiceSpy.getAllAnnouncementAssignments.and.returnValue(throwError(() => err))
spyOn(console, 'error')

component.ngOnInit()
component['getUsedWorkspacesAndProducts']()

expect(msgServiceSpy.error).toHaveBeenCalledWith({
summaryKey: 'GENERAL.ASSIGNMENTS.NOT_FOUND',
detailKey: 'EXCEPTIONS.HTTP_STATUS_' + err.status + '.ASSIGNMENTS'
component.usedProducts$?.subscribe({
next: () => {
expect(console.error).toHaveBeenCalledWith('getUsedWorkspacesAndProducts', err)
done()
}
})
})

// it('should get all existing products', () => {
// const productNames = { stream: [{ name: 'prod1', displayName: 'prod1_display' }, { name: 'prod2', displayName: 'prod2_display'}] }
// apiServiceSpy.getAllProductNames.and.returnValue(of(productNames))
// component.allProducts = []

// component.ngOnInit()

// expect(component.allProducts).toEqual([{label: 'ANNOUNCEMENT.EVERY_PRODUCT', value: 'all' }, { label: 'prod1_display', value: 'prod1' }, { label: 'prod2_display', value: 'prod2' }])
// })
it('should get all existing products', (done) => {
const productNames = {
stream: [
{ name: 'prod1', displayName: 'prod1_display' },
{ name: 'prod2', displayName: 'prod2_display' }
]
}
apiServiceSpy.getAllProductNames.and.returnValue(of(productNames))
component.allProducts = []

component['searchProducts']()

component.allProducts$.subscribe({
next: (products) => {
if (products.stream) {
expect(products.stream.length).toBe(2)
expect(products.stream[0].displayName).toEqual('prod1_display')
done()
}
}
})
})

it('should display error if that call fails', () => {
const err = { status: '400' }
apiServiceSpy.getAllProductNames.and.returnValue(throwError(() => err))
spyOn(console, 'error')

component.ngOnInit()
component['searchProducts']()

expect(msgServiceSpy.error).toHaveBeenCalledWith({
summaryKey: 'GENERAL.PRODUCTS.NOT_FOUND',
detailKey: 'EXCEPTIONS.HTTP_STATUS_' + err.status + '.PRODUCTS'
component.allProducts$.subscribe({
next: () => {
expect(console.error).toHaveBeenCalledWith('getAllProductNames():', err)
}
})
})

Expand Down
Loading

0 comments on commit e0e5512

Please sign in to comment.