From c44faa190c08a16c4f8395be2d10fd2c2f361922 Mon Sep 17 00:00:00 2001 From: Tifenn Guillas <tifenn.guillas@gmail.com> Date: Thu, 29 Jul 2021 16:29:50 +0200 Subject: [PATCH] WIP: tests on containers --- .../containers/dataset-list.component.ts | 7 +- .../documentation.component.spec.ts | 91 +++++++++++++++++++ .../containers/documentation.component.ts | 5 +- 3 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 client/src/app/instance/documentation/containers/documentation.component.spec.ts diff --git a/client/src/app/instance/documentation/containers/dataset-list.component.ts b/client/src/app/instance/documentation/containers/dataset-list.component.ts index 88997f50..2b045c3a 100644 --- a/client/src/app/instance/documentation/containers/dataset-list.component.ts +++ b/client/src/app/instance/documentation/containers/dataset-list.component.ts @@ -16,10 +16,9 @@ import * as datasetActions from 'src/app/metamodel/actions/dataset.actions'; import * as datasetFamilySelector from 'src/app/metamodel/selectors/dataset-family.selector'; import * as datasetSelector from 'src/app/metamodel/selectors/dataset.selector'; import { Dataset, DatasetFamily, Survey } from 'src/app/metamodel/models'; -import { environment } from 'src/environments/environment'; -import * as authSelector from "../../../auth/auth.selector"; -import * as instanceSelector from "../../../metamodel/selectors/instance.selector"; -import * as surveySelector from "../../../metamodel/selectors/survey.selector"; +import * as authSelector from '../../../auth/auth.selector'; +import * as instanceSelector from '../../../metamodel/selectors/instance.selector'; +import * as surveySelector from '../../../metamodel/selectors/survey.selector'; @Component({ selector: 'app-dataset-list', diff --git a/client/src/app/instance/documentation/containers/documentation.component.spec.ts b/client/src/app/instance/documentation/containers/documentation.component.spec.ts new file mode 100644 index 00000000..45c440ed --- /dev/null +++ b/client/src/app/instance/documentation/containers/documentation.component.spec.ts @@ -0,0 +1,91 @@ +import { Component, Input } from '@angular/core'; +import { TestBed, waitForAsync, ComponentFixture } from '@angular/core/testing'; +import { RouterTestingModule } from '@angular/router/testing'; + +import { of } from 'rxjs'; +import { provideMockStore, MockStore } from '@ngrx/store/testing'; + +import { DocumentationComponent } from './documentation.component'; +import { AppConfigService } from 'src/app/app-config.service'; +import * as authActions from 'src/app/auth/auth.actions'; +import { Attribute } from '../../../metamodel/models'; +import * as attributeActions from '../../../metamodel/actions/attribute.actions'; + +describe('DocumentationComponent', () => { + @Component({ selector: 'app-spinner', template: '' }) + class SpinnerStubComponent { } + + @Component({ selector: '<app-output-list', template: '' }) + class OutputListStubComponent { + @Input() datasetSelected: string; + @Input() attributeList: Attribute[]; + } + + let component: DocumentationComponent; + let fixture: ComponentFixture<DocumentationComponent>; + let store: MockStore; + let config: AppConfigService + + beforeEach(waitForAsync(() => { + TestBed.configureTestingModule({ + imports: [RouterTestingModule], + declarations: [ + DocumentationComponent, + SpinnerStubComponent, + OutputListStubComponent + ], + providers: [ + provideMockStore({ }), + AppConfigService + ] + }).compileComponents(); + fixture = TestBed.createComponent(DocumentationComponent); + component = fixture.componentInstance; + store = TestBed.inject(MockStore); + config = TestBed.inject(AppConfigService); + })); + + it('should create the component', () => { + expect(component).toBeDefined(); + }); + + it('should execute ngOnInit lifecycle', () => { + const spy = jest.spyOn(store, 'dispatch'); + component.ngOnInit(); + expect(spy).toHaveBeenCalledTimes(1); + expect(spy).toHaveBeenCalledWith(attributeActions.loadAttributeList()); + }); + + it('#getUrlServer() should return server URL address', () => { + // console.log(component.getUrlServer()); + expect(component.getUrlServer()).toBeTruthy(); + }); + + // it('login() should dispatch login action', () => { + // const spy = jest.spyOn(store, 'dispatch'); + // component.login(); + // expect(spy).toHaveBeenCalledTimes(1); + // expect(spy).toHaveBeenCalledWith(authActions.login()); + // }); + // + // it('logout() should dispatch logout action', () => { + // const spy = jest.spyOn(store, 'dispatch'); + // component.logout(); + // expect(spy).toHaveBeenCalledTimes(1); + // expect(spy).toHaveBeenCalledWith(authActions.logout()); + // }); + // + // it('openEditProfile() should dispatch open edit profile action', () => { + // const spy = jest.spyOn(store, 'dispatch'); + // component.openEditProfile(); + // expect(spy).toHaveBeenCalledTimes(1); + // expect(spy).toHaveBeenCalledWith(authActions.openEditProfile()); + // }); + // + // it('isAnisAdmin() should return observable true if user is authenticated', () => { + // component.userRoles = of(['user']); + // component.isAnisAdmin().subscribe(isAuthenticated => expect(isAuthenticated).toBeFalsy()); + // component.userRoles = of(['user', 'anis_admin']); + // component.isAnisAdmin().subscribe(isAuthenticated => expect(isAuthenticated).toBeTruthy()); + // }); +}); diff --git a/client/src/app/instance/documentation/containers/documentation.component.ts b/client/src/app/instance/documentation/containers/documentation.component.ts index 49d8b251..f99dc712 100644 --- a/client/src/app/instance/documentation/containers/documentation.component.ts +++ b/client/src/app/instance/documentation/containers/documentation.component.ts @@ -16,8 +16,8 @@ import * as attributeActions from 'src/app/metamodel/actions/attribute.actions'; import * as datasetSelector from 'src/app/metamodel/selectors/dataset.selector'; import { AppConfigService } from 'src/app/app-config.service'; import { Attribute } from 'src/app/metamodel/models'; -import * as instanceSelector from "../../../metamodel/selectors/instance.selector"; -import * as attributeSelector from "../../../metamodel/selectors/attribute.selector"; +import * as instanceSelector from '../../../metamodel/selectors/instance.selector'; +import * as attributeSelector from '../../../metamodel/selectors/attribute.selector'; @Component({ selector: 'app-documentation', @@ -55,6 +55,7 @@ export class DocumentationComponent implements OnInit { * @return string */ getUrlServer(): string { + console.log(this.config); if (!this.config.apiUrl.startsWith('http')) { const url = window.location; return url.protocol + '//' + url.host + this.config.apiUrl; -- GitLab