Skip to content

Commit

Permalink
fix: product detail tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryT-CG committed Jan 21, 2024
1 parent cd66136 commit 4b598d7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ describe('ProductPropertyComponent', () => {
classifications: new FormControl<string[] | null>(null)
})
component.formGroup = formGroup as FormGroup<ProductDetailForm>
component.changeMode = 'NEW'
component.changeMode = 'CREATE'

component.onSubmit()

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

it('should call updateProduct onSubmit in view mode', () => {
it('should call updateProduct onSubmit in edit mode', () => {
apiServiceSpy.updateProduct.and.returnValue(of({}))
const formGroup = new FormGroup<ProductDetailForm>({
id: new FormControl<string | null>('id'),
Expand All @@ -123,15 +123,15 @@ describe('ProductPropertyComponent', () => {
classifications: new FormControl<string[] | null>(null)
})
component.formGroup = formGroup as FormGroup<ProductDetailForm>
component.changeMode = 'VIEW'
component.changeMode = 'EDIT'

component.onSubmit()

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

it('should display error if searchProducts fails', () => {
it('should display error if updateProduct fails', () => {
apiServiceSpy.updateProduct.and.returnValue(throwError(() => new Error()))
const formGroup = new FormGroup<ProductDetailForm>({
id: new FormControl<string | null>('id'),
Expand All @@ -146,17 +146,17 @@ describe('ProductPropertyComponent', () => {
classifications: new FormControl<string[] | null>(null)
})
component.formGroup = formGroup as FormGroup<ProductDetailForm>
component.changeMode = 'VIEW'
component.changeMode = 'EDIT'

component.onSubmit()

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

it('should display error if searchProducts fails', () => {
it('should display error if createProduct fails', () => {
apiServiceSpy.createProduct.and.returnValue(throwError(() => new Error()))
const formGroup = new FormGroup<ProductDetailForm>({
id: new FormControl<string | null>('id'),
Expand All @@ -171,13 +171,13 @@ describe('ProductPropertyComponent', () => {
classifications: new FormControl<string[] | null>(null)
})
component.formGroup = formGroup as FormGroup<ProductDetailForm>
component.changeMode = 'NEW'
component.changeMode = 'CREATE'

component.onSubmit()

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ export class ProductPropertyComponent implements OnChanges {
this.changeMode !== 'VIEW' ? this.formGroup.enable() : this.formGroup.disable()
}

/** CREATE/UPDATE product
*/
public onSubmit() {
if (this.formGroup.valid) {
this.changeMode === 'CREATE' ? this.createProduct() : this.updateProduct()
Expand Down Expand Up @@ -120,15 +122,6 @@ export class ProductPropertyComponent implements OnChanges {
})
}

private displaySaveError(err: any) {
err.error && err.error.errorCode && err.error.errorCode === 'MERGE_ENTITY_FAILED' // 'PERSIST_ENTITY_FAILED'
? this.msgService.error({
summaryKey: 'ACTIONS.' + this.changeMode + '.PRODUCT.NOK',
detailKey: 'VALIDATION.PRODUCT.UNIQUE_CONSTRAINT'
})
: this.msgService.error({ summaryKey: 'ACTIONS.' + this.changeMode + '.PRODUCT.NOK' })
}

private updateProduct() {
this.productApi
.updateProduct({
Expand All @@ -154,6 +147,17 @@ export class ProductPropertyComponent implements OnChanges {
})
}

private displaySaveError(err: any) {
err.error && err.error.errorCode && err.error.errorCode === 'MERGE_ENTITY_FAILED' // 'PERSIST_ENTITY_FAILED'
? this.msgService.error({
summaryKey: 'ACTIONS.' + this.changeMode + '.PRODUCT.NOK',
detailKey: 'VALIDATION.PRODUCT.UNIQUE_CONSTRAINT'
})
: this.msgService.error({ summaryKey: 'ACTIONS.' + this.changeMode + '.PRODUCT.NOK' })
}

/** File Handling
*/
public onFileUpload(ev: Event, fieldType: 'logo'): void {
if (ev.target && (ev.target as HTMLInputElement).files) {
const files = (ev.target as HTMLInputElement).files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ describe('ProductDetailComponent', () => {
expect(component.productPropsComponent.onSubmit).toHaveBeenCalled()
})

it('should behave correctly onCreate', () => {
it('should behave correctly onCreate henry', () => {
const data: any = { id: 'id ' }

component.onCreate(data)
Expand All @@ -207,7 +207,7 @@ describe('ProductDetailComponent', () => {
expect(component.close).toHaveBeenCalled()
})

it('should behave correctly onNameChange if change false', () => {
it('should behave correctly onChange if change false', () => {
spyOn(component, 'getProduct')

component.onChange(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('ProductSearchComponent', () => {
})

it('should prepare action buttons on init', () => {
translateServiceSpy.get.and.returnValue(of({ 'ACTIONS.CREATE.PRODUCT': 'Create' }))
translateServiceSpy.get.and.returnValue(of({ 'ACTIONS.CREATE.LABEL': 'Create' }))
spyOn(component, 'onNewProduct')

component.ngOnInit()
Expand Down Expand Up @@ -104,7 +104,7 @@ describe('ProductSearchComponent', () => {
})

it('should call loadProducts onSearch', () => {
translateServiceSpy.get.and.returnValue(of({ 'ACTIONS.CREATE.PRODUCT': 'Create' }))
translateServiceSpy.get.and.returnValue(of({ 'ACTIONS.CREATE.LABEL': 'Create' }))
spyOn(component, 'loadProducts')

component.onSearch()
Expand Down

0 comments on commit 4b598d7

Please sign in to comment.