mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 18:14:17 +00:00
Added tests
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
import { ChangeDetectorRef, NO_ERRORS_SCHEMA } from '@angular/core';
|
||||
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { of as observableOf } from 'rxjs';
|
||||
import { TranslateModule, TranslateService } from '@ngx-translate/core';
|
||||
import { Store } from '@ngrx/store';
|
||||
|
||||
import { SubmissionServiceStub } from '../../../shared/testing/submission-service-stub';
|
||||
import {
|
||||
mockSectionsData,
|
||||
mockSubmissionCollectionId,
|
||||
mockSubmissionId,
|
||||
mockSubmissionObject,
|
||||
mockUploadResponse1ParsedErrors,
|
||||
mockUploadResponse2Errors,
|
||||
mockUploadResponse2ParsedErrors
|
||||
} from '../../../shared/mocks/mock-submission';
|
||||
import { SubmissionService } from '../../submission.service';
|
||||
|
||||
import { SectionsServiceStub } from '../../../shared/testing/sections-service-stub';
|
||||
import { SectionsService } from '../../sections/sections.service';
|
||||
import { SubmissionUploadFilesComponent } from './submission-upload-files.component';
|
||||
import { NotificationsService } from '../../../shared/notifications/notifications.service';
|
||||
import { NotificationsServiceStub } from '../../../shared/testing/notifications-service-stub';
|
||||
import { getMockTranslateService } from '../../../shared/mocks/mock-translate.service';
|
||||
import { cold, hot } from 'jasmine-marbles';
|
||||
import { SubmissionJsonPatchOperationsServiceStub } from '../../../shared/testing/submission-json-patch-operations-service-stub';
|
||||
import { SubmissionJsonPatchOperationsService } from '../../../core/submission/submission-json-patch-operations.service';
|
||||
import { SharedModule } from '../../../shared/shared.module';
|
||||
|
||||
describe('SubmissionUploadFilesComponent Component', () => {
|
||||
|
||||
let comp: SubmissionUploadFilesComponent;
|
||||
let compAsAny: any;
|
||||
let fixture: ComponentFixture<SubmissionUploadFilesComponent>;
|
||||
let submissionServiceStub: SubmissionServiceStub;
|
||||
let sectionsServiceStub: SectionsServiceStub;
|
||||
let notificationsServiceStub: NotificationsServiceStub;
|
||||
let translateService: any;
|
||||
|
||||
const submissionJsonPatchOperationsServiceStub = new SubmissionJsonPatchOperationsServiceStub();
|
||||
const submissionId = mockSubmissionId;
|
||||
const collectionId = mockSubmissionCollectionId;
|
||||
const uploadRestResponse: any = mockSubmissionObject;
|
||||
|
||||
const store: any = jasmine.createSpyObj('store', {
|
||||
dispatch: jasmine.createSpy('dispatch'),
|
||||
select: jasmine.createSpy('select')
|
||||
});
|
||||
|
||||
beforeEach(async(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [
|
||||
SharedModule,
|
||||
TranslateModule.forRoot()
|
||||
],
|
||||
declarations: [SubmissionUploadFilesComponent],
|
||||
providers: [
|
||||
{ provide: NotificationsService, useClass: NotificationsServiceStub },
|
||||
{ provide: SubmissionService, useClass: SubmissionServiceStub },
|
||||
{ provide: SectionsService, useClass: SectionsServiceStub },
|
||||
{ provide: TranslateService, useValue: getMockTranslateService() },
|
||||
{ provide: SubmissionJsonPatchOperationsService, useValue: submissionJsonPatchOperationsServiceStub },
|
||||
{ provide: Store, useValue: store },
|
||||
ChangeDetectorRef
|
||||
],
|
||||
schemas: [NO_ERRORS_SCHEMA]
|
||||
}).compileComponents();
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(SubmissionUploadFilesComponent);
|
||||
comp = fixture.componentInstance;
|
||||
compAsAny = comp;
|
||||
submissionServiceStub = TestBed.get(SubmissionService);
|
||||
sectionsServiceStub = TestBed.get(SectionsService);
|
||||
notificationsServiceStub = TestBed.get(NotificationsService);
|
||||
translateService = TestBed.get(TranslateService);
|
||||
comp.submissionId = submissionId;
|
||||
comp.collectionId = collectionId;
|
||||
comp.sectionId = 'upload';
|
||||
comp.uploadFilesOptions = {
|
||||
url: '',
|
||||
authToken: null,
|
||||
disableMultipart: false,
|
||||
itemAlias: null
|
||||
};
|
||||
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
comp = null;
|
||||
compAsAny = null;
|
||||
fixture = null;
|
||||
submissionServiceStub = null;
|
||||
sectionsServiceStub = null;
|
||||
notificationsServiceStub = null;
|
||||
translateService = null;
|
||||
});
|
||||
|
||||
it('should init uploadEnabled properly', () => {
|
||||
sectionsServiceStub.isSectionAvailable.and.returnValue(hot('-a-b', {
|
||||
a: false,
|
||||
b: true
|
||||
}));
|
||||
|
||||
const expected = cold('-c-d', {
|
||||
c: false,
|
||||
d: true
|
||||
});
|
||||
|
||||
comp.ngOnChanges();
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(compAsAny.uploadEnabled).toBeObservable(expected);
|
||||
});
|
||||
|
||||
it('should show a success notification and call updateSectionData on upload complete', () => {
|
||||
|
||||
const expectedErrors: any = mockUploadResponse1ParsedErrors;
|
||||
compAsAny.uploadEnabled = observableOf(true);
|
||||
fixture.detectChanges();
|
||||
|
||||
comp.onCompleteItem(Object.assign({}, uploadRestResponse, { sections: mockSectionsData }));
|
||||
|
||||
Object.keys(mockSectionsData).forEach((sectionId) => {
|
||||
expect(sectionsServiceStub.updateSectionData).toHaveBeenCalledWith(
|
||||
submissionId,
|
||||
sectionId,
|
||||
mockSectionsData[sectionId],
|
||||
expectedErrors[sectionId]
|
||||
);
|
||||
});
|
||||
|
||||
expect(notificationsServiceStub.success).toHaveBeenCalled();
|
||||
|
||||
});
|
||||
|
||||
it('should show an error notification and call updateSectionData on upload complete', () => {
|
||||
|
||||
const responseErrors = mockUploadResponse2Errors;
|
||||
|
||||
const expectedErrors: any = mockUploadResponse2ParsedErrors;
|
||||
compAsAny.uploadEnabled = observableOf(true);
|
||||
fixture.detectChanges();
|
||||
|
||||
comp.onCompleteItem(Object.assign({}, uploadRestResponse, {
|
||||
sections: mockSectionsData,
|
||||
errors: responseErrors.errors
|
||||
}));
|
||||
|
||||
Object.keys(mockSectionsData).forEach((sectionId) => {
|
||||
expect(sectionsServiceStub.updateSectionData).toHaveBeenCalledWith(
|
||||
submissionId,
|
||||
sectionId,
|
||||
mockSectionsData[sectionId],
|
||||
expectedErrors[sectionId]
|
||||
);
|
||||
});
|
||||
|
||||
expect(notificationsServiceStub.success).not.toHaveBeenCalled();
|
||||
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user