Skip to content

Commit

Permalink
feat: finish all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Badura committed Jan 18, 2024
1 parent d438a54 commit 2b029e1
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { PortalMessageService } from '@onecx/portal-integration-angular'
import { HttpLoaderFactory } from 'src/app/shared/shared.module'
import { ProductPropertyComponent, ProductDetailForm } from './product-props.component'
import { ProductsAPIService } from 'src/app/generated'
// import { ProductDetailComponent } from '../product-detail.component'

describe('ProductPropertyComponent', () => {
let component: ProductPropertyComponent
Expand Down Expand Up @@ -106,6 +105,55 @@ describe('ProductPropertyComponent', () => {
component.onSubmit()

expect(apiServiceSpy.createProduct).toHaveBeenCalled()
expect(msgServiceSpy.success).toHaveBeenCalledWith({ summaryKey: 'ACTIONS.CREATE.MESSAGE.PRODUCT_OK' })
})

it('should call updateProduct onSubmit in view mode', () => {
apiServiceSpy.updateProduct.and.returnValue(of({}))
const formGroup = new FormGroup<ProductDetailForm>({
id: new FormControl<string | null>('id'),
name: new FormControl<string | null>('name'),
operator: new FormControl<boolean | null>(null),
version: new FormControl<string | null>('version'),
description: new FormControl<string | null>(null),
imageUrl: new FormControl<string | null>(null),
basePath: new FormControl<string | null>('path'),
displayName: new FormControl<string | null>('display'),
iconName: new FormControl<string | null>('icon'),
classifications: new FormControl<string[] | null>(null)
})
component.formGroup = formGroup as FormGroup<ProductDetailForm>
component.changeMode = 'VIEW'

component.onSubmit()

expect(apiServiceSpy.updateProduct).toHaveBeenCalled()
expect(msgServiceSpy.success).toHaveBeenCalledWith({ summaryKey: 'ACTIONS.EDIT.MESSAGE.PRODUCT_OK' })
})

it('should display error if searchProducts fails', () => {
apiServiceSpy.updateProduct.and.returnValue(throwError(() => new Error()))
const formGroup = new FormGroup<ProductDetailForm>({
id: new FormControl<string | null>('id'),
name: new FormControl<string | null>('name'),
operator: new FormControl<boolean | null>(null),
version: new FormControl<string | null>('version'),
description: new FormControl<string | null>(null),
imageUrl: new FormControl<string | null>(null),
basePath: new FormControl<string | null>('path'),
displayName: new FormControl<string | null>('display'),
iconName: new FormControl<string | null>('icon'),
classifications: new FormControl<string[] | null>(null)
})
component.formGroup = formGroup as FormGroup<ProductDetailForm>
component.changeMode = 'VIEW'

component.onSubmit()

expect(component.formGroup.valid).toBeTrue()
expect(msgServiceSpy.error).toHaveBeenCalledWith({
summaryKey: 'ACTIONS.EDIT.MESSAGE.PRODUCT_NOK'
})
})

it('should display error if searchProducts fails', () => {
Expand Down Expand Up @@ -155,4 +203,16 @@ describe('ProductPropertyComponent', () => {
summaryKey: 'VALIDATION.FORM_INVALID'
})
})

it('should display error onSubmit if formGroup invalid', () => {
const event = {
target: {
files: ['file']
}
}

component.onFileUpload(event as any)

expect(component.formGroup.valid).toBeFalse()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,18 @@ export class ProductPropertyComponent implements OnChanges {
this.productNameChanged.emit(this.productName !== this.formGroup.value['name'])
},
error: (err) => {
err.error.key && err.error.key === 'PERSIST_ENTITY_FAILED'
/* err.error.key && err.error.key === 'PERSIST_ENTITY_FAILED'
? this.msgService.error({
summaryKey: 'ACTIONS.EDIT.MESSAGE.PRODUCT_NOK',
detailKey: 'VALIDATION.PRODUCT.UNIQUE_CONSTRAINT'
})
: this.msgService.error({ summaryKey: 'ACTIONS.EDIT.MESSAGE.PRODUCT_NOK' })
: */
this.msgService.error({ summaryKey: 'ACTIONS.EDIT.MESSAGE.PRODUCT_NOK' })
}
})
}

public onFileUpload(ev: Event, fieldType: 'logo'): void {
public onFileUpload(ev: Event /* , fieldType: 'logo' */): void {
if (ev.target && (ev.target as HTMLInputElement).files) {
const files = (ev.target as HTMLInputElement).files
if (files) {
Expand Down
46 changes: 46 additions & 0 deletions src/app/shared/shared.module.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { NO_ERRORS_SCHEMA } from '@angular/core'
import { TestBed } from '@angular/core/testing'
import { HttpClient } from '@angular/common/http'
import { HttpClientTestingModule } from '@angular/common/http/testing'

import { MfeInfo, TranslateCombinedLoader } from '@onecx/portal-integration-angular'
import { basePathProvider, HttpLoaderFactory } from './shared.module'

describe('SharedModule', () => {
let httpClient: HttpClient

beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
schemas: [NO_ERRORS_SCHEMA]
})

httpClient = TestBed.inject(HttpClient)
})

it('should return the correct basePath with mfeInfo', () => {
const mfeInfo: MfeInfo = {
mountPath: '',
remoteBaseUrl: 'http://localhost:4200/',
baseHref: '',
shellName: ''
}

const result = basePathProvider(mfeInfo)

expect(result).toEqual('http://localhost:4200/product-store-bff')
})

it('should return a translate loader', () => {
const mfeInfo: MfeInfo = {
mountPath: '',
remoteBaseUrl: 'http://localhost:4200/',
baseHref: '',
shellName: ''
}

const result = HttpLoaderFactory(httpClient, mfeInfo)

expect(result).toBeInstanceOf(TranslateCombinedLoader)
})
})
8 changes: 4 additions & 4 deletions src/app/shared/shared.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ import { CanActivateGuard } from './can-active-guard.service'
import { ImageContainerComponent } from './image-container/image-container.component'

export const basePathProvider = (mfeInfo: MfeInfo) => {
console.log(
/* console.log(
'Base path provider: ' + (mfeInfo ? mfeInfo.remoteBaseUrl + '' + environment.apiPrefix : '' + environment.apiPrefix)
)
) */
return mfeInfo ? mfeInfo.remoteBaseUrl + '' + environment.apiPrefix : '' + environment.apiPrefix
}

export function HttpLoaderFactory(http: HttpClient, mfeInfo: MfeInfo) {
if (mfeInfo) {
/* if (mfeInfo) {
console.log(`Configuring translation loader ${mfeInfo?.remoteBaseUrl}`)
}
} */
// if running standalone then load the app assets directly from remote base URL
const appAssetPrefix = mfeInfo && mfeInfo.remoteBaseUrl ? mfeInfo.remoteBaseUrl : './'
return new TranslateCombinedLoader(
Expand Down

0 comments on commit 2b029e1

Please sign in to comment.