|
| 1 | +import { of as observableOf } from 'rxjs'; |
| 2 | +import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; |
| 3 | +import { RouterTestingModule } from '@angular/router/testing'; |
| 4 | +import { TranslateLoader, TranslateModule } from '@ngx-translate/core'; |
| 5 | +import { DebugElement, NgModule, NO_ERRORS_SCHEMA } from '@angular/core'; |
| 6 | +import { NgbActiveModal, NgbModal, NgbModalModule } from '@ng-bootstrap/ng-bootstrap'; |
| 7 | +import { ActivatedRoute, Router } from '@angular/router'; |
| 8 | +import { BATCH_EXPORT_SCRIPT_NAME, ScriptDataService } from '../../../../core/data/processes/script-data.service'; |
| 9 | +import { Collection } from '../../../../core/shared/collection.model'; |
| 10 | +import { Item } from '../../../../core/shared/item.model'; |
| 11 | +import { ProcessParameter } from '../../../../process-page/processes/process-parameter.model'; |
| 12 | +import { ConfirmationModalComponent } from '../../../confirmation-modal/confirmation-modal.component'; |
| 13 | +import { TranslateLoaderMock } from '../../../mocks/translate-loader.mock'; |
| 14 | +import { NotificationsService } from '../../../notifications/notifications.service'; |
| 15 | +import { NotificationsServiceStub } from '../../../testing/notifications-service.stub'; |
| 16 | +import { |
| 17 | + createFailedRemoteDataObject$, |
| 18 | + createSuccessfulRemoteDataObject, |
| 19 | + createSuccessfulRemoteDataObject$ |
| 20 | +} from '../../../remote-data.utils'; |
| 21 | +import { ExportBatchSelectorComponent } from './export-batch-selector.component'; |
| 22 | +import { AuthorizationDataService } from '../../../../core/data/feature-authorization/authorization-data.service'; |
| 23 | + |
| 24 | +// No way to add entryComponents yet to testbed; alternative implemented; source: https://stackoverflow.com/questions/41689468/how-to-shallow-test-a-component-with-an-entrycomponents |
| 25 | +@NgModule({ |
| 26 | + imports: [NgbModalModule, |
| 27 | + TranslateModule.forRoot({ |
| 28 | + loader: { |
| 29 | + provide: TranslateLoader, |
| 30 | + useClass: TranslateLoaderMock |
| 31 | + } |
| 32 | + }), |
| 33 | + ], |
| 34 | + exports: [], |
| 35 | + declarations: [ConfirmationModalComponent], |
| 36 | + providers: [] |
| 37 | +}) |
| 38 | +class ModelTestModule { |
| 39 | +} |
| 40 | + |
| 41 | +describe('ExportBatchSelectorComponent', () => { |
| 42 | + let component: ExportBatchSelectorComponent; |
| 43 | + let fixture: ComponentFixture<ExportBatchSelectorComponent>; |
| 44 | + let debugElement: DebugElement; |
| 45 | + let modalRef; |
| 46 | + |
| 47 | + let router; |
| 48 | + let notificationService: NotificationsServiceStub; |
| 49 | + let scriptService; |
| 50 | + let authorizationDataService; |
| 51 | + |
| 52 | + const mockItem = Object.assign(new Item(), { |
| 53 | + id: 'fake-id', |
| 54 | + uuid: 'fake-id', |
| 55 | + handle: 'fake/handle', |
| 56 | + lastModified: '2018' |
| 57 | + }); |
| 58 | + |
| 59 | + const mockCollection: Collection = Object.assign(new Collection(), { |
| 60 | + id: 'test-collection-1-1', |
| 61 | + uuid: 'test-collection-1-1', |
| 62 | + name: 'test-collection-1', |
| 63 | + metadata: { |
| 64 | + 'dc.identifier.uri': [ |
| 65 | + { |
| 66 | + language: null, |
| 67 | + value: 'fake/test-collection-1' |
| 68 | + } |
| 69 | + ] |
| 70 | + } |
| 71 | + }); |
| 72 | + const itemRD = createSuccessfulRemoteDataObject(mockItem); |
| 73 | + const modalStub = jasmine.createSpyObj('modalStub', ['close']); |
| 74 | + |
| 75 | + beforeEach(waitForAsync(() => { |
| 76 | + notificationService = new NotificationsServiceStub(); |
| 77 | + router = jasmine.createSpyObj('router', { |
| 78 | + navigateByUrl: jasmine.createSpy('navigateByUrl') |
| 79 | + }); |
| 80 | + scriptService = jasmine.createSpyObj('scriptService', |
| 81 | + { |
| 82 | + invoke: createSuccessfulRemoteDataObject$({ processId: '45' }) |
| 83 | + } |
| 84 | + ); |
| 85 | + authorizationDataService = jasmine.createSpyObj('authorizationDataService', { |
| 86 | + isAuthorized: observableOf(true) |
| 87 | + }); |
| 88 | + TestBed.configureTestingModule({ |
| 89 | + imports: [TranslateModule.forRoot(), RouterTestingModule.withRoutes([]), ModelTestModule], |
| 90 | + declarations: [ExportBatchSelectorComponent], |
| 91 | + providers: [ |
| 92 | + { provide: NgbActiveModal, useValue: modalStub }, |
| 93 | + { provide: NotificationsService, useValue: notificationService }, |
| 94 | + { provide: ScriptDataService, useValue: scriptService }, |
| 95 | + { provide: AuthorizationDataService, useValue: authorizationDataService }, |
| 96 | + { |
| 97 | + provide: ActivatedRoute, |
| 98 | + useValue: { |
| 99 | + root: { |
| 100 | + snapshot: { |
| 101 | + data: { |
| 102 | + dso: itemRD, |
| 103 | + }, |
| 104 | + }, |
| 105 | + } |
| 106 | + }, |
| 107 | + }, |
| 108 | + { |
| 109 | + provide: Router, useValue: router |
| 110 | + } |
| 111 | + ], |
| 112 | + schemas: [NO_ERRORS_SCHEMA] |
| 113 | + }).compileComponents(); |
| 114 | + |
| 115 | + })); |
| 116 | + |
| 117 | + beforeEach(() => { |
| 118 | + fixture = TestBed.createComponent(ExportBatchSelectorComponent); |
| 119 | + component = fixture.componentInstance; |
| 120 | + debugElement = fixture.debugElement; |
| 121 | + const modalService = TestBed.inject(NgbModal); |
| 122 | + modalRef = modalService.open(ConfirmationModalComponent); |
| 123 | + modalRef.componentInstance.response = observableOf(true); |
| 124 | + fixture.detectChanges(); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should create', () => { |
| 128 | + expect(component).toBeTruthy(); |
| 129 | + }); |
| 130 | + |
| 131 | + describe('if item is selected', () => { |
| 132 | + let scriptRequestSucceeded; |
| 133 | + beforeEach((done) => { |
| 134 | + component.navigate(mockItem).subscribe((succeeded: boolean) => { |
| 135 | + scriptRequestSucceeded = succeeded; |
| 136 | + done(); |
| 137 | + }); |
| 138 | + }); |
| 139 | + it('should not invoke batch-export script', () => { |
| 140 | + expect(scriptService.invoke).not.toHaveBeenCalled(); |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + describe('if collection is selected and is admin', () => { |
| 145 | + let scriptRequestSucceeded; |
| 146 | + beforeEach((done) => { |
| 147 | + spyOn((component as any).modalService, 'open').and.returnValue(modalRef); |
| 148 | + component.navigate(mockCollection).subscribe((succeeded: boolean) => { |
| 149 | + scriptRequestSucceeded = succeeded; |
| 150 | + done(); |
| 151 | + }); |
| 152 | + }); |
| 153 | + it('should invoke the batch-export script with option --id uuid and -a option', () => { |
| 154 | + const parameterValues: ProcessParameter[] = [ |
| 155 | + Object.assign(new ProcessParameter(), { name: '--id', value: mockCollection.uuid }), |
| 156 | + Object.assign(new ProcessParameter(), { name: '--type', value: 'COLLECTION' }), |
| 157 | + Object.assign(new ProcessParameter(), { name: '-a' }), |
| 158 | + ]; |
| 159 | + expect(scriptService.invoke).toHaveBeenCalledWith(BATCH_EXPORT_SCRIPT_NAME, parameterValues, []); |
| 160 | + }); |
| 161 | + it('success notification is shown', () => { |
| 162 | + expect(scriptRequestSucceeded).toBeTrue(); |
| 163 | + expect(notificationService.success).toHaveBeenCalled(); |
| 164 | + }); |
| 165 | + it('redirected to process page', () => { |
| 166 | + expect(router.navigateByUrl).toHaveBeenCalledWith('/processes/45'); |
| 167 | + }); |
| 168 | + }); |
| 169 | + describe('if collection is selected and is not admin', () => { |
| 170 | + let scriptRequestSucceeded; |
| 171 | + beforeEach((done) => { |
| 172 | + (authorizationDataService.isAuthorized as jasmine.Spy).and.returnValue(observableOf(false)); |
| 173 | + spyOn((component as any).modalService, 'open').and.returnValue(modalRef); |
| 174 | + component.navigate(mockCollection).subscribe((succeeded: boolean) => { |
| 175 | + scriptRequestSucceeded = succeeded; |
| 176 | + done(); |
| 177 | + }); |
| 178 | + }); |
| 179 | + it('should invoke the Batch-export script with option --id uuid without the -a option', () => { |
| 180 | + const parameterValues: ProcessParameter[] = [ |
| 181 | + Object.assign(new ProcessParameter(), { name: '--id', value: mockCollection.uuid }), |
| 182 | + Object.assign(new ProcessParameter(), { name: '--type', value: 'COLLECTION' }) |
| 183 | + ]; |
| 184 | + expect(scriptService.invoke).toHaveBeenCalledWith(BATCH_EXPORT_SCRIPT_NAME, parameterValues, []); |
| 185 | + }); |
| 186 | + it('success notification is shown', () => { |
| 187 | + expect(scriptRequestSucceeded).toBeTrue(); |
| 188 | + expect(notificationService.success).toHaveBeenCalled(); |
| 189 | + }); |
| 190 | + it('redirected to process page', () => { |
| 191 | + expect(router.navigateByUrl).toHaveBeenCalledWith('/processes/45'); |
| 192 | + }); |
| 193 | + }); |
| 194 | + |
| 195 | + describe('if collection is selected; but script invoke fails', () => { |
| 196 | + let scriptRequestSucceeded; |
| 197 | + beforeEach((done) => { |
| 198 | + spyOn((component as any).modalService, 'open').and.returnValue(modalRef); |
| 199 | + jasmine.getEnv().allowRespy(true); |
| 200 | + spyOn(scriptService, 'invoke').and.returnValue(createFailedRemoteDataObject$('Error', 500)); |
| 201 | + component.navigate(mockCollection).subscribe((succeeded: boolean) => { |
| 202 | + scriptRequestSucceeded = succeeded; |
| 203 | + done(); |
| 204 | + }); |
| 205 | + }); |
| 206 | + it('error notification is shown', () => { |
| 207 | + expect(scriptRequestSucceeded).toBeFalse(); |
| 208 | + expect(notificationService.error).toHaveBeenCalled(); |
| 209 | + }); |
| 210 | + }); |
| 211 | +}); |
0 commit comments