|
1 | | -// |
2 | | -// describe('AccessControlFormContainerComponent', () => { |
3 | | -// let component: AccessControlFormContainerComponent<any>; |
4 | | -// let fixture: ComponentFixture<AccessControlFormContainerComponent<any>>; |
5 | | -// |
6 | | -// let bulkAccessConfigDataServiceMock: BulkAccessConfigDataService; |
7 | | -// |
8 | | -// beforeEach(async () => { |
9 | | -// |
10 | | -// bulkAccessConfigDataServiceMock = jasmine.createSpyObj('BulkAccessConfigDataService', { |
11 | | -// findByName: jasmine.createSpy('findByName'), |
12 | | -// }); |
13 | | -// |
14 | | -// |
15 | | -// await TestBed.configureTestingModule({ |
16 | | -// declarations: [ AccessControlFormContainerComponent ], |
17 | | -// imports: [ CommonModule, ReactiveFormsModule, SharedBrowseByModule, TranslateModule, NgbDatepickerModule ], |
18 | | -// providers: [ |
19 | | -// { provide: BulkAccessConfigDataService, useValue: bulkAccessConfigDataServiceMock }, |
20 | | -// // private bulkAccessControlService: BulkAccessControlService, |
21 | | -// // private selectableListService: SelectableListService, |
22 | | -// // protected modalService: NgbModal, |
23 | | -// // private cdr: ChangeDetectorRef |
24 | | -// ] |
25 | | -// }) |
26 | | -// .compileComponents(); |
27 | | -// }); |
28 | | -// |
29 | | -// beforeEach(() => { |
30 | | -// fixture = TestBed.createComponent(AccessControlFormContainerComponent); |
31 | | -// component = fixture.componentInstance; |
32 | | -// fixture.detectChanges(); |
33 | | -// }); |
34 | | -// |
35 | | -// it('should create', () => { |
36 | | -// expect(component).toBeTruthy(); |
37 | | -// }); |
38 | | -// }); |
| 1 | +import {ComponentFixture, fakeAsync, TestBed} from '@angular/core/testing'; |
| 2 | +import {NgbDatepickerModule, NgbModal, NgbModalRef} from '@ng-bootstrap/ng-bootstrap'; |
| 3 | +import {Component} from '@angular/core'; |
| 4 | +import {of} from 'rxjs'; |
| 5 | +import {AccessControlFormContainerComponent} from './access-control-form-container.component'; |
| 6 | +import {BulkAccessControlService} from './bulk-access-control.service'; |
| 7 | +import {BulkAccessConfigDataService} from '../../core/config/bulk-access-config-data.service'; |
| 8 | +import {Item} from '../../core/shared/item.model'; |
| 9 | +import {SelectableListService} from '../object-list/selectable-list/selectable-list.service'; |
| 10 | +import {createAccessControlInitialFormState} from './access-control-form-container-intial-state'; |
| 11 | +import {CommonModule} from '@angular/common'; |
| 12 | +import {SharedBrowseByModule} from '../browse-by/shared-browse-by.module'; |
| 13 | +import {TranslateModule} from '@ngx-translate/core'; |
| 14 | +import {FormsModule} from '@angular/forms'; |
| 15 | +import {UiSwitchModule} from 'ngx-ui-switch'; |
| 16 | +import { |
| 17 | + ITEM_ACCESS_CONTROL_SELECT_BITSTREAMS_LIST_ID |
| 18 | +} from './item-access-control-select-bitstreams-modal/item-access-control-select-bitstreams-modal.component'; |
| 19 | +import {AccessControlFormModule} from './access-control-form.module'; |
| 20 | + |
| 21 | + |
| 22 | +describe('AccessControlFormContainerComponent', () => { |
| 23 | + let component: AccessControlFormContainerComponent<any>; |
| 24 | + let fixture: ComponentFixture<AccessControlFormContainerComponent<any>>; |
| 25 | + |
| 26 | + |
| 27 | +// Mock NgbModal |
| 28 | + @Component({selector: 'ds-ngb-modal', template: ''}) |
| 29 | + class MockNgbModalComponent { |
| 30 | + } |
| 31 | + |
| 32 | +// Mock dependencies |
| 33 | + const mockBulkAccessControlService = { |
| 34 | + createPayloadFile: jasmine.createSpy('createPayloadFile').and.returnValue({file: 'mocked-file'}), |
| 35 | + executeScript: jasmine.createSpy('executeScript').and.returnValue(of('success')), |
| 36 | + }; |
| 37 | + |
| 38 | + const mockBulkAccessConfigDataService = { |
| 39 | + findByName: jasmine.createSpy('findByName').and.returnValue(of({payload: {options: []}})), |
| 40 | + }; |
| 41 | + |
| 42 | + const mockSelectableListService = { |
| 43 | + getSelectableList: jasmine.createSpy('getSelectableList').and.returnValue(of({selection: []})), |
| 44 | + deselectAll: jasmine.createSpy('deselectAll'), |
| 45 | + }; |
| 46 | + |
| 47 | + const mockNgbModal = { |
| 48 | + open: jasmine.createSpy('open').and.returnValue( |
| 49 | + { componentInstance: {}, closed: of({})} as NgbModalRef |
| 50 | + ) |
| 51 | + }; |
| 52 | + |
| 53 | + beforeEach(async () => { |
| 54 | + await TestBed.configureTestingModule({ |
| 55 | + declarations: [AccessControlFormContainerComponent, MockNgbModalComponent], |
| 56 | + imports: [ |
| 57 | + CommonModule, |
| 58 | + FormsModule, |
| 59 | + SharedBrowseByModule, |
| 60 | + AccessControlFormModule, |
| 61 | + TranslateModule.forRoot(), |
| 62 | + NgbDatepickerModule, |
| 63 | + UiSwitchModule |
| 64 | + ], |
| 65 | + providers: [ |
| 66 | + {provide: BulkAccessControlService, useValue: mockBulkAccessControlService}, |
| 67 | + {provide: BulkAccessConfigDataService, useValue: mockBulkAccessConfigDataService}, |
| 68 | + {provide: SelectableListService, useValue: mockSelectableListService}, |
| 69 | + {provide: NgbModal, useValue: mockNgbModal}, |
| 70 | + ], |
| 71 | + }).compileComponents(); |
| 72 | + }); |
| 73 | + |
| 74 | + beforeEach(() => { |
| 75 | + fixture = TestBed.createComponent(AccessControlFormContainerComponent); |
| 76 | + component = fixture.componentInstance; |
| 77 | + component.state = createAccessControlInitialFormState(); |
| 78 | + fixture.detectChanges(); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should create the component', () => { |
| 82 | + expect(component).toBeTruthy(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should reset the form', fakeAsync(() => { |
| 86 | + fixture.detectChanges(); |
| 87 | + const resetSpy = spyOn(component.bitstreamAccessCmp, 'reset'); |
| 88 | + spyOn(component.itemAccessCmp, 'reset'); |
| 89 | + |
| 90 | + component.reset(); |
| 91 | + |
| 92 | + expect(resetSpy).toHaveBeenCalled(); |
| 93 | + expect(component.itemAccessCmp.reset).toHaveBeenCalled(); |
| 94 | + expect(component.state).toEqual(createAccessControlInitialFormState()); |
| 95 | + })); |
| 96 | + |
| 97 | + it('should submit the form', () => { |
| 98 | + const bitstreamAccess = 'bitstreamAccess'; |
| 99 | + const itemAccess = 'itemAccess'; |
| 100 | + component.bitstreamAccessCmp.getValue = jasmine.createSpy('getValue').and.returnValue(bitstreamAccess); |
| 101 | + component.itemAccessCmp.getValue = jasmine.createSpy('getValue').and.returnValue(itemAccess); |
| 102 | + component.itemRD = {payload: {uuid: 'item-uuid'}} as any; |
| 103 | + |
| 104 | + component.submit(); |
| 105 | + |
| 106 | + expect(mockBulkAccessControlService.createPayloadFile).toHaveBeenCalledWith({ |
| 107 | + bitstreamAccess, |
| 108 | + itemAccess, |
| 109 | + state: createAccessControlInitialFormState(), |
| 110 | + }); |
| 111 | + expect(mockBulkAccessControlService.executeScript).toHaveBeenCalledWith(['item-uuid'], 'mocked-file'); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should handle the status change for bitstream access', () => { |
| 115 | + component.bitstreamAccessCmp.enable = jasmine.createSpy('enable'); |
| 116 | + component.bitstreamAccessCmp.disable = jasmine.createSpy('disable'); |
| 117 | + |
| 118 | + component.handleStatusChange('bitstream', true); |
| 119 | + expect(component.bitstreamAccessCmp.enable).toHaveBeenCalled(); |
| 120 | + |
| 121 | + component.handleStatusChange('bitstream', false); |
| 122 | + expect(component.bitstreamAccessCmp.disable).toHaveBeenCalled(); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should handle the status change for item access', () => { |
| 126 | + component.itemAccessCmp.enable = jasmine.createSpy('enable'); |
| 127 | + component.itemAccessCmp.disable = jasmine.createSpy('disable'); |
| 128 | + |
| 129 | + component.handleStatusChange('item', true); |
| 130 | + expect(component.itemAccessCmp.enable).toHaveBeenCalled(); |
| 131 | + |
| 132 | + component.handleStatusChange('item', false); |
| 133 | + expect(component.itemAccessCmp.disable).toHaveBeenCalled(); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should open the select bitstreams modal', () => { |
| 137 | + const modalService = TestBed.inject(NgbModal); |
| 138 | + |
| 139 | + component.openSelectBitstreamsModal(new Item()); |
| 140 | + expect(modalService.open).toHaveBeenCalled(); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should unsubscribe and deselect all on component destroy', () => { |
| 144 | + component.ngOnDestroy(); |
| 145 | + expect(component.selectableListService.deselectAll).toHaveBeenCalledWith( |
| 146 | + ITEM_ACCESS_CONTROL_SELECT_BITSTREAMS_LIST_ID |
| 147 | + ); |
| 148 | + }); |
| 149 | +}); |
0 commit comments