From e615e27282444ed9b4ddbbbd47763a7816686d2d Mon Sep 17 00:00:00 2001 From: Tifenn Guillas <tifenn.guillas@lam.fr> Date: Fri, 21 Aug 2020 15:30:40 +0200 Subject: [PATCH] Tests on detail containers => DONE --- .../detail/containers/detail.component.html | 10 +-- .../containers/detail.component.spec.ts | 84 +++++++++++++++++++ 2 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 src/app/detail/containers/detail.component.spec.ts diff --git a/src/app/detail/containers/detail.component.html b/src/app/detail/containers/detail.component.html index 4589b02d..395f0539 100644 --- a/src/app/detail/containers/detail.component.html +++ b/src/app/detail/containers/detail.component.html @@ -12,11 +12,11 @@ </div> <div *ngIf="objectIsLoaded | async"> <app-object-display - [datasetName]="datasetName |Â async" - [outputFamilyList]="outputFamilyList |Â async" - [categoryList]="categoryList |Â async" - [attributeList]="attributeList |Â async" - [object]="object |Â async" + [datasetName]="datasetName | async" + [outputFamilyList]="outputFamilyList | async" + [categoryList]="categoryList | async" + [attributeList]="attributeList | async" + [object]="object | async" [spectraIsLoading]="spectraIsLoading | async" [spectraIsLoaded]="spectraIsLoaded | async" [spectraCSV]="spectraCSV | async" diff --git a/src/app/detail/containers/detail.component.spec.ts b/src/app/detail/containers/detail.component.spec.ts new file mode 100644 index 00000000..116d26f1 --- /dev/null +++ b/src/app/detail/containers/detail.component.spec.ts @@ -0,0 +1,84 @@ +import { ComponentFixture, TestBed, async, inject } from '@angular/core/testing'; +import { provideMockStore, MockStore } from '@ngrx/store/testing'; +import { Component, InjectionToken, Input } from '@angular/core'; +import { Location } from '@angular/common'; + +import { DetailComponent } from './detail.component'; +import * as fromDetail from '../store/detail.reducer'; +import * as detailActions from '../store/detail.action'; +import { Attribute, Family, Category } from 'src/app/metamodel/model'; + +describe('[Search] Container: DetailComponent', () => { + @Component({ selector: 'app-object-display', template: '' }) + class ObjectDisplayStubComponent { + @Input() datasetName: string; + @Input() outputFamilyList: Family[]; + @Input() categoryList: Category[]; + @Input() attributeList: Attribute[]; + @Input() object: any; + @Input() spectraIsLoading: boolean; + @Input() spectraIsLoaded: boolean; + @Input() spectraCSV: string; + } + + let component: DetailComponent; + let fixture: ComponentFixture<DetailComponent>; + let store: MockStore; + const initialState = { + detail: { ...fromDetail.initialState } + }; + const LOCATION_TOKEN = new InjectionToken<Location>('Window location object'); + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ + DetailComponent, + ObjectDisplayStubComponent + ], + providers: [ + provideMockStore({ initialState }), + { provide: LOCATION_TOKEN, useValue: window.history } + ] + }); + fixture = TestBed.createComponent(DetailComponent); + component = fixture.componentInstance; + store = TestBed.inject(MockStore); + })); + + it('should create the component', () => { + expect(component).toBeTruthy(); + }); + + it('should execute ngOnInit lifecycle', (done) => { + const initDetailAction = new detailActions.InitDetailAction(); + const spy = spyOn(store, 'dispatch'); + component.ngOnInit(); + Promise.resolve(null).then(function() { + expect(spy).toHaveBeenCalledTimes(1); + expect(spy).toHaveBeenCalledWith(initDetailAction); + done(); + }); + }); + + it ("#goBackToResult() should go to the previous location", inject([LOCATION_TOKEN], (location: Location) => { + spyOn(location, 'back'); + component.goBackToResult(); + expect(location.back).toHaveBeenCalledTimes(1); + })); + + it('#getSpectraCSV() should dispatch RetrieveSpectraAction', () => { + const retrieveSpectraAction = new detailActions.RetrieveSpectraAction('toto'); + const spy = spyOn(store, 'dispatch'); + component.getSpectraCSV('toto'); + expect(spy).toHaveBeenCalledTimes(1); + expect(spy).toHaveBeenCalledWith(retrieveSpectraAction); + }); + + it('#ngOnDestroy() should dispatch DestroyDetailAction', () => { + const destroyDetailAction = new detailActions.DestroyDetailAction(); + const spy = spyOn(store, 'dispatch'); + component.ngOnDestroy(); + expect(spy).toHaveBeenCalledTimes(1); + expect(spy).toHaveBeenCalledWith(destroyDetailAction); + }); +}); -- GitLab