|
| 1 | +import { CommonModule } from '@angular/common'; |
| 2 | +import { NO_ERRORS_SCHEMA } from '@angular/core'; |
| 3 | +import { |
| 4 | + ComponentFixture, |
| 5 | + fakeAsync, |
| 6 | + inject, |
| 7 | + TestBed, |
| 8 | + tick, |
| 9 | +} from '@angular/core/testing'; |
| 10 | +import { |
| 11 | + FormsModule, |
| 12 | + ReactiveFormsModule, |
| 13 | +} from '@angular/forms'; |
| 14 | +import { BrowserModule } from '@angular/platform-browser'; |
| 15 | +import { |
| 16 | + ActivatedRoute, |
| 17 | + Router, |
| 18 | +} from '@angular/router'; |
| 19 | +import { RouterTestingModule } from '@angular/router/testing'; |
| 20 | +import { NotificationsService } from '@dspace/core/notification-system/notifications.service'; |
| 21 | +import { ActivatedRouteStub } from '@dspace/core/testing/active-router.stub'; |
| 22 | +import { NotificationsServiceStub } from '@dspace/core/testing/notifications-service.stub'; |
| 23 | +import { getMockRequestService } from '@dspace/core/testing/request.service.mock'; |
| 24 | +import { RouterMock } from '@dspace/core/testing/router.mock'; |
| 25 | +import { TranslateLoaderMock } from '@dspace/core/testing/translate-loader.mock'; |
| 26 | +import { createSuccessfulRemoteDataObject } from '@dspace/core/utilities/remote-data.utils'; |
| 27 | +import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; |
| 28 | +import { |
| 29 | + TranslateLoader, |
| 30 | + TranslateModule, |
| 31 | +} from '@ngx-translate/core'; |
| 32 | +import { of } from 'rxjs'; |
| 33 | + |
| 34 | +import { AuthService } from '../core/auth/auth.service'; |
| 35 | +import { DSONameService } from '../core/breadcrumbs/dso-name.service'; |
| 36 | +import { RestResponse } from '../core/cache/response.models'; |
| 37 | +import { ScriptDataService } from '../core/data/processes/script-data.service'; |
| 38 | +import { RequestService } from '../core/data/request.service'; |
| 39 | +import { RequestEntry } from '../core/data/request-entry.model'; |
| 40 | +import { Collection } from '../core/shared/collection.model'; |
| 41 | +import { BulkImportPageComponent } from './bulk-import-page.component'; |
| 42 | + |
| 43 | +describe('BulkImportPageComponent', () => { |
| 44 | + |
| 45 | + let component: BulkImportPageComponent; |
| 46 | + let fixture: ComponentFixture<BulkImportPageComponent>; |
| 47 | + |
| 48 | + let requestService: any; |
| 49 | + let scriptDataService: any; |
| 50 | + let notificationsService: any; |
| 51 | + let route: any; |
| 52 | + let router: RouterMock; |
| 53 | + |
| 54 | + const collection: Collection = Object.assign(new Collection(), { |
| 55 | + id: '626b80c5-ef15-4b29-8e69-bda89b0a7acf', |
| 56 | + name: 'Test collection', |
| 57 | + }); |
| 58 | + |
| 59 | + const file: File = new File(['test'], 'test.xls'); |
| 60 | + |
| 61 | + const fileList: any = { |
| 62 | + item: (index: number) => file, |
| 63 | + length: 10, |
| 64 | + }; |
| 65 | + |
| 66 | + const authService = jasmine.createSpyObj('authService', { |
| 67 | + isAuthenticated: of(true), |
| 68 | + setRedirectUrl: {}, |
| 69 | + }); |
| 70 | + |
| 71 | + beforeEach(() => { |
| 72 | + requestService = getMockRequestService(); |
| 73 | + scriptDataService = jasmine.createSpyObj('scriptDataService', { |
| 74 | + invoke: of(Object.assign(new RequestEntry(), { |
| 75 | + response: new RestResponse(true, 200, 'OK'), |
| 76 | + })), |
| 77 | + }); |
| 78 | + notificationsService = new NotificationsServiceStub(); |
| 79 | + |
| 80 | + route = new ActivatedRouteStub({}, { |
| 81 | + collection: createSuccessfulRemoteDataObject( collection ), |
| 82 | + }); |
| 83 | + router = new RouterMock(); |
| 84 | + |
| 85 | + TestBed.configureTestingModule({ |
| 86 | + imports: [CommonModule, NgbModule, FormsModule, ReactiveFormsModule, BrowserModule, RouterTestingModule, |
| 87 | + TranslateModule.forRoot({ |
| 88 | + loader: { |
| 89 | + provide: TranslateLoader, |
| 90 | + useClass: TranslateLoaderMock, |
| 91 | + }, |
| 92 | + }), BulkImportPageComponent], |
| 93 | + providers: [ |
| 94 | + BulkImportPageComponent, |
| 95 | + DSONameService, |
| 96 | + { provide: RequestService, useValue: requestService }, |
| 97 | + { provide: RequestService, useValue: requestService }, |
| 98 | + { provide: ScriptDataService, useValue: scriptDataService }, |
| 99 | + { provide: NotificationsService, useValue: notificationsService }, |
| 100 | + { provide: ActivatedRoute, useValue: route }, |
| 101 | + { provide: Router, useValue: router }, |
| 102 | + { provide: AuthService, useValue: authService }, |
| 103 | + ], |
| 104 | + schemas: [NO_ERRORS_SCHEMA], |
| 105 | + }).compileComponents(); |
| 106 | + |
| 107 | + }); |
| 108 | + |
| 109 | + beforeEach(fakeAsync(() => { |
| 110 | + fixture = TestBed.createComponent(BulkImportPageComponent); |
| 111 | + component = fixture.componentInstance; |
| 112 | + fixture.detectChanges(); |
| 113 | + tick(); |
| 114 | + })); |
| 115 | + |
| 116 | + it('should create BulkImportPageComponent', inject([BulkImportPageComponent], (comp: BulkImportPageComponent) => { |
| 117 | + expect(comp).toBeDefined(); |
| 118 | + })); |
| 119 | + |
| 120 | + describe('when the user submit the form', () => { |
| 121 | + |
| 122 | + beforeEach(() => { |
| 123 | + component.form.value.abortOnError = true; |
| 124 | + component.form.value.file = fileList; |
| 125 | + component.setFile(fileList); |
| 126 | + component.submit(); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should invoke the bulk-import script', () => { |
| 130 | + expect(scriptDataService.invoke).toHaveBeenCalledWith('bulk-import', [ |
| 131 | + { name: '-c', value: '626b80c5-ef15-4b29-8e69-bda89b0a7acf' }, |
| 132 | + { name: '-f', value: 'test.xls' }, |
| 133 | + { name: '-er', value: true }, |
| 134 | + ], [file]); |
| 135 | + }); |
| 136 | + |
| 137 | + }); |
| 138 | + |
| 139 | + describe('when the user click on back button', () => { |
| 140 | + |
| 141 | + beforeEach(() => { |
| 142 | + component.goBack(); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should nagivate to collection page', () => { |
| 146 | + expect(router.navigateByUrl).toHaveBeenCalledWith('/collections/626b80c5-ef15-4b29-8e69-bda89b0a7acf'); |
| 147 | + }); |
| 148 | + |
| 149 | + }); |
| 150 | + |
| 151 | +}); |
0 commit comments