-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for searching and editing products (#8)
* feat: add first tests to search * feat: finish search tests * feat: prepare detail and props files * feat: finish search tests * feat: almost finish props tests * feat: finish tests for shared files * feat: finish all tests * feat: put back fieldType variable, needed in html * feat: fix file upload test --------- Co-authored-by: Christian Badura <[email protected]>
- Loading branch information
Showing
12 changed files
with
867 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
218 changes: 218 additions & 0 deletions
218
src/app/product-store/product-detail/product-props/product-props.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
import { NO_ERRORS_SCHEMA } from '@angular/core' | ||
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing' | ||
import { HttpClient } from '@angular/common/http' | ||
import { HttpClientTestingModule } from '@angular/common/http/testing' | ||
import { RouterTestingModule } from '@angular/router/testing' | ||
import { TranslateLoader, TranslateModule } from '@ngx-translate/core' | ||
import { of, throwError } from 'rxjs' | ||
import { FormControl, FormGroup, Validators } from '@angular/forms' | ||
|
||
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' | ||
|
||
describe('ProductPropertyComponent', () => { | ||
let component: ProductPropertyComponent | ||
let fixture: ComponentFixture<ProductPropertyComponent> | ||
|
||
const msgServiceSpy = jasmine.createSpyObj<PortalMessageService>('PortalMessageService', ['success', 'error', 'info']) | ||
const apiServiceSpy = { | ||
createProduct: jasmine.createSpy('createProduct').and.returnValue(of({})), | ||
updateProduct: jasmine.createSpy('updateProduct').and.returnValue(of({})) | ||
} | ||
|
||
beforeEach(waitForAsync(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [ProductPropertyComponent], | ||
imports: [ | ||
HttpClientTestingModule, | ||
RouterTestingModule, | ||
TranslateModule.forRoot({ | ||
loader: { | ||
provide: TranslateLoader, | ||
useFactory: HttpLoaderFactory, | ||
deps: [HttpClient] | ||
} | ||
}) | ||
], | ||
schemas: [NO_ERRORS_SCHEMA], | ||
providers: [ | ||
{ provide: ProductsAPIService, useValue: apiServiceSpy }, | ||
{ provide: PortalMessageService, useValue: msgServiceSpy } | ||
] | ||
}).compileComponents() | ||
})) | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(ProductPropertyComponent) | ||
component = fixture.componentInstance | ||
fixture.detectChanges() | ||
}) | ||
|
||
afterEach(() => { | ||
msgServiceSpy.success.calls.reset() | ||
msgServiceSpy.error.calls.reset() | ||
msgServiceSpy.info.calls.reset() | ||
apiServiceSpy.createProduct.calls.reset() | ||
apiServiceSpy.updateProduct.calls.reset() | ||
}) | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy() | ||
}) | ||
|
||
it('should patchValue in formGroup onChanges if product', () => { | ||
const product = { | ||
id: 'id', | ||
name: 'name', | ||
basePath: 'path' | ||
} | ||
component.product = product | ||
spyOn(component.formGroup, 'patchValue') | ||
|
||
component.ngOnChanges() | ||
|
||
expect(component.formGroup.patchValue).toHaveBeenCalledWith({ ...product }) | ||
expect(component.product.name).toEqual(product.name) | ||
}) | ||
|
||
it('should reset formGroup onChanges if no product', () => { | ||
spyOn(component.formGroup, 'reset') | ||
|
||
component.ngOnChanges() | ||
|
||
expect(component.formGroup.reset).toHaveBeenCalled() | ||
}) | ||
|
||
it('should call createProduct onSubmit in new mode', () => { | ||
apiServiceSpy.createProduct.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 = 'NEW' | ||
|
||
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', () => { | ||
apiServiceSpy.createProduct.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 = 'NEW' | ||
|
||
component.onSubmit() | ||
|
||
expect(component.formGroup.valid).toBeTrue() | ||
expect(msgServiceSpy.error).toHaveBeenCalledWith({ | ||
summaryKey: 'ACTIONS.CREATE.MESSAGE.PRODUCT_NOK' | ||
}) | ||
}) | ||
|
||
it('should display error onSubmit if formGroup invalid', () => { | ||
const formGroup = new FormGroup<ProductDetailForm>({ | ||
id: new FormControl<string | null>(null, Validators.required), | ||
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.onSubmit() | ||
|
||
expect(component.formGroup.valid).toBeFalse() | ||
expect(msgServiceSpy.error).toHaveBeenCalledWith({ | ||
summaryKey: 'VALIDATION.FORM_INVALID' | ||
}) | ||
}) | ||
|
||
it('should display error onSubmit if formGroup invalid', () => { | ||
const event = { | ||
target: { | ||
files: ['file'] | ||
} | ||
} | ||
|
||
component.onFileUpload(event as any, 'logo') | ||
|
||
expect(component.formGroup.valid).toBeFalse() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.