|
| 1 | +import { async, ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing'; |
| 2 | + |
| 3 | +import { CollectionSelectorComponent } from './collection-selector.component'; |
| 4 | +import { CollectionDropdownComponent } from 'src/app/shared/collection-dropdown/collection-dropdown.component'; |
| 5 | +import { Collection } from 'src/app/core/shared/collection.model'; |
| 6 | +import { of, Observable } from 'rxjs'; |
| 7 | +import { RemoteData } from 'src/app/core/data/remote-data'; |
| 8 | +import { Community } from 'src/app/core/shared/community.model'; |
| 9 | +import { FindListOptions } from 'src/app/core/data/request.models'; |
| 10 | +import { FollowLinkConfig } from 'src/app/shared/utils/follow-link-config.model'; |
| 11 | +import { PaginatedList } from 'src/app/core/data/paginated-list'; |
| 12 | +import { createSuccessfulRemoteDataObject } from 'src/app/shared/remote-data.utils'; |
| 13 | +import { PageInfo } from 'src/app/core/shared/page-info.model'; |
| 14 | +import { TranslateModule, TranslateLoader } from '@ngx-translate/core'; |
| 15 | +import { TranslateLoaderMock } from 'src/app/shared/mocks/translate-loader.mock'; |
| 16 | +import { CollectionDataService } from 'src/app/core/data/collection-data.service'; |
| 17 | +import { ChangeDetectorRef, ElementRef, NO_ERRORS_SCHEMA } from '@angular/core'; |
| 18 | +import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; |
| 19 | +import { ActivatedRoute } from '@angular/router'; |
| 20 | +import { hot } from 'jasmine-marbles'; |
| 21 | +import { By } from '@angular/platform-browser'; |
| 22 | + |
| 23 | +describe('CollectionSelectorComponent', () => { |
| 24 | + let component: CollectionSelectorComponent; |
| 25 | + let fixture: ComponentFixture<CollectionSelectorComponent>; |
| 26 | + const modal = jasmine.createSpyObj('modal', ['close', 'dismiss']); |
| 27 | + |
| 28 | + const community: Community = Object.assign(new Community(), { |
| 29 | + id: 'ce64f48e-2c9b-411a-ac36-ee429c0e6a88', |
| 30 | + uuid: 'ce64f48e-2c9b-411a-ac36-ee429c0e6a88', |
| 31 | + name: 'Community 1' |
| 32 | + }); |
| 33 | + |
| 34 | + const collections: Collection[] = [ |
| 35 | + Object.assign(new Collection(), { |
| 36 | + id: 'ce64f48e-2c9b-411a-ac36-ee429c0e6a88', |
| 37 | + name: 'Collection 1', |
| 38 | + metadata: [ |
| 39 | + { |
| 40 | + key: 'dc.title', |
| 41 | + language: 'en_US', |
| 42 | + value: 'Community 1-Collection 1' |
| 43 | + }], |
| 44 | + parentCommunity: of( |
| 45 | + new RemoteData(false, false, true, undefined, community, 200) |
| 46 | + ) |
| 47 | + }), |
| 48 | + Object.assign(new Collection(), { |
| 49 | + id: '59ee713b-ee53-4220-8c3f-9860dc84fe33', |
| 50 | + name: 'Collection 2', |
| 51 | + metadata: [ |
| 52 | + { |
| 53 | + key: 'dc.title', |
| 54 | + language: 'en_US', |
| 55 | + value: 'Community 1-Collection 2' |
| 56 | + }], |
| 57 | + parentCommunity: of( |
| 58 | + new RemoteData(false, false, true, undefined, community, 200) |
| 59 | + ) |
| 60 | + }), |
| 61 | + Object.assign(new Collection(), { |
| 62 | + id: 'e9dbf393-7127-415f-8919-55be34a6e9ed', |
| 63 | + name: 'Collection 3', |
| 64 | + metadata: [ |
| 65 | + { |
| 66 | + key: 'dc.title', |
| 67 | + language: 'en_US', |
| 68 | + value: 'Community 1-Collection 3' |
| 69 | + }], |
| 70 | + parentCommunity: of( |
| 71 | + new RemoteData(false, false, true, undefined, community, 200) |
| 72 | + ) |
| 73 | + }), |
| 74 | + Object.assign(new Collection(), { |
| 75 | + id: '59da2ff0-9bf4-45bf-88be-e35abd33f304', |
| 76 | + name: 'Collection 4', |
| 77 | + metadata: [ |
| 78 | + { |
| 79 | + key: 'dc.title', |
| 80 | + language: 'en_US', |
| 81 | + value: 'Community 1-Collection 4' |
| 82 | + }], |
| 83 | + parentCommunity: of( |
| 84 | + new RemoteData(false, false, true, undefined, community, 200) |
| 85 | + ) |
| 86 | + }), |
| 87 | + Object.assign(new Collection(), { |
| 88 | + id: 'a5159760-f362-4659-9e81-e3253ad91ede', |
| 89 | + name: 'Collection 5', |
| 90 | + metadata: [ |
| 91 | + { |
| 92 | + key: 'dc.title', |
| 93 | + language: 'en_US', |
| 94 | + value: 'Community 1-Collection 5' |
| 95 | + }], |
| 96 | + parentCommunity: of( |
| 97 | + new RemoteData(false, false, true, undefined, community, 200) |
| 98 | + ) |
| 99 | + }) |
| 100 | + ]; |
| 101 | + |
| 102 | + // tslint:disable-next-line: max-classes-per-file |
| 103 | + const collectionDataServiceMock = { |
| 104 | + getAuthorizedCollection(query: string, options: FindListOptions = {}, ...linksToFollow: Array<FollowLinkConfig<Collection>>): Observable<RemoteData<PaginatedList<Collection>>> { |
| 105 | + return hot( 'a|', { |
| 106 | + a: createSuccessfulRemoteDataObject( |
| 107 | + new PaginatedList(new PageInfo(), collections) |
| 108 | + ) |
| 109 | + }); |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | + beforeEach(async(() => { |
| 114 | + TestBed.configureTestingModule({ |
| 115 | + imports: [ |
| 116 | + TranslateModule.forRoot({ |
| 117 | + loader: { |
| 118 | + provide: TranslateLoader, |
| 119 | + useClass: TranslateLoaderMock |
| 120 | + } |
| 121 | + }) |
| 122 | + ], |
| 123 | + declarations: [ CollectionSelectorComponent, CollectionDropdownComponent ], |
| 124 | + providers: [ |
| 125 | + {provide: CollectionDataService, useValue: collectionDataServiceMock}, |
| 126 | + {provide: ChangeDetectorRef, useValue: {}}, |
| 127 | + {provide: ElementRef, userValue: {}}, |
| 128 | + {provide: NgbActiveModal, useValue: modal}, |
| 129 | + {provide: ActivatedRoute, useValue: {}} |
| 130 | + ], |
| 131 | + schemas: [NO_ERRORS_SCHEMA] |
| 132 | + }) |
| 133 | + .compileComponents(); |
| 134 | + })); |
| 135 | + |
| 136 | + beforeEach(() => { |
| 137 | + fixture = TestBed.createComponent(CollectionSelectorComponent); |
| 138 | + component = fixture.componentInstance; |
| 139 | + fixture.detectChanges(); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should create', () => { |
| 143 | + expect(component).toBeTruthy(); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should call selectObject', fakeAsync(() => { |
| 147 | + spyOn(component, 'selectObject'); |
| 148 | + fixture.detectChanges(); |
| 149 | + tick(); |
| 150 | + fixture.whenStable().then(() => { |
| 151 | + const collectionItem = fixture.debugElement.query(By.css('.collection-item:nth-child(2)')); |
| 152 | + collectionItem.triggerEventHandler('click', { |
| 153 | + preventDefault: () => {/**/ |
| 154 | + } |
| 155 | + }); |
| 156 | + expect(component.selectObject).toHaveBeenCalled(); |
| 157 | + }); |
| 158 | + })); |
| 159 | + |
| 160 | + it('should close the dialog', () => { |
| 161 | + component.close(); |
| 162 | + expect((component as any).activeModal.close).toHaveBeenCalled(); |
| 163 | + }); |
| 164 | +}); |
0 commit comments