diff --git a/src/app/product-store/product-detail/product-props/product-props.component.spec.ts b/src/app/product-store/product-detail/product-props/product-props.component.spec.ts index 151b9df..cc88b82 100644 --- a/src/app/product-store/product-detail/product-props/product-props.component.spec.ts +++ b/src/app/product-store/product-detail/product-props/product-props.component.spec.ts @@ -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 @@ -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({ + id: new FormControl('id'), + name: new FormControl('name'), + operator: new FormControl(null), + version: new FormControl('version'), + description: new FormControl(null), + imageUrl: new FormControl(null), + basePath: new FormControl('path'), + displayName: new FormControl('display'), + iconName: new FormControl('icon'), + classifications: new FormControl(null) + }) + component.formGroup = formGroup as FormGroup + 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({ + id: new FormControl('id'), + name: new FormControl('name'), + operator: new FormControl(null), + version: new FormControl('version'), + description: new FormControl(null), + imageUrl: new FormControl(null), + basePath: new FormControl('path'), + displayName: new FormControl('display'), + iconName: new FormControl('icon'), + classifications: new FormControl(null) + }) + component.formGroup = formGroup as FormGroup + 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', () => { @@ -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() + }) }) diff --git a/src/app/product-store/product-detail/product-props/product-props.component.ts b/src/app/product-store/product-detail/product-props/product-props.component.ts index 3d553b5..e2ee32c 100644 --- a/src/app/product-store/product-detail/product-props/product-props.component.ts +++ b/src/app/product-store/product-detail/product-props/product-props.component.ts @@ -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) { diff --git a/src/app/shared/shared.module.spec.ts b/src/app/shared/shared.module.spec.ts new file mode 100644 index 0000000..6cbfe53 --- /dev/null +++ b/src/app/shared/shared.module.spec.ts @@ -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) + }) +}) diff --git a/src/app/shared/shared.module.ts b/src/app/shared/shared.module.ts index efbe245..15771ce 100644 --- a/src/app/shared/shared.module.ts +++ b/src/app/shared/shared.module.ts @@ -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(