Skip to content

Commit

Permalink
Improve tests (#61)
Browse files Browse the repository at this point in the history
* fix: finish product search, improve app search

* fix: improve product props

* fix: finish product props

* fix: improve tests as far as possible without further research

* fix: exclude two tests in delete

---------

Co-authored-by: Christian Badura <[email protected]>
  • Loading branch information
cbadura and Christian Badura authored Feb 29, 2024
1 parent 0d56a24 commit ed1430c
Show file tree
Hide file tree
Showing 8 changed files with 453 additions and 49 deletions.
4 changes: 2 additions & 2 deletions src/app/product-store/app-delete/app-delete.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('AppDeleteComponent', () => {
expect(component.appDeleted.emit).toHaveBeenCalledWith(true)
})

it('should display error if api all fails onConfirmDeletion: mfe', () => {
it('should display error if api call fails onConfirmDeletion: mfe', () => {
apiMfeServiceSpy.deleteMicrofrontend.and.returnValue(throwError(() => new Error()))
component.appAbstract = appMfe

Expand All @@ -104,7 +104,7 @@ describe('AppDeleteComponent', () => {
expect(msgServiceSpy.success).toHaveBeenCalledWith({ summaryKey: 'ACTIONS.DELETE.APP.OK' })
})

xit('should display error if api all fails onConfirmDeletion: mfe', () => {
xit('should display error if api call fails onConfirmDeletion: ms', () => {
apiMfeServiceSpy.deleteMicroservice.and.returnValue(throwError(() => new Error()))
const appMs: AppAbstract = {
id: 'id',
Expand Down
6 changes: 4 additions & 2 deletions src/app/product-store/app-delete/app-delete.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,24 @@ export class AppDeleteComponent {

public onConfirmDeletion(): void {
if (this.appAbstract?.id) {
if (this.appAbstract?.appType === 'MFE')
if (this.appAbstract?.appType === 'MFE') {
this.mfeApi.deleteMicrofrontend({ id: this.appAbstract?.id }).subscribe({
next: () => {
this.msgService.success({ summaryKey: 'ACTIONS.DELETE.APP.OK' })
this.appDeleted.emit(true)
},
error: () => this.msgService.error({ summaryKey: 'ACTIONS.DELETE.APP.NOK' })
})
if (this.appAbstract?.appType === 'MS')
}
if (this.appAbstract?.appType === 'MS') {
this.msApi.deleteMicroservice({ id: this.appAbstract?.id }).subscribe({
next: () => {
this.msgService.success({ summaryKey: 'ACTIONS.DELETE.APP.OK' })
this.appDeleted.emit(true)
},
error: () => this.msgService.error({ summaryKey: 'ACTIONS.DELETE.APP.NOK' })
})
}
}
}
}
12 changes: 6 additions & 6 deletions src/app/product-store/app-detail/app-detail.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ export class AppDetailComponent implements OnChanges {
if (this.appAbstract.appType === 'MS') this.getMs()
}
}
private log(text: string, obj?: object): void {
if (this.debug) {
if (obj) console.log('app detail: ' + text, obj)
else console.log('app detail: ' + text)
}
}
// private log(text: string, obj?: object): void {
// if (this.debug) {
// if (obj) console.log('app detail: ' + text, obj)
// else console.log('app detail: ' + text)
// }
// }

public allowEditing(): boolean {
return (
Expand Down
124 changes: 98 additions & 26 deletions src/app/product-store/app-search/app-search.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,51 @@ describe('AppSearchComponent', () => {
expect(component.searchApps).toHaveBeenCalled()
})

it('should search mfes: one mfe', (done) => {
component.appSearchCriteriaGroup.controls['appType'].setValue('MFE')
component.mfes$ = of({
stream: [
{
id: 'mfe1',
appId: 'appId1',
appName: 'Microfrontend 1',
productName: 'p1',
remoteBaseUrl: 'url'
}
]
})

component.searchApps()

component.apps$.subscribe({
next: (apps) => {
expect(apps.length).toBe(1)
apps.forEach((app) => {
expect(app.appType).toEqual('MFE')
})
done()
},
error: done.fail
})
})

it('should search mfes: empty', (done) => {
component.appSearchCriteriaGroup.controls['appType'].setValue('MFE')
component.mfes$ = of({})

component.searchApps()

component.apps$.subscribe({
next: (apps) => {
expect(apps.length).toBe(0)
done()
},
error: done.fail
})
})

it('should catch error on searchApps: mfes', () => {
component.appSearchCriteriaGroup.controls['appType'].setValue('MFE')
const err = {
status: 404
}
Expand All @@ -182,8 +226,45 @@ describe('AppSearchComponent', () => {

expect(component.exceptionKey).toEqual('')
})
// expect(component.exceptionKey).toEqual('EXCEPTIONS.HTTP_STATUS_404.APPS')

it('should search mss: one ms', (done) => {
component.appSearchCriteriaGroup.controls['appType'].setValue('MS')
component.mss$ = of({
stream: [{ id: 'ms1', appId: 'appId3', appName: 'Microservice 1', productName: 'p1' }]
})

component.searchApps()

component.apps$.subscribe({
next: (apps) => {
expect(apps.length).toBe(1)
apps.forEach((app) => {
expect(app.appType).toEqual('MS')
})
done()
},
error: done.fail
})
})

it('should search mss: empty', (done) => {
component.appSearchCriteriaGroup.controls['appType'].setValue('MS')
component.mss$ = of({})

component.searchApps()

component.apps$.subscribe({
next: (apps) => {
expect(apps.length).toBe(0)
done()
},
error: done.fail
})
})

it('should catch error on searchApps: mss', () => {
component.appSearchCriteriaGroup.controls['appType'].setValue('MS')
const err = {
status: 404
}
Expand All @@ -194,37 +275,28 @@ describe('AppSearchComponent', () => {
expect(component.exceptionKey).toEqual('EXCEPTIONS.HTTP_STATUS_404.APPS')
})

it('should combine mfe and ms streams into apps$ with appType: only mfes', (done: DoneFn) => {
apiServiceSpy.searchMicrofrontends.and.returnValue(
of({
stream: [
{ id: 'mfe1', name: 'Microfrontend 1', productName: 'p1' },
{ id: 'mfe2', name: 'Microfrontend 2', productName: 'p1' }
]
})
)
apiServiceSpy.searchMicroservice.and.returnValue(
of({
stream: [
{ id: 'ms1', name: 'Microservice 1', productName: 'p1' },
{ id: 'ms2', name: 'Microservice 2', productName: 'p1' }
]
})
)
it('should combine mfe and ms streams into apps$ with appType', (done: DoneFn) => {
component.appSearchCriteriaGroup.controls['appType'].setValue('ALL')
component.mfes$ = of({
stream: [
{
id: 'mfe1',
appId: 'appId1',
appName: 'Microfrontend 1',
productName: 'p1',
remoteBaseUrl: 'url'
}
]
})
component.mss$ = of({
stream: [{ id: 'ms1', appId: 'appId3', appName: 'Microservice 1', productName: 'p1' }]
})

component.searchApps()

component.apps$.subscribe({
next: (result) => {
expect(result.length).toBe(0) // should be 4
/* expect(result).toEqual(
jasmine.arrayContaining([
jasmine.objectContaining({ id: 'mfe1', name: 'Microfrontend 1', appType: 'MFE' }),
jasmine.objectContaining({ id: 'mfe2', name: 'Microfrontend 2', appType: 'MFE' })
// jasmine.objectContaining({ id: 'ms1', name: 'Microservice 1', appType: 'MS' }),
// jasmine.objectContaining({ id: 'ms2', name: 'Microservice 2', appType: 'MS' })
])
) */
expect(result.length).toBe(2)
done()
},
error: done.fail
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { NO_ERRORS_SCHEMA } from '@angular/core'
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'
import { ComponentFixture, TestBed, fakeAsync, waitForAsync } from '@angular/core/testing'
import { HttpClientTestingModule } from '@angular/common/http/testing'
import { RouterTestingModule } from '@angular/router/testing'
import { of } from 'rxjs'
import { of, throwError } from 'rxjs'
import { TranslateTestingModule } from 'ngx-translate-testing'

import { PortalMessageService } from '@onecx/portal-integration-angular'
Expand Down Expand Up @@ -101,6 +101,63 @@ describe('ProductAppsComponent', () => {
})
})

xit('should display console error msg on searchApps', fakeAsync((done: DoneFn) => {
const searchSpy = spyOn((component as any).mfeApi, 'searchMicrofrontends').and.returnValue(
throwError(() => new Error())
)
// apiServiceSpy.searchMicrofrontends.and.callFake(() => {
// throw error
// })
spyOn(console, 'error')

component.searchApps()

component.mfes$.subscribe({
next: (obj) => {
console.log('NEXT', obj)
done()
},
error: (err) => {
console.log('ERROR', err)
// expect(console.error).toHaveBeenCalled()
}
})

expect(searchSpy).toHaveBeenCalledWith({
mfeAndMsSearchCriteria: { productName: component.product?.name }
})
expect(console.error).toHaveBeenCalled()

// tick()
}))

xit('should combine mfe and ms streams into apps$ with appType', (done: DoneFn) => {
component.mfes$ = of({
stream: [
{
id: 'mfe1',
appId: 'appId1',
appName: 'Microfrontend 1',
productName: 'p1',
remoteBaseUrl: 'url'
}
]
})
component.mss$ = of({
stream: [{ id: 'ms1', appId: 'appId3', appName: 'Microservice 1', productName: 'p1' }]
})

component.searchApps()

component.apps$.subscribe({
next: (result) => {
expect(result.length).toBe(2)
done()
},
error: done.fail
})
})

it('should set correct value onLayoutChange', () => {
const viewMode = 'EDIT'

Expand Down Expand Up @@ -171,4 +228,22 @@ describe('ProductAppsComponent', () => {
expect(component.app).toEqual(mockApp)
expect(component.displayDeleteDialog).toBeTrue()
})

it('should call searchApps if app changed', () => {
spyOn(component, 'searchApps')

component.appChanged(true)

expect(component.searchApps).toHaveBeenCalled()
expect(component.displayDetailDialog).toBeFalse()
})

it('should call searchApps if app deleted', () => {
spyOn(component, 'searchApps')

component.appDeleted(true)

expect(component.searchApps).toHaveBeenCalled()
expect(component.displayDetailDialog).toBeFalse()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ export class ProductAppsComponent implements OnChanges {
if (this.product) this.searchApps()
}

private log(text: string, obj?: object): void {
if (this.debug) {
if (obj) console.log('app search: ' + text, obj)
else console.log('app search: ' + text)
}
}
// private log(text: string, obj?: object): void {
// if (this.debug) {
// if (obj) console.log('app search: ' + text, obj)
// else console.log('app search: ' + text)
// }
// }

public searchApps(): void {
this.searchInProgress = true
Expand Down
Loading

0 comments on commit ed1430c

Please sign in to comment.