collection-dropdown.component.spec.ts (dspace-angular-dspace-7.0) | : | collection-dropdown.component.spec.ts (dspace-angular-dspace-7.1) | ||
---|---|---|---|---|
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; | import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; | |||
import { ChangeDetectorRef, ElementRef, NO_ERRORS_SCHEMA } from '@angular/core'; | import { ChangeDetectorRef, ElementRef, NO_ERRORS_SCHEMA } from '@angular/core'; | |||
import { By } from '@angular/platform-browser'; | import { By } from '@angular/platform-browser'; | |||
import { getTestScheduler } from 'jasmine-marbles'; | import { getTestScheduler } from 'jasmine-marbles'; | |||
import { TestScheduler } from 'rxjs/testing'; | import { TestScheduler } from 'rxjs/testing'; | |||
import { TranslateLoader, TranslateModule } from '@ngx-translate/core'; | import { TranslateLoader, TranslateModule } from '@ngx-translate/core'; | |||
import { of as observableOf } from 'rxjs'; | ||||
import { CollectionDropdownComponent } from './collection-dropdown.component'; | import { CollectionDropdownComponent } from './collection-dropdown.component'; | |||
import { RemoteData } from '../../core/data/remote-data'; | import { buildPaginatedList } from '../../core/data/paginated-list.model'; | |||
import { buildPaginatedList, PaginatedList } from '../../core/data/paginated-lis | import { createSuccessfulRemoteDataObject$ } from '../remote-data.utils'; | |||
t.model'; | ||||
import { createSuccessfulRemoteDataObject, createSuccessfulRemoteDataObject$ } f | ||||
rom '../remote-data.utils'; | ||||
import { PageInfo } from '../../core/shared/page-info.model'; | import { PageInfo } from '../../core/shared/page-info.model'; | |||
import { Collection } from '../../core/shared/collection.model'; | import { Collection } from '../../core/shared/collection.model'; | |||
import { CollectionDataService } from '../../core/data/collection-data.service'; | import { CollectionDataService } from '../../core/data/collection-data.service'; | |||
import { TranslateLoaderMock } from '../mocks/translate-loader.mock'; | import { TranslateLoaderMock } from '../mocks/translate-loader.mock'; | |||
import { Community } from '../../core/shared/community.model'; | import { Community } from '../../core/shared/community.model'; | |||
import { MockElementRef } from '../testing/element-ref.mock'; | import { MockElementRef } from '../testing/element-ref.mock'; | |||
import { FollowLinkConfig } from '../utils/follow-link-config.model'; | ||||
import { FindListOptions } from '../../core/data/request.models'; | ||||
import { Observable } from 'rxjs/internal/Observable'; | ||||
const community: Community = Object.assign(new Community(), { | const community: Community = Object.assign(new Community(), { | |||
id: 'ce64f48e-2c9b-411a-ac36-ee429c0e6a88', | id: 'ce64f48e-2c9b-411a-ac36-ee429c0e6a88', | |||
uuid: 'ce64f48e-2c9b-411a-ac36-ee429c0e6a88', | uuid: 'ce64f48e-2c9b-411a-ac36-ee429c0e6a88', | |||
name: 'Community 1' | name: 'Community 1' | |||
}); | }); | |||
const collections: Collection[] = [ | const collections: Collection[] = [ | |||
Object.assign(new Collection(), { | Object.assign(new Collection(), { | |||
id: 'ce64f48e-2c9b-411a-ac36-ee429c0e6a88', | id: 'ce64f48e-2c9b-411a-ac36-ee429c0e6a88', | |||
skipping to change at line 102 | skipping to change at line 97 | |||
name: 'Community 1' | name: 'Community 1' | |||
} | } | |||
], | ], | |||
collection: { | collection: { | |||
id: 'e9dbf393-7127-415f-8919-55be34a6e9ed', | id: 'e9dbf393-7127-415f-8919-55be34a6e9ed', | |||
uuid: 'e9dbf393-7127-415f-8919-55be34a6e9ed', | uuid: 'e9dbf393-7127-415f-8919-55be34a6e9ed', | |||
name: 'Collection 3' | name: 'Collection 3' | |||
} | } | |||
}; | }; | |||
// tslint:disable-next-line: max-classes-per-file | ||||
class CollectionDataServiceMock { | ||||
getAuthorizedCollection(query: string, options: FindListOptions = {}, ...links | ||||
ToFollow: FollowLinkConfig<Collection>[]): Observable<RemoteData<PaginatedList<C | ||||
ollection>>> { | ||||
return observableOf( | ||||
createSuccessfulRemoteDataObject( | ||||
buildPaginatedList(new PageInfo(), collections) | ||||
) | ||||
); | ||||
} | ||||
} | ||||
describe('CollectionDropdownComponent', () => { | describe('CollectionDropdownComponent', () => { | |||
let component: CollectionDropdownComponent; | let component: CollectionDropdownComponent; | |||
let componentAsAny: any; | let componentAsAny: any; | |||
let fixture: ComponentFixture<CollectionDropdownComponent>; | let fixture: ComponentFixture<CollectionDropdownComponent>; | |||
let scheduler: TestScheduler; | let scheduler: TestScheduler; | |||
const collectionDataServiceMock: any = jasmine.createSpyObj('CollectionDataSer vice', { | const collectionDataServiceMock: any = jasmine.createSpyObj('CollectionDataSer vice', { | |||
getAuthorizedCollection: jasmine.createSpy('getAuthorizedCollection') | getAuthorizedCollection: jasmine.createSpy('getAuthorizedCollection'), | |||
getAuthorizedCollectionByEntityType: jasmine.createSpy('getAuthorizedCollect | ||||
ionByEntityType') | ||||
}); | }); | |||
const paginatedCollection = buildPaginatedList(new PageInfo(), collections); | const paginatedCollection = buildPaginatedList(new PageInfo(), collections); | |||
const paginatedCollectionRD$ = createSuccessfulRemoteDataObject$(paginatedColl ection); | const paginatedCollectionRD$ = createSuccessfulRemoteDataObject$(paginatedColl ection); | |||
const paginatedOneElementCollection = buildPaginatedList(new PageInfo(), [coll | ||||
ections[0]]); | ||||
const paginatedOneElementCollectionRD$ = createSuccessfulRemoteDataObject$(pag | ||||
inatedOneElementCollection); | ||||
beforeEach(waitForAsync(() => { | beforeEach(waitForAsync(() => { | |||
TestBed.configureTestingModule({ | TestBed.configureTestingModule({ | |||
imports: [ | imports: [ | |||
TranslateModule.forRoot({ | TranslateModule.forRoot({ | |||
loader: { | loader: { | |||
provide: TranslateLoader, | provide: TranslateLoader, | |||
useClass: TranslateLoaderMock | useClass: TranslateLoaderMock | |||
} | } | |||
}) | }) | |||
], | ], | |||
skipping to change at line 153 | skipping to change at line 141 | |||
}) | }) | |||
.compileComponents(); | .compileComponents(); | |||
})); | })); | |||
beforeEach(() => { | beforeEach(() => { | |||
scheduler = getTestScheduler(); | scheduler = getTestScheduler(); | |||
fixture = TestBed.createComponent(CollectionDropdownComponent); | fixture = TestBed.createComponent(CollectionDropdownComponent); | |||
component = fixture.componentInstance; | component = fixture.componentInstance; | |||
componentAsAny = component; | componentAsAny = component; | |||
componentAsAny.collectionDataService.getAuthorizedCollection.and.returnValue (paginatedCollectionRD$); | componentAsAny.collectionDataService.getAuthorizedCollection.and.returnValue (paginatedCollectionRD$); | |||
componentAsAny.collectionDataService.getAuthorizedCollectionByEntityType.and .returnValue(paginatedCollectionRD$); | ||||
}); | }); | |||
it('should init component with collection list', () => { | it('should init component with collection list', () => { | |||
spyOn(component.subs, 'push'); | spyOn(component.subs, 'push'); | |||
spyOn(component, 'resetPagination'); | spyOn(component, 'resetPagination'); | |||
spyOn(component, 'populateCollectionList').and.callThrough(); | spyOn(component, 'populateCollectionList').and.callThrough(); | |||
scheduler.schedule(() => fixture.detectChanges()); | scheduler.schedule(() => fixture.detectChanges()); | |||
scheduler.flush(); | scheduler.flush(); | |||
const elements = fixture.debugElement.queryAll(By.css('.collection-item')); | const elements = fixture.debugElement.queryAll(By.css('.collection-item')); | |||
skipping to change at line 214 | skipping to change at line 203 | |||
expect(component.searchField.setValue).toHaveBeenCalledWith(''); | expect(component.searchField.setValue).toHaveBeenCalledWith(''); | |||
expect(component.resetPagination).toHaveBeenCalled(); | expect(component.resetPagination).toHaveBeenCalled(); | |||
expect(component.currentQuery).toEqual(''); | expect(component.currentQuery).toEqual(''); | |||
expect(component.populateCollectionList).toHaveBeenCalledWith(component.curr entQuery, component.currentPage); | expect(component.populateCollectionList).toHaveBeenCalledWith(component.curr entQuery, component.currentPage); | |||
expect(component.searchListCollection.length).toEqual(5); | expect(component.searchListCollection.length).toEqual(5); | |||
expect(component.subs.push).toHaveBeenCalled(); | expect(component.subs.push).toHaveBeenCalled(); | |||
}); | }); | |||
it('should change loader status', () => { | it('should change loader status', () => { | |||
spyOn(component.isLoadingList, 'next').and.callThrough(); | spyOn(component.isLoading, 'next').and.callThrough(); | |||
component.hideShowLoader(true); | component.hideShowLoader(true); | |||
expect(component.isLoadingList.next).toHaveBeenCalledWith(true); | expect(component.isLoading.next).toHaveBeenCalledWith(true); | |||
}); | }); | |||
it('reset pagination fields', () => { | it('reset pagination fields', () => { | |||
component.resetPagination(); | component.resetPagination(); | |||
expect(component.currentPage).toEqual(1); | expect(component.currentPage).toEqual(1); | |||
expect(component.currentQuery).toEqual(''); | expect(component.currentQuery).toEqual(''); | |||
expect(component.hasNextPage).toEqual(true); | expect(component.hasNextPage).toEqual(true); | |||
expect(component.searchListCollection).toEqual([]); | expect(component.searchListCollection).toEqual([]); | |||
}); | }); | |||
it('should invoke the method getAuthorizedCollectionByEntityType of Collection | ||||
DataService when entityType is set',() => { | ||||
component.entityType = 'rel'; | ||||
scheduler.schedule(() => fixture.detectChanges()); | ||||
scheduler.flush(); | ||||
expect((component as any).collectionDataService.getAuthorizedCollectionByEnt | ||||
ityType).toHaveBeenCalled(); | ||||
}); | ||||
it('should emit hasChoice true when totalElements is greater then one', () => | ||||
{ | ||||
spyOn(component.searchComplete, 'emit').and.callThrough(); | ||||
component.ngOnInit(); | ||||
fixture.detectChanges(); | ||||
expect(component.searchComplete.emit).toHaveBeenCalledWith(); | ||||
}); | ||||
it('should emit theOnlySelectable when totalElements is equal to one', () => { | ||||
componentAsAny.collectionDataService.getAuthorizedCollection.and.returnValue | ||||
(paginatedOneElementCollectionRD$); | ||||
componentAsAny.collectionDataService.getAuthorizedCollectionByEntityType.and | ||||
.returnValue(paginatedOneElementCollectionRD$); | ||||
spyOn(component.theOnlySelectable, 'emit').and.callThrough(); | ||||
component.ngOnInit(); | ||||
fixture.detectChanges(); | ||||
const expectedTheOnlySelectable = { | ||||
communities: [ { id: 'ce64f48e-2c9b-411a-ac36-ee429c0e6a88', name: 'Commun | ||||
ity 1', uuid: 'ce64f48e-2c9b-411a-ac36-ee429c0e6a88' } ], | ||||
collection: { id: 'ce64f48e-2c9b-411a-ac36-ee429c0e6a88', uuid: 'ce64f48e- | ||||
2c9b-411a-ac36-ee429c0e6a88', name: 'Collection 1' } | ||||
}; | ||||
expect(component.theOnlySelectable.emit).toHaveBeenCalledWith(expectedTheOnl | ||||
ySelectable); | ||||
}); | ||||
}); | }); | |||
End of changes. 10 change blocks. | ||||
25 lines changed or deleted | 53 lines changed or added |