Skip to content

Commit

Permalink
Fix tests (#143)
Browse files Browse the repository at this point in the history
* fix: slot search tests

* fix: prod detail and img container tests

* fix: prod search app search tests

* fix: all tests

* fix: last bit

* fix: delete comment in mixins css

---------

Co-authored-by: Christian Badura <[email protected]>
Co-authored-by: Henry Täschner <[email protected]>
  • Loading branch information
3 people authored Jun 14, 2024
1 parent 9e34848 commit 1255189
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 15 deletions.
1 change: 0 additions & 1 deletion src/_mixins.scss
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@
color: var(--primary-color-text);
background: var(--button-hover-bg);
cursor: pointer;
//padding: 5px !important;
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/app/product-store/app-search/app-search.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ describe('AppSearchComponent', () => {
}
})

it('should navigate to Slots when button clicked and actionCallback executed', () => {
component.ngOnInit()

if (component.actions$) {
component.actions$.subscribe((actions) => {
const firstAction = actions[1]
firstAction.actionCallback()
expect(routerSpy.navigate).toHaveBeenCalledWith(['../slots'], { relativeTo: routeMock })
})
}
})

it('should call onCreate when actionCallback is executed', () => {
spyOn(component, 'onCreate')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,19 +433,23 @@ describe('ProductPropertyComponent', () => {
})
})

it('should not upload a file that is too large', () => {
const largeBlob = new Blob(['a'.repeat(120000)], { type: 'image/png' })
const largeFile = new File([largeBlob], 'test.png', { type: 'image/png' })
it('should not upload a file of wrong file type', () => {
const blob = new Blob(['a'.repeat(10)], { type: 'txt' })
const file = new File([blob], 'test.txt', { type: 'txt' })
const event = {
target: {
files: [largeFile]
files: [file]
}
}
component.formGroup.controls['name'].setValue('name')

component.onFileUpload(event as any)

expect(component.formGroup.valid).toBeFalse()
expect(msgServiceSpy.error).toHaveBeenCalledWith({
summaryKey: 'IMAGE.CONSTRAINT_FAILED',
detailKey: 'IMAGE.CONSTRAINT_FILE_TYPE'
})
})

it('should upload a file', () => {
Expand All @@ -466,8 +470,9 @@ describe('ProductPropertyComponent', () => {
})
})

it('should display error if upload fails', () => {
it('should display error if api call to upload a file fails', () => {
imgServiceSpy.getImage.and.returnValue(throwError(() => new Error()))
imgServiceSpy.uploadImage.and.returnValue(of({}))
const blob = new Blob(['a'.repeat(10)], { type: 'image/png' })
const file = new File([blob], 'test.png', { type: 'image/png' })
const event = {
Expand All @@ -484,6 +489,23 @@ describe('ProductPropertyComponent', () => {
})
})

it('should display error if file choice fails', () => {
imgServiceSpy.getImage.and.returnValue(throwError(() => new Error()))
const event = {
target: {
files: undefined
}
}
component.formGroup.controls['name'].setValue('name')

component.onFileUpload(event as any)

expect(msgServiceSpy.error).toHaveBeenCalledWith({
summaryKey: 'IMAGE.CONSTRAINT_FAILED',
detailKey: 'IMAGE.CONSTRAINT_FILE_MISSING'
})
})

it('should test getLogoUrl()', () => {
let result = component.getLogoUrl(undefined)
expect(result).toBeUndefined()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,12 @@ export class ProductPropertyComponent implements OnChanges, OnInit {
} else {
this.saveImage(workspaceName, files) // store image
}
} else {
this.msgService.error({
summaryKey: 'IMAGE.CONSTRAINT_FAILED',
detailKey: 'IMAGE.CONSTRAINT_FILE_MISSING'
})
}
} else {
this.msgService.error({
summaryKey: 'IMAGE.CONSTRAINT_FAILED',
detailKey: 'IMAGE.CONSTRAINT_FILE_MISSING'
})
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import { ProductsAPIService } from 'src/app/shared/generated'
const product = {
id: 'id',
name: 'name',
basePath: 'path'
basePath: 'path',
imageUrl: 'imageUrl',
undeployed: true
}

class MockProductPropertyComponent {
Expand Down Expand Up @@ -99,14 +101,22 @@ describe('ProductDetailComponent', () => {
expect(component).toBeTruthy()
})

it('should be set up correctly onInit if no product name', () => {
it('should be set up correctly onInit if there is a product name', () => {
component.productName = 'name'

component.ngOnInit()

expect(component.changeMode).toEqual('VIEW')
})

it('should be set up correctly onInit if no product name', () => {
component.productName = ''

component.ngOnInit()

expect(component.changeMode).toEqual('CREATE')
})

it('should get product onInit', () => {
const p = { id: 'id', name: 'name', basePath: 'path' }
apiServiceSpy.getProductByName.and.returnValue(of(p))
Expand Down Expand Up @@ -146,6 +156,21 @@ describe('ProductDetailComponent', () => {
expect(component.onSave).toHaveBeenCalled()
})

it('should fulfill all conditions for edit button', () => {
spyOn(component, 'onEdit')
component.product = product
component.changeMode = 'VIEW'

component.prepareActionButtons()

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

actions[2].actionCallback()

expect(component.onEdit).toHaveBeenCalled()
})

it('should call close() onClose', () => {
spyOn(component, 'close')

Expand Down Expand Up @@ -270,4 +295,16 @@ describe('ProductDetailComponent', () => {
expect(component.selectedTabIndex).toEqual(2)
expect(component.product_for_apps).toEqual(product)
})

it('should update logo url', () => {
component.onUpdateLogoUrl('testUrl')

expect(component.currentLogoUrl).toBe('testUrl')
})

it('shoult get logo url', () => {
const result = component.getLogoUrl(product)

expect(result).toBe(product.imageUrl)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,12 @@ describe('ProductSearchComponent', () => {

expect(result).toEqual('')
})

it('should getLogoUrl from image api if not from existing product', () => {
const product = undefined

const result = component.getLogoUrl(product)

expect(result).toBeUndefined()
})
})
48 changes: 48 additions & 0 deletions src/app/product-store/slot-search/slot-search.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ describe('SlotSearchComponent', () => {
}
})

it('should navigate to Apps when button clicked and actionCallback executed', () => {
component.ngOnInit()

if (component.actions$) {
component.actions$.subscribe((actions) => {
const firstAction = actions[1]
firstAction.actionCallback()
expect(routerSpy.navigate).toHaveBeenCalledWith(['../apps'], { relativeTo: routeMock })
})
}
})

it('should set correct values onFilterChange', () => {
const filter = 'filter'

Expand All @@ -93,6 +105,24 @@ describe('SlotSearchComponent', () => {
expect(component.filter).toEqual(filter)
})

it('should set correct value onSortChange', () => {
const sortField = 'field'

component.onSortChange(sortField)

expect(component.sortField).toEqual(sortField)
})

it('should set correct value onSortDirChange', () => {
let asc = true
component.onSortDirChange(asc)
expect(component.sortOrder).toEqual(-1)

asc = false
component.onSortDirChange(asc)
expect(component.sortOrder).toEqual(1)
})

it('should search slots - successful found', (done) => {
apiSlotsServiceSpy.searchSlots.and.returnValue(of({ stream: slots } as SlotPageResult))

Expand Down Expand Up @@ -180,4 +210,22 @@ describe('SlotSearchComponent', () => {

expect(routerSpy.navigate).toHaveBeenCalledWith(['../'], { relativeTo: routeMock })
})

it('should behave correctly onDelete', () => {
const event: MouseEvent = new MouseEvent('type')

component.onDelete(event, slots[0])

expect(component.slot).toEqual(slots[0])
expect(component.displayDeleteDialog).toBeTrue()
})

it('should search slots again if a slot was deleted', () => {
spyOn(component, 'searchSlots')

component.slotDeleted(true)

expect(component.displayDeleteDialog).toBeFalse()
expect(component.searchSlots).toHaveBeenCalled()
})
})
31 changes: 29 additions & 2 deletions src/app/shared/image-container/image-container.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { NO_ERRORS_SCHEMA } from '@angular/core'
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'
import { TranslateTestingModule } from 'ngx-translate-testing'
import { of } from 'rxjs'
Expand Down Expand Up @@ -27,11 +28,12 @@ describe('ImageContainerComponent', () => {
en: require('src/assets/i18n/en.json')
}).withDefaultLanguage('en')
],
providers: [{ provide: AppStateService, useValue: mockAppStateService }]
providers: [{ provide: AppStateService, useValue: mockAppStateService }],
schemas: [NO_ERRORS_SCHEMA]
}).compileComponents()
}))

beforeEach(() => {
beforeEach(async () => {
fixture = TestBed.createComponent(ImageContainerComponent)
component = fixture.componentInstance
fixture.detectChanges()
Expand All @@ -41,6 +43,18 @@ describe('ImageContainerComponent', () => {
expect(component).toBeTruthy()
})

it('should get a default image url with info from app state service on creation', (done) => {
component.defaultImageUrl$.subscribe({
next: (url) => {
if (url) {
expect(url).toBe('/base/assets/images/logo.png')
}
done()
},
error: done.fail
})
})

describe('ngOnChanges', () => {
it('should not modify imageUrl if it starts with http/https', () => {
const testUrl = 'http://path/to/image.jpg'
Expand All @@ -56,6 +70,19 @@ describe('ImageContainerComponent', () => {

expect(component.imageUrl).toBe(testUrl)
})

it('should set defaultLogoUrl if component imageUrl is undefined', () => {
component.ngOnChanges({
imageUrl: {
currentValue: '',
previousValue: null,
firstChange: true,
isFirstChange: () => true
}
})

expect(component.displayDefaultLogo).toBeTrue()
})
})

it('onImageError should set displayDefaultLogo to true', () => {
Expand Down

0 comments on commit 1255189

Please sign in to comment.