|
| 1 | +import { NO_ERRORS_SCHEMA } from '@angular/core'; |
| 2 | +import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; |
| 3 | +import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; |
| 4 | +import { RouterTestingModule } from '@angular/router/testing'; |
| 5 | + |
| 6 | +import { of } from 'rxjs'; |
| 7 | +import { TranslateModule } from '@ngx-translate/core'; |
| 8 | +import { MyDSpaceBulkActionComponent } from './my-dspace-bulk-action.component'; |
| 9 | +import { SearchService } from '../../../core/shared/search/search.service'; |
| 10 | +import { SelectableListService } from '../../../shared/object-list/selectable-list/selectable-list.service'; |
| 11 | +import { PoolTaskDataService } from '../../../core/tasks/pool-task-data.service'; |
| 12 | +import { ProcessTaskResponse } from '../../../core/tasks/models/process-task-response'; |
| 13 | +import { ClaimedTaskDataService } from '../../../core/tasks/claimed-task-data.service'; |
| 14 | +import { NotificationsService } from '../../../shared/notifications/notifications.service'; |
| 15 | +import { NotificationsServiceStub } from '../../../shared/testing/notifications-service.stub'; |
| 16 | +import { Router } from '@angular/router'; |
| 17 | +import { RouterStub } from '../../../shared/testing/router.stub'; |
| 18 | +import { SearchServiceStub } from '../../../shared/testing/search-service.stub'; |
| 19 | +import { UntypedFormBuilder } from '@angular/forms'; |
| 20 | +import { RequestService } from '../../../core/data/request.service'; |
| 21 | +import { By } from '@angular/platform-browser'; |
| 22 | + |
| 23 | +describe('MyDSpaceBulkActionComponent test suite', () => { |
| 24 | + let comp: MyDSpaceBulkActionComponent; |
| 25 | + let fixture: ComponentFixture<MyDSpaceBulkActionComponent>; |
| 26 | + |
| 27 | + const testAction = { |
| 28 | + _embedded: { |
| 29 | + indexableObject: { |
| 30 | + type: '' |
| 31 | + } |
| 32 | + } |
| 33 | + }; |
| 34 | + |
| 35 | + const selectableListService = jasmine.createSpyObj('selectableListService', { |
| 36 | + getSelectableList: of({selection: []}), |
| 37 | + deselectAll: jasmine.createSpy('deselectAll') |
| 38 | + }); |
| 39 | + |
| 40 | + const poolTaskService = new PoolTaskDataService(null, null, null, null); |
| 41 | + |
| 42 | + const claimedTaskService = jasmine.createSpyObj('claimedTaskService', { |
| 43 | + submitTask: of(new ProcessTaskResponse(true)) |
| 44 | + }); |
| 45 | + |
| 46 | + const requestService = jasmine.createSpyObj('requestService', { |
| 47 | + removeByHrefSubstring: {}, |
| 48 | + }); |
| 49 | + |
| 50 | + beforeEach(waitForAsync(() => { |
| 51 | + TestBed.configureTestingModule({ |
| 52 | + imports: [TranslateModule.forRoot(), RouterTestingModule.withRoutes([])], |
| 53 | + declarations: [MyDSpaceBulkActionComponent], |
| 54 | + providers: [ |
| 55 | + { provide: SelectableListService, useValue: selectableListService }, |
| 56 | + { provide: RequestService, useValue: requestService }, |
| 57 | + { provide: PoolTaskDataService, useValue: poolTaskService }, |
| 58 | + { provide: ClaimedTaskDataService, useValue: claimedTaskService }, |
| 59 | + { provide: NotificationsService, useValue: new NotificationsServiceStub() }, |
| 60 | + { provide: Router, useValue: new RouterStub() }, |
| 61 | + { provide: SearchService, useValue: new SearchServiceStub('searchLink') }, |
| 62 | + { |
| 63 | + provide: NgbModal, useValue: { |
| 64 | + /* eslint-disable no-empty,@typescript-eslint/no-empty-function */ |
| 65 | + open: () => {} |
| 66 | + } |
| 67 | + }, |
| 68 | + { provide: UntypedFormBuilder, useValue: new UntypedFormBuilder() }, |
| 69 | + ], |
| 70 | + schemas: [NO_ERRORS_SCHEMA] |
| 71 | + }).compileComponents(); |
| 72 | + })); |
| 73 | + |
| 74 | + afterEach(() => { |
| 75 | + fixture.destroy(); |
| 76 | + }); |
| 77 | + |
| 78 | + beforeEach(() => { |
| 79 | + fixture = TestBed.createComponent(MyDSpaceBulkActionComponent); |
| 80 | + comp = fixture.componentInstance; |
| 81 | + comp.processing$.next(false); |
| 82 | + selectableListService.getSelectableList.and.returnValue(of({ selection: []})); |
| 83 | + |
| 84 | + fixture.detectChanges(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should create', () => { |
| 88 | + expect(comp).toBeTruthy(); |
| 89 | + }); |
| 90 | + |
| 91 | + it('all button should be disabled by default', () => { |
| 92 | + const disabledButtons = fixture.debugElement.queryAll(By.css('button[data-test-disabled="true"]')); |
| 93 | + expect(disabledButtons.length).toBe(4); |
| 94 | + }); |
| 95 | + |
| 96 | + it('claim button should be enabled', () => { |
| 97 | + const mockAction = testAction; |
| 98 | + mockAction._embedded.indexableObject.type = 'claimaction'; |
| 99 | + selectableListService.getSelectableList.and.returnValue(of({ selection: [mockAction]})); |
| 100 | + comp.claimEnabled$ = of(true); |
| 101 | + fixture.detectChanges(); |
| 102 | + |
| 103 | + const disabledButtons = fixture.debugElement.queryAll(By.css('button[data-test-disabled="true"]')); |
| 104 | + const claimButton = fixture.debugElement.query(By.css('.btn.btn-info')); |
| 105 | + expect(disabledButtons.length).toBe(3); |
| 106 | + expect(claimButton.nativeElement.disabled).toBeFalsy(); |
| 107 | + }); |
| 108 | + |
| 109 | + it('claimed task buttons should be enabled', () => { |
| 110 | + const mockAction = testAction; |
| 111 | + mockAction._embedded.indexableObject.type = 'claimedtask'; |
| 112 | + selectableListService.getSelectableList.and.returnValue(of({ selection: [mockAction]})); |
| 113 | + comp.claimedTaskActionsEnabled$ = of(true); |
| 114 | + fixture.detectChanges(); |
| 115 | + |
| 116 | + const disabledButtons = fixture.debugElement.queryAll(By.css('button[data-test-disabled="true"]')); |
| 117 | + const approveButton = fixture.debugElement.query(By.css('.btn.btn-success')); |
| 118 | + const rejectButton = fixture.debugElement.query(By.css('.btn.btn-danger')); |
| 119 | + const returnToPoolButton = fixture.debugElement.query(By.css('.btn.btn-secondary')); |
| 120 | + expect(disabledButtons.length).toBe(1); |
| 121 | + expect(approveButton.nativeElement.disabled).toBeFalsy(); |
| 122 | + expect(rejectButton.nativeElement.disabled).toBeFalsy(); |
| 123 | + expect(returnToPoolButton.nativeElement.disabled).toBeFalsy(); |
| 124 | + }); |
| 125 | + |
| 126 | + it('All buttons should be enabled', () => { |
| 127 | + const mockAction = testAction; |
| 128 | + const mockAction2 = testAction; |
| 129 | + mockAction._embedded.indexableObject.type = 'claimaction'; |
| 130 | + mockAction2._embedded.indexableObject.type = 'claimedtask'; |
| 131 | + selectableListService.getSelectableList.and.returnValue(of({ selection: [mockAction, mockAction2]})); |
| 132 | + comp.claimEnabled$ = of(true); |
| 133 | + comp.claimedTaskActionsEnabled$ = of(true); |
| 134 | + fixture.detectChanges(); |
| 135 | + |
| 136 | + const disabledButtons = fixture.debugElement.queryAll(By.css('button[data-test-disabled="true"]')); |
| 137 | + expect(disabledButtons.length).toBe(0); |
| 138 | + }); |
| 139 | + |
| 140 | +}); |
0 commit comments