|
| 1 | +import { ComponentFixture, fakeAsync, TestBed, waitForAsync } from '@angular/core/testing'; |
| 2 | +import { BatchImportPageComponent } from './batch-import-page.component'; |
| 3 | +import { NotificationsServiceStub } from '../../shared/testing/notifications-service.stub'; |
| 4 | +import { createFailedRemoteDataObject$, createSuccessfulRemoteDataObject$ } from '../../shared/remote-data.utils'; |
| 5 | +import { FormsModule } from '@angular/forms'; |
| 6 | +import { TranslateModule } from '@ngx-translate/core'; |
| 7 | +import { RouterTestingModule } from '@angular/router/testing'; |
| 8 | +import { FileValueAccessorDirective } from '../../shared/utils/file-value-accessor.directive'; |
| 9 | +import { FileValidator } from '../../shared/utils/require-file.validator'; |
| 10 | +import { NotificationsService } from '../../shared/notifications/notifications.service'; |
| 11 | +import { |
| 12 | + BATCH_IMPORT_SCRIPT_NAME, |
| 13 | + ScriptDataService |
| 14 | +} from '../../core/data/processes/script-data.service'; |
| 15 | +import { Router } from '@angular/router'; |
| 16 | +import { Location } from '@angular/common'; |
| 17 | +import { NO_ERRORS_SCHEMA } from '@angular/core'; |
| 18 | +import { By } from '@angular/platform-browser'; |
| 19 | +import { ProcessParameter } from '../../process-page/processes/process-parameter.model'; |
| 20 | + |
| 21 | +describe('BatchImportPageComponent', () => { |
| 22 | + let component: BatchImportPageComponent; |
| 23 | + let fixture: ComponentFixture<BatchImportPageComponent>; |
| 24 | + |
| 25 | + let notificationService: NotificationsServiceStub; |
| 26 | + let scriptService: any; |
| 27 | + let router; |
| 28 | + let locationStub; |
| 29 | + |
| 30 | + function init() { |
| 31 | + notificationService = new NotificationsServiceStub(); |
| 32 | + scriptService = jasmine.createSpyObj('scriptService', |
| 33 | + { |
| 34 | + invoke: createSuccessfulRemoteDataObject$({ processId: '46' }) |
| 35 | + } |
| 36 | + ); |
| 37 | + router = jasmine.createSpyObj('router', { |
| 38 | + navigateByUrl: jasmine.createSpy('navigateByUrl') |
| 39 | + }); |
| 40 | + locationStub = jasmine.createSpyObj('location', { |
| 41 | + back: jasmine.createSpy('back') |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + beforeEach(waitForAsync(() => { |
| 46 | + init(); |
| 47 | + TestBed.configureTestingModule({ |
| 48 | + imports: [ |
| 49 | + FormsModule, |
| 50 | + TranslateModule.forRoot(), |
| 51 | + RouterTestingModule.withRoutes([]) |
| 52 | + ], |
| 53 | + declarations: [BatchImportPageComponent, FileValueAccessorDirective, FileValidator], |
| 54 | + providers: [ |
| 55 | + { provide: NotificationsService, useValue: notificationService }, |
| 56 | + { provide: ScriptDataService, useValue: scriptService }, |
| 57 | + { provide: Router, useValue: router }, |
| 58 | + { provide: Location, useValue: locationStub }, |
| 59 | + ], |
| 60 | + schemas: [NO_ERRORS_SCHEMA] |
| 61 | + }).compileComponents(); |
| 62 | + })); |
| 63 | + |
| 64 | + beforeEach(() => { |
| 65 | + fixture = TestBed.createComponent(BatchImportPageComponent); |
| 66 | + component = fixture.componentInstance; |
| 67 | + fixture.detectChanges(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should create', () => { |
| 71 | + expect(component).toBeTruthy(); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('if back button is pressed', () => { |
| 75 | + beforeEach(fakeAsync(() => { |
| 76 | + const proceed = fixture.debugElement.query(By.css('#backButton')).nativeElement; |
| 77 | + proceed.click(); |
| 78 | + fixture.detectChanges(); |
| 79 | + })); |
| 80 | + it('should do location.back', () => { |
| 81 | + expect(locationStub.back).toHaveBeenCalled(); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('if file is set', () => { |
| 86 | + let fileMock: File; |
| 87 | + |
| 88 | + beforeEach(() => { |
| 89 | + fileMock = new File([''], 'filename.zip', { type: 'application/zip' }); |
| 90 | + component.setFile(fileMock); |
| 91 | + }); |
| 92 | + |
| 93 | + describe('if proceed button is pressed without validate only', () => { |
| 94 | + beforeEach(fakeAsync(() => { |
| 95 | + component.validateOnly = false; |
| 96 | + const proceed = fixture.debugElement.query(By.css('#proceedButton')).nativeElement; |
| 97 | + proceed.click(); |
| 98 | + fixture.detectChanges(); |
| 99 | + })); |
| 100 | + it('metadata-import script is invoked with --zip fileName and the mockFile', () => { |
| 101 | + const parameterValues: ProcessParameter[] = [ |
| 102 | + Object.assign(new ProcessParameter(), { name: '--zip', value: 'filename.zip' }), |
| 103 | + ]; |
| 104 | + parameterValues.push(Object.assign(new ProcessParameter(), { name: '--add' })); |
| 105 | + expect(scriptService.invoke).toHaveBeenCalledWith(BATCH_IMPORT_SCRIPT_NAME, parameterValues, [fileMock]); |
| 106 | + }); |
| 107 | + it('success notification is shown', () => { |
| 108 | + expect(notificationService.success).toHaveBeenCalled(); |
| 109 | + }); |
| 110 | + it('redirected to process page', () => { |
| 111 | + expect(router.navigateByUrl).toHaveBeenCalledWith('/processes/46'); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + describe('if proceed button is pressed with validate only', () => { |
| 116 | + beforeEach(fakeAsync(() => { |
| 117 | + component.validateOnly = true; |
| 118 | + const proceed = fixture.debugElement.query(By.css('#proceedButton')).nativeElement; |
| 119 | + proceed.click(); |
| 120 | + fixture.detectChanges(); |
| 121 | + })); |
| 122 | + it('metadata-import script is invoked with --zip fileName and the mockFile and -v validate-only', () => { |
| 123 | + const parameterValues: ProcessParameter[] = [ |
| 124 | + Object.assign(new ProcessParameter(), { name: '--zip', value: 'filename.zip' }), |
| 125 | + Object.assign(new ProcessParameter(), { name: '--add' }), |
| 126 | + Object.assign(new ProcessParameter(), { name: '-v', value: true }), |
| 127 | + ]; |
| 128 | + expect(scriptService.invoke).toHaveBeenCalledWith(BATCH_IMPORT_SCRIPT_NAME, parameterValues, [fileMock]); |
| 129 | + }); |
| 130 | + it('success notification is shown', () => { |
| 131 | + expect(notificationService.success).toHaveBeenCalled(); |
| 132 | + }); |
| 133 | + it('redirected to process page', () => { |
| 134 | + expect(router.navigateByUrl).toHaveBeenCalledWith('/processes/46'); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe('if proceed is pressed; but script invoke fails', () => { |
| 139 | + beforeEach(fakeAsync(() => { |
| 140 | + jasmine.getEnv().allowRespy(true); |
| 141 | + spyOn(scriptService, 'invoke').and.returnValue(createFailedRemoteDataObject$('Error', 500)); |
| 142 | + const proceed = fixture.debugElement.query(By.css('#proceedButton')).nativeElement; |
| 143 | + proceed.click(); |
| 144 | + fixture.detectChanges(); |
| 145 | + })); |
| 146 | + it('error notification is shown', () => { |
| 147 | + expect(notificationService.error).toHaveBeenCalled(); |
| 148 | + }); |
| 149 | + }); |
| 150 | + }); |
| 151 | +}); |
0 commit comments