Skip to content

Commit

Permalink
test(ontology): set correct test environment
Browse files Browse the repository at this point in the history
test(ontology): enable all tests
  • Loading branch information
kilchenmann committed Jun 17, 2021
1 parent 4aa4b43 commit 6c87033
Showing 1 changed file with 93 additions and 12 deletions.
105 changes: 93 additions & 12 deletions src/app/project/ontology/ontology.component.spec.ts
@@ -1,5 +1,5 @@
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { waitForAsync, ComponentFixture, TestBed } from '@angular/core/testing';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { ReactiveFormsModule } from '@angular/forms';
import { MatCardModule } from '@angular/material/card';
import { MatOptionModule } from '@angular/material/core';
Expand All @@ -14,34 +14,53 @@ import { MatTooltipModule } from '@angular/material/tooltip';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ActivatedRoute } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { KnoraApiConnection } from '@dasch-swiss/dsp-js';
import { ApiResponseData, CanDoResponse, ListNodeInfo, ListsEndpointAdmin, ListsResponse, MockList, MockOntology, OntologiesEndpointV2, OntologiesMetadata, ReadOntology } from '@dasch-swiss/dsp-js';
import {
AppInitService,
DspActionModule,
DspApiConfigToken,
DspApiConnectionToken,
DspActionModule, DspApiConnectionToken,
DspCoreModule
} from '@dasch-swiss/dsp-ui';
import { of } from 'rxjs';
import { AjaxResponse } from 'rxjs/ajax';
import { CacheService } from 'src/app/main/cache/cache.service';
import { DialogComponent } from 'src/app/main/dialog/dialog.component';
import { ErrorComponent } from 'src/app/main/error/error.component';
import { TestConfig } from 'test.config';
import { OntologyVisualizerComponent } from './ontology-visualizer/ontology-visualizer.component';
import { OntologyComponent } from './ontology.component';
import { PropertyInfoComponent } from './property-info/property-info.component';
import { ResourceClassInfoComponent } from './resource-class-info/resource-class-info.component';

describe('OntologyComponent', () => {
let component: OntologyComponent;
let fixture: ComponentFixture<OntologyComponent>;

beforeEach(waitForAsync(() => {
const ontologyEndpointSpyObj = {
admin: {
listsEndpoint: jasmine.createSpyObj('listsEndpoint', ['getListsInProject'])
},
v2: {
onto: jasmine.createSpyObj('onto', [
'getOntologiesByProjectIri',
'getOntology',
'canDeleteOntology',
'deleteOntology',
'deleteResourceClass',
'deleteResourceProperty'
])
}
};

const cacheServiceSpy = jasmine.createSpyObj('CacheService', ['get', 'set']);

TestBed.configureTestingModule({
declarations: [
OntologyComponent,
OntologyVisualizerComponent,
DialogComponent,
ErrorComponent,
PropertyInfoComponent
PropertyInfoComponent,
ResourceClassInfoComponent
],
imports: [
BrowserAnimationsModule,
Expand Down Expand Up @@ -76,14 +95,13 @@ describe('OntologyComponent', () => {
}
}
},
AppInitService,
{
provide: DspApiConfigToken,
useValue: TestConfig.ApiConfig
provide: DspApiConnectionToken,
useValue: ontologyEndpointSpyObj
},
{
provide: DspApiConnectionToken,
useValue: new KnoraApiConnection(TestConfig.ApiConfig)
provide: CacheService,
useValue: cacheServiceSpy
}
]
})
Expand Down Expand Up @@ -111,8 +129,71 @@ describe('OntologyComponent', () => {
});

beforeEach(() => {
// set local storage session data
localStorage.setItem('session', JSON.stringify(TestConfig.CurrentSession));

// set cache with current ontology
const cacheSpy = TestBed.inject(CacheService);

(cacheSpy as jasmine.SpyObj<CacheService>).get.and.callFake(
() => {
const response: ReadOntology = MockOntology.mockReadOntology('http://0.0.0.0:3333/ontology/0001/anything/v2');
return of(response);
}
);

// can delete ontology request
const dspConnSpy = TestBed.inject(DspApiConnectionToken);
(dspConnSpy.v2.onto as jasmine.SpyObj<OntologiesEndpointV2>).canDeleteOntology.and.callFake(
() => {
const deleteResClass: CanDoResponse = {
'canDo': false
};

return of(deleteResClass);
}
);

(dspConnSpy.v2.onto as jasmine.SpyObj<OntologiesEndpointV2>).getOntologiesByProjectIri.and.callFake(
() => {
const response: OntologiesMetadata = MockOntology.mockOntologiesMetadata();
return of(response);
}
);

(dspConnSpy.v2.onto as jasmine.SpyObj<OntologiesEndpointV2>).getOntology.and.callFake(
() => {
const response: ReadOntology = MockOntology.mockReadOntology('http://0.0.0.0:3333/ontology/0001/anything/v2');
return of(response);
}
);
(dspConnSpy.admin.listsEndpoint as jasmine.SpyObj<ListsEndpointAdmin>).getListsInProject.and.callFake(
() => {
const response = new ListsResponse();

response.lists = new Array<ListNodeInfo>();

const mockList1 = new ListNodeInfo();
mockList1.comments = [];
mockList1.id = 'http://rdfh.ch/lists/0001/mockList01';
mockList1.isRootNode = true;
mockList1.labels = [{ language: 'en', value: 'Mock List 01' }];
mockList1.projectIri = 'http://rdfh.ch/projects/myProjectIri';

const mockList2 = new ListNodeInfo();
mockList2.comments = [];
mockList2.id = 'http://rdfh.ch/lists/0001/mockList02';
mockList2.isRootNode = true;
mockList2.labels = [{ language: 'en', value: 'Mock List 02' }];
mockList2.projectIri = 'http://rdfh.ch/projects/myProjectIri';

response.lists.push(mockList1, mockList2);

return of(ApiResponseData.fromAjaxResponse({ response } as AjaxResponse));
}
);


fixture = TestBed.createComponent(OntologyComponent);
component = fixture.componentInstance;
fixture.detectChanges();
Expand Down

0 comments on commit 6c87033

Please sign in to comment.