Added tests

This commit is contained in:
Giuseppe Digilio
2019-01-12 15:56:56 +01:00
parent 9898d2a90c
commit 54f80a6e45
10 changed files with 673 additions and 121 deletions

View File

@@ -1,81 +1,72 @@
// Load the implementations that should be tested
import { ChangeDetectorRef, Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ActivatedRoute, Router } from '@angular/router';
import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { async, ComponentFixture, inject, TestBed, } from '@angular/core/testing';
import { BrowserModule } from '@angular/platform-browser';
import { By } from '@angular/platform-browser';
import { of as observableOf } from 'rxjs';
import { ActionsSubject, Store } from '@ngrx/store';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { TranslateModule } from '@ngx-translate/core';
import { ScrollToService } from '@nicky-lenaers/ngx-scroll-to';
import { SectionContainerComponent } from './section-container.component';
import { createTestComponent } from '../../../shared/testing/utils';
import { SectionsType } from '../sections-type';
import { SectionsDirective } from '../sections.directive';
import { SubmissionState } from '../../submission.reducers';
import { SubmissionService } from '../../submission.service';
import { GLOBAL_CONFIG } from '../../../../config';
import { SubmissionRestService } from '../../submission-rest.service';
import { SubmissionRestServiceStub } from '../../../shared/testing/submission-rest-service-stub';
import { MockRouter } from '../../../shared/mocks/mock-router';
import { RouteService } from '../../../shared/services/route.service';
import { MockActivatedRoute } from '../../../shared/mocks/mock-active-router';
import { SectionsService } from '../sections.service';
import { NotificationsService } from '../../../shared/notifications/notifications.service';
import { SubmissionServiceStub } from '../../../shared/testing/submission-service-stub';
import { SectionsServiceStub } from '../../../shared/testing/sections-service-stub';
import { SectionDataObject } from '../models/section-data.model';
import { mockSubmissionCollectionId, mockSubmissionId } from '../../../shared/mocks/mock-submission';
const sectionState = {
header: 'submit.progressbar.describe.stepone',
config: 'https://rest.api/dspace-spring-rest/api/config/submissionforms/traditionalpageone',
mandatory: true,
sectionType: 'submission-form',
collapsed: false,
enabled: true,
data: {},
errors: [],
isLoading: false,
isValid: false
} as any;
const sectionObject: SectionDataObject = {
config: 'https://dspace7.4science.it/or2018/api/config/submissionforms/traditionalpageone',
mandatory: true,
data: {},
errors: [],
header: 'submit.progressbar.describe.stepone',
id: 'traditionalpageone',
sectionType: SectionsType.SubmissionForm
};
describe('SectionContainerComponent test suite', () => {
let testComp: TestComponent;
let testFixture: ComponentFixture<TestComponent>;
let html;
let comp: SectionContainerComponent;
let compAsAny: any;
let fixture: ComponentFixture<SectionContainerComponent>;
const config = {
autosave: {
metadata: ['dc.title', 'dc.identifier.doi', 'dc.identifier.pmid', 'dc.identifier.arxiv'],
timer: 5
},
metadata: {
icons: [
{
name: 'dc.contributor.author',
config: {
withAuthority:{
style: 'fa-user'
}
}
},
{
name: 'local.contributor.affiliation',
config: {
withAuthority:{
style: 'fa-university'
},
withoutAuthority:{
style: 'fa-university text-muted'
}
}
},
{
name: 'default',
config: {}
}
]
}
} as any;
const restService = new SubmissionRestServiceStub();
const router = new MockRouter();
const store = new Store<SubmissionState>(observableOf({}), new ActionsSubject(), undefined);
let submissionServiceStub: SubmissionServiceStub;
let sectionsServiceStub: SectionsServiceStub;
const submissionId = mockSubmissionId;
const collectionId = mockSubmissionCollectionId;
function init() {
sectionsServiceStub = TestBed.get(SectionsService);
submissionServiceStub = TestBed.get(SubmissionService);
sectionsServiceStub.isSectionValid.and.returnValue(observableOf(true));
sectionsServiceStub.getSectionState.and.returnValue(observableOf(sectionState));
submissionServiceStub.getActiveSectionId.and.returnValue(observableOf('traditionalpageone'));
}
// async beforeEach
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
BrowserModule,
CommonModule,
NgbModule.forRoot(),
TranslateModule.forRoot()
],
@@ -85,41 +76,149 @@ describe('SectionContainerComponent test suite', () => {
TestComponent,
], // declare the test component
providers: [
ChangeDetectorRef,
NotificationsService,
RouteService,
ScrollToService,
SectionContainerComponent,
SectionsService,
SubmissionService,
{ provide: ActivatedRoute, useValue: new MockActivatedRoute() },
{ provide: Router, useValue: router },
{ provide: SubmissionRestService, useValue: restService },
{ provide: GLOBAL_CONFIG, useValue: config },
{ provide: Store, useValue: store }
{ provide: SectionsService, useClass: SectionsServiceStub },
{ provide: SubmissionService, useClass: SubmissionServiceStub },
SectionContainerComponent
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
});
}).compileComponents();
}));
describe('', () => {
let testComp: TestComponent;
let testFixture: ComponentFixture<TestComponent>;
let html;
// synchronous beforeEach
beforeEach(() => {
html = `
<ds-submission-form-section-container [collectionId]="collectionId"
[submissionId]="submissionId"
[sectionData]="object"></ds-submission-form-section-container>`;
<ds-submission-form-section-container [collectionId]="collectionId"
[submissionId]="submissionId"
[sectionData]="object"></ds-submission-form-section-container>`;
testFixture = createTestComponent(html, TestComponent) as ComponentFixture<TestComponent>;
testComp = testFixture.componentInstance;
init();
});
it('should create Chips Component', inject([SectionContainerComponent], (app: SectionContainerComponent) => {
it('should create SectionContainerComponent', inject([SectionContainerComponent], (app: SectionContainerComponent) => {
expect(app).toBeDefined();
}));
});
describe('', () => {
beforeEach(() => {
init();
fixture = TestBed.createComponent(SectionContainerComponent);
comp = fixture.componentInstance;
compAsAny = comp;
comp.submissionId = submissionId;
comp.collectionId = collectionId;
comp.sectionData = sectionObject;
spyOn(comp, 'getSectionContent');
fixture.detectChanges();
});
afterEach(() => {
fixture.destroy();
comp = null;
compAsAny = null;
});
it('should inject section properly', () => {
spyOn(comp.sectionRef, 'isEnabled').and.returnValue(observableOf(true));
spyOn(comp.sectionRef, 'hasGenericErrors').and.returnValue(false);
comp.ngOnInit();
fixture.detectChanges();
const section = fixture.debugElement.query(By.css('[id^=\'sectionContent_\']'));
expect(comp.getSectionContent).toHaveBeenCalled();
expect(section).toBeDefined();
});
it('should call removeSection properly', () => {
const mockEvent = jasmine.createSpyObj('event', {
preventDefault: jasmine.createSpy('preventDefault'),
stopPropagation: jasmine.createSpy('stopPropagation'),
});
spyOn(comp.sectionRef, 'removeSection');
comp.removeSection(mockEvent);
expect(mockEvent.preventDefault).toHaveBeenCalled();
expect(mockEvent.stopPropagation).toHaveBeenCalled();
expect(comp.sectionRef.removeSection).toHaveBeenCalledWith(submissionId, 'traditionalpageone');
});
it('should display generic section errors div', () => {
let sectionErrorsDiv = fixture.debugElement.query(By.css('[id^=\'sectionGenericError_\']'));
expect(sectionErrorsDiv).toBeNull();
spyOn(comp.sectionRef, 'isEnabled').and.returnValue(observableOf(true));
spyOn(comp.sectionRef, 'hasGenericErrors').and.returnValue(true);
comp.ngOnInit();
fixture.detectChanges();
sectionErrorsDiv = fixture.debugElement.query(By.css('[id^=\'sectionGenericError_\']'));
expect(sectionErrorsDiv).toBeDefined();
});
it('should display warning icon', () => {
spyOn(comp.sectionRef, 'isEnabled').and.returnValue(observableOf(true));
spyOn(comp.sectionRef, 'isValid').and.returnValue(observableOf(false));
spyOn(comp.sectionRef, 'hasErrors').and.returnValue(false);
comp.ngOnInit();
fixture.detectChanges();
const iconWarn = fixture.debugElement.query(By.css('i.text-warning'));
const iconErr = fixture.debugElement.query(By.css('i.text-danger'));
const iconSuccess = fixture.debugElement.query(By.css('i.text-success'));
expect(iconWarn).toBeDefined();
expect(iconErr).toBeNull();
expect(iconSuccess).toBeNull();
});
it('should display error icon', () => {
spyOn(comp.sectionRef, 'isEnabled').and.returnValue(observableOf(true));
spyOn(comp.sectionRef, 'isValid').and.returnValue(observableOf(false));
spyOn(comp.sectionRef, 'hasErrors').and.returnValue(true);
comp.ngOnInit();
fixture.detectChanges();
const iconWarn = fixture.debugElement.query(By.css('i.text-warning'));
const iconErr = fixture.debugElement.query(By.css('i.text-danger'));
const iconSuccess = fixture.debugElement.query(By.css('i.text-success'));
expect(iconWarn).toBeNull();
expect(iconErr).toBeDefined();
expect(iconSuccess).toBeNull();
});
it('should display success icon', () => {
spyOn(comp.sectionRef, 'isEnabled').and.returnValue(observableOf(true));
spyOn(comp.sectionRef, 'isValid').and.returnValue(observableOf(true));
spyOn(comp.sectionRef, 'hasErrors').and.returnValue(false);
comp.ngOnInit();
fixture.detectChanges();
const iconWarn = fixture.debugElement.query(By.css('i.text-warning'));
const iconErr = fixture.debugElement.query(By.css('i.text-danger'));
const iconSuccess = fixture.debugElement.query(By.css('i.text-success'));
expect(iconWarn).toBeNull();
expect(iconErr).toBeNull();
expect(iconSuccess).toBeDefined();
});
});
});
// declare a test component
@@ -130,14 +229,6 @@ describe('SectionContainerComponent test suite', () => {
class TestComponent {
public collectionId = '1c11f3f1-ba1f-4f36-908a-3f1ea9a557eb';
public submissionId = 1;
public object = {
config: 'https://dspace7.4science.it/or2018/api/config/submissionforms/traditionalpageone',
mandatory: true,
data: {},
errors: [],
header: 'submit.progressbar.describe.stepone',
id: 'traditionalpageone',
sectionType: SectionsType.SubmissionForm
}
public submissionId = mockSubmissionId;
public object = sectionObject;
}