[CST-3090] fix tests

This commit is contained in:
Danilo Di Nuzzo
2020-06-18 15:38:03 +02:00
parent 709726e041
commit f841e45019
5 changed files with 237 additions and 8 deletions

View File

@@ -1,5 +1,5 @@
import { ChangeDetectorRef, Component, NO_ERRORS_SCHEMA } from '@angular/core'; import { ChangeDetectorRef, Component, NO_ERRORS_SCHEMA } from '@angular/core';
import { async, ComponentFixture, inject, TestBed } from '@angular/core/testing'; import { async, ComponentFixture, inject, TestBed, tick, fakeAsync } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing'; import { RouterTestingModule } from '@angular/router/testing';
import { Store } from '@ngrx/store'; import { Store } from '@ngrx/store';
@@ -21,6 +21,8 @@ import { NotificationsServiceStub } from '../../shared/testing/notifications-ser
import { SharedModule } from '../../shared/shared.module'; import { SharedModule } from '../../shared/shared.module';
import { getMockScrollToService } from '../../shared/mocks/scroll-to-service.mock'; import { getMockScrollToService } from '../../shared/mocks/scroll-to-service.mock';
import { UploaderService } from '../../shared/uploader/uploader.service'; import { UploaderService } from '../../shared/uploader/uploader.service';
import { By } from '@angular/platform-browser';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
describe('MyDSpaceNewSubmissionComponent test', () => { describe('MyDSpaceNewSubmissionComponent test', () => {
@@ -54,6 +56,11 @@ describe('MyDSpaceNewSubmissionComponent test', () => {
{ provide: ScrollToService, useValue: getMockScrollToService() }, { provide: ScrollToService, useValue: getMockScrollToService() },
{ provide: Store, useValue: store }, { provide: Store, useValue: store },
{ provide: TranslateService, useValue: translateService }, { provide: TranslateService, useValue: translateService },
{
provide: NgbModal, useValue: {
open: () => {/*comment*/}
}
},
ChangeDetectorRef, ChangeDetectorRef,
MyDSpaceNewSubmissionComponent, MyDSpaceNewSubmissionComponent,
UploaderService UploaderService
@@ -86,6 +93,25 @@ describe('MyDSpaceNewSubmissionComponent test', () => {
})); }));
}); });
describe('', () => {
let fixture: ComponentFixture<MyDSpaceNewSubmissionComponent>;
let comp: MyDSpaceNewSubmissionComponent;
beforeEach(() => {
fixture = TestBed.createComponent(MyDSpaceNewSubmissionComponent);
comp = fixture.componentInstance;
});
it('should call app.openDialog', () => {
spyOn(comp, 'openDialog');
const submissionButton = fixture.debugElement.query(By.css('button.btn-primary'));
submissionButton.triggerEventHandler('click', {
preventDefault: () => {/**/
}
});
expect(comp.openDialog).toHaveBeenCalled();
});
});
}); });
// declare a test component // declare a test component

View File

@@ -13,13 +13,19 @@ import { RequestEntry } from './request.reducer';
import { ErrorResponse, RestResponse } from '../cache/response.models'; import { ErrorResponse, RestResponse } from '../cache/response.models';
import { ObjectCacheService } from '../cache/object-cache.service'; import { ObjectCacheService } from '../cache/object-cache.service';
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service'; import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
import { Collection } from '../shared/collection.model';
import { PageInfo } from '../shared/page-info.model';
import { PaginatedList } from './paginated-list';
import { createSuccessfulRemoteDataObject } from 'src/app/shared/remote-data.utils';
import { hot, getTestScheduler, cold } from 'jasmine-marbles';
import { TestScheduler } from 'rxjs/testing';
const url = 'fake-url'; const url = 'fake-url';
const collectionId = 'fake-collection-id'; const collectionId = 'fake-collection-id';
describe('CollectionDataService', () => { describe('CollectionDataService', () => {
let service: CollectionDataService; let service: CollectionDataService;
let scheduler: TestScheduler;
let requestService: RequestService; let requestService: RequestService;
let translate: TranslateService; let translate: TranslateService;
let notificationsService: any; let notificationsService: any;
@@ -27,6 +33,44 @@ describe('CollectionDataService', () => {
let objectCache: ObjectCacheService; let objectCache: ObjectCacheService;
let halService: any; let halService: any;
const mockCollection1: Collection = Object.assign(new Collection(), {
id: 'test-collection-1-1',
name: 'test-collection-1',
_links: {
self: {
href: 'https://rest.api/collections/test-collection-1-1'
}
}
});
const mockCollection2: Collection = Object.assign(new Collection(), {
id: 'test-collection-2-2',
name: 'test-collection-2',
_links: {
self: {
href: 'https://rest.api/collections/test-collection-2-2'
}
}
});
const mockCollection3: Collection = Object.assign(new Collection(), {
id: 'test-collection-3-3',
name: 'test-collection-3',
_links: {
self: {
href: 'https://rest.api/collections/test-collection-3-3'
}
}
});
const queryString = 'test-string';
const communityId = '8b3c613a-5a4b-438b-9686-be1d5b4a1c5a';
const pageInfo = new PageInfo();
const array = [mockCollection1, mockCollection2, mockCollection3];
const paginatedList = new PaginatedList(pageInfo, array);
const paginatedListRD = createSuccessfulRemoteDataObject(paginatedList);
describe('when the requests are successful', () => { describe('when the requests are successful', () => {
beforeEach(() => { beforeEach(() => {
createService(); createService();
@@ -74,6 +118,43 @@ describe('CollectionDataService', () => {
}); });
}); });
describe('when calling getAuthorizedCollection', () => {
beforeEach(() => {
scheduler = getTestScheduler();
spyOn(service, 'getAuthorizedCollection').and.callThrough();
spyOn(service, 'getAuthorizedCollectionByCommunity').and.callThrough();
});
it('should proxy the call to getAuthorizedCollection', () => {
scheduler.schedule(() => service.getAuthorizedCollection(queryString));
scheduler.flush();
expect(service.getAuthorizedCollection).toHaveBeenCalledWith(queryString);
});
it('should return a RemoteData<PaginatedList<Colletion>> for the getAuthorizedCollection', () => {
const result = service.getAuthorizedCollection(queryString)
const expected = cold('a|', {
a: paginatedListRD
});
expect(result).toBeObservable(expected);
});
it('should proxy the call to getAuthorizedCollectionByCommunity', () => {
scheduler.schedule(() => service.getAuthorizedCollectionByCommunity(communityId, queryString));
scheduler.flush();
expect(service.getAuthorizedCollectionByCommunity).toHaveBeenCalledWith(communityId, queryString);
});
it('should return a RemoteData<PaginatedList<Colletion>> for the getAuthorizedCollectionByCommunity', () => {
const result = service.getAuthorizedCollectionByCommunity(communityId, queryString)
const expected = cold('a|', {
a: paginatedListRD
});
expect(result).toBeObservable(expected);
});
});
}); });
describe('when the requests are unsuccessful', () => { describe('when the requests are unsuccessful', () => {
@@ -117,7 +198,9 @@ describe('CollectionDataService', () => {
function createService(requestEntry$?) { function createService(requestEntry$?) {
requestService = getMockRequestService(requestEntry$); requestService = getMockRequestService(requestEntry$);
rdbService = jasmine.createSpyObj('rdbService', { rdbService = jasmine.createSpyObj('rdbService', {
buildList: jasmine.createSpy('buildList') buildList: hot('a|', {
a: paginatedListRD
})
}); });
objectCache = jasmine.createSpyObj('objectCache', { objectCache = jasmine.createSpyObj('objectCache', {
remove: jasmine.createSpy('remove') remove: jasmine.createSpy('remove')

View File

@@ -167,6 +167,21 @@ describe('CollectionDropdownComponent', () => {
}); });
})); }));
it('should init component with collection list', fakeAsync(() => {
spyOn(component.subs, 'push').and.callThrough();
spyOn(component, 'resetPagination').and.callThrough();
spyOn(component, 'populateCollectionList').and.callThrough();
component.ngOnInit();
tick();
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(component.subs.push).toHaveBeenCalled();
expect(component.resetPagination).toHaveBeenCalled();
expect(component.populateCollectionList).toHaveBeenCalled();
});
}));
it('should emit collectionChange event when selecting a new collection', () => { it('should emit collectionChange event when selecting a new collection', () => {
spyOn(component.selectionChange, 'emit').and.callThrough(); spyOn(component.selectionChange, 'emit').and.callThrough();
component.ngOnInit(); component.ngOnInit();
@@ -177,6 +192,7 @@ describe('CollectionDropdownComponent', () => {
}); });
it('should reset collections list after reset of searchField', fakeAsync(() => { it('should reset collections list after reset of searchField', fakeAsync(() => {
spyOn(component.subs, 'push').and.callThrough();
spyOn(component, 'reset').and.callThrough(); spyOn(component, 'reset').and.callThrough();
spyOn(component.searchField, 'setValue').and.callThrough(); spyOn(component.searchField, 'setValue').and.callThrough();
spyOn(component, 'resetPagination').and.callThrough(); spyOn(component, 'resetPagination').and.callThrough();
@@ -187,7 +203,7 @@ describe('CollectionDropdownComponent', () => {
el.value = searchedCollection; el.value = searchedCollection;
el.dispatchEvent(new Event('input')); el.dispatchEvent(new Event('input'));
fixture.detectChanges(); fixture.detectChanges();
tick(250); tick(500);
fixture.whenStable().then(() => { fixture.whenStable().then(() => {
expect(component.reset).toHaveBeenCalled(); expect(component.reset).toHaveBeenCalled();
@@ -195,6 +211,31 @@ describe('CollectionDropdownComponent', () => {
expect(component.resetPagination).toHaveBeenCalled(); expect(component.resetPagination).toHaveBeenCalled();
expect(component.currentQuery).toEqual(''); expect(component.currentQuery).toEqual('');
expect(component.populateCollectionList).toHaveBeenCalledWith(component.currentQuery, component.currentPage); expect(component.populateCollectionList).toHaveBeenCalledWith(component.currentQuery, component.currentPage);
expect(component.searchListCollection).toEqual(collections as any);
expect(component.subs.push).toHaveBeenCalled();
}); });
})); }));
it('should reset searchField when dropdown menu has been closed', () => {
spyOn(component.searchField, 'setValue').and.callThrough();
component.reset();
expect(component.searchField.setValue).toHaveBeenCalled();
});
it('should change loader status', () => {
spyOn(component.isLoadingList, 'next').and.callThrough();
component.hideShowLoader(true);
expect(component.isLoadingList.next).toHaveBeenCalledWith(true);
});
it('reset pagination fields', () => {
component.resetPagination();
expect(component.currentPage).toEqual(1);
expect(component.currentQuery).toEqual('');
expect(component.hasNextPage).toEqual(true);
expect(component.searchListCollection).toEqual([]);
});
}); });

View File

@@ -64,7 +64,7 @@ export class CollectionDropdownComponent implements OnInit, OnDestroy {
* Array to track all subscriptions and unsubscribe them onDestroy * Array to track all subscriptions and unsubscribe them onDestroy
* @type {Array} * @type {Array}
*/ */
private subs: Subscription[] = []; public subs: Subscription[] = [];
/** /**
* The list of collection to render * The list of collection to render

View File

@@ -8,7 +8,7 @@ import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { Store } from '@ngrx/store'; import { Store } from '@ngrx/store';
import { SubmissionServiceStub } from '../../../shared/testing/submission-service.stub'; import { SubmissionServiceStub } from '../../../shared/testing/submission-service.stub';
import { mockSubmissionId } from '../../../shared/mocks/submission.mock'; import { mockSubmissionId, mockSubmissionRestResponse } from '../../../shared/mocks/submission.mock';
import { SubmissionService } from '../../submission.service'; import { SubmissionService } from '../../submission.service';
import { SubmissionFormCollectionComponent } from './submission-form-collection.component'; import { SubmissionFormCollectionComponent } from './submission-form-collection.component';
import { CommunityDataService } from '../../../core/data/community-data.service'; import { CommunityDataService } from '../../../core/data/community-data.service';
@@ -18,12 +18,13 @@ import { JsonPatchOperationsBuilder } from '../../../core/json-patch/builder/jso
import { JsonPatchOperationPathCombiner } from '../../../core/json-patch/builder/json-patch-operation-path-combiner'; import { JsonPatchOperationPathCombiner } from '../../../core/json-patch/builder/json-patch-operation-path-combiner';
import { createTestComponent } from '../../../shared/testing/utils.test'; import { createTestComponent } from '../../../shared/testing/utils.test';
import { CollectionDataService } from '../../../core/data/collection-data.service'; import { CollectionDataService } from '../../../core/data/collection-data.service';
import { hot } from 'jasmine-marbles'; import { hot, cold } from 'jasmine-marbles';
import { of } from 'rxjs'; import { of } from 'rxjs';
import { SectionsService } from '../../sections/sections.service'; import { SectionsService } from '../../sections/sections.service';
import { componentFactoryName } from '@angular/compiler'; import { componentFactoryName } from '@angular/compiler';
import { Collection } from 'src/app/core/shared/collection.model';
fdescribe('SubmissionFormCollectionComponent Component', () => { describe('SubmissionFormCollectionComponent Component', () => {
let comp: SubmissionFormCollectionComponent; let comp: SubmissionFormCollectionComponent;
let compAsAny: any; let compAsAny: any;
@@ -34,6 +35,58 @@ fdescribe('SubmissionFormCollectionComponent Component', () => {
const submissionId = mockSubmissionId; const submissionId = mockSubmissionId;
const collectionId = '1234567890-1'; const collectionId = '1234567890-1';
const definition = 'traditional'; const definition = 'traditional';
const submissionRestResponse = mockSubmissionRestResponse;
const mockCollectionList = [
{
communities: [
{
id: '123456789-1',
name: 'Community 1'
}
],
collection: {
id: '1234567890-1',
name: 'Community 1-Collection 1'
}
},
{
communities: [
{
id: '123456789-1',
name: 'Community 1'
}
],
collection: {
id: '1234567890-2',
name: 'Community 1-Collection 2'
}
},
{
communities: [
{
id: '123456789-2',
name: 'Community 2'
}
],
collection: {
id: '1234567890-3',
name: 'Community 2-Collection 1'
}
},
{
communities: [
{
id: '123456789-2',
name: 'Community 2'
}
],
collection: {
id: '1234567890-4',
name: 'Community 2-Collection 2'
}
}
];
const communityDataService: any = jasmine.createSpyObj('communityDataService', { const communityDataService: any = jasmine.createSpyObj('communityDataService', {
findAll: jasmine.createSpy('findAll') findAll: jasmine.createSpy('findAll')
@@ -180,6 +233,32 @@ fdescribe('SubmissionFormCollectionComponent Component', () => {
const dropDown = fixture.debugElement.query(By.css('#collectionControlsDropdownMenu')); const dropDown = fixture.debugElement.query(By.css('#collectionControlsDropdownMenu'));
expect(dropDown).toBeFalsy(); expect(dropDown).toBeFalsy();
}); });
it('should be simulated when the drop-down menu is closed', () => {
spyOn(comp, 'onClose');
comp.onClose();
expect(comp.onClose).toHaveBeenCalled();
});
it('should be simulated when the drop-down menu is toggled', () => {
spyOn(comp, 'toggled');
comp.toggled(false);
expect(comp.toggled).toHaveBeenCalled();
});
it('should ', () => {
spyOn(comp.collectionChange, 'emit').and.callThrough();
jsonPatchOpServiceStub.jsonPatchByResourceID.and.returnValue(of(submissionRestResponse));
comp.ngOnInit();
comp.onSelect(mockCollectionList[1]);
fixture.detectChanges();
expect(submissionServiceStub.changeSubmissionCollection).toHaveBeenCalled();
expect(comp.selectedCollectionId).toBe(mockCollectionList[1].collection.id);
expect(comp.selectedCollectionName$).toBeObservable(cold('(a|)', {
a: mockCollectionList[1].collection.name
}));
});
}); });
}); });