|
| 1 | +import { CommonModule } from '@angular/common'; |
| 2 | +import { NO_ERRORS_SCHEMA } from '@angular/core'; |
| 3 | +import { |
| 4 | + ComponentFixture, |
| 5 | + TestBed, |
| 6 | + waitForAsync, |
| 7 | +} from '@angular/core/testing'; |
| 8 | +import { |
| 9 | + FormsModule, |
| 10 | + ReactiveFormsModule, |
| 11 | +} from '@angular/forms'; |
| 12 | +import { |
| 13 | + BrowserModule, |
| 14 | + By, |
| 15 | +} from '@angular/platform-browser'; |
| 16 | +import { |
| 17 | + ActivatedRoute, |
| 18 | + RouterModule, |
| 19 | +} from '@angular/router'; |
| 20 | +import { TranslateModule } from '@ngx-translate/core'; |
| 21 | +import { |
| 22 | + EMPTY, |
| 23 | + of as observableOf, |
| 24 | + of, |
| 25 | +} from 'rxjs'; |
| 26 | + |
| 27 | +import { AuthorizationDataService } from '../../../core/data/feature-authorization/authorization-data.service'; |
| 28 | +import { ItemDataService } from '../../../core/data/item-data.service'; |
| 29 | +import { VersionDataService } from '../../../core/data/version-data.service'; |
| 30 | +import { VersionHistoryDataService } from '../../../core/data/version-history-data.service'; |
| 31 | +import { Item } from '../../../core/shared/item.model'; |
| 32 | +import { Version } from '../../../core/shared/version.model'; |
| 33 | +import { VersionHistory } from '../../../core/shared/version-history.model'; |
| 34 | +import { WorkflowItemDataService } from '../../../core/submission/workflowitem-data.service'; |
| 35 | +import { WorkspaceitemDataService } from '../../../core/submission/workspaceitem-data.service'; |
| 36 | +import { NotificationsService } from '../../../shared/notifications/notifications.service'; |
| 37 | +import { createSuccessfulRemoteDataObject$ } from '../../../shared/remote-data.utils'; |
| 38 | +import { ActivatedRouteStub } from '../../../shared/testing/active-router.stub'; |
| 39 | +import { NotificationsServiceStub } from '../../../shared/testing/notifications-service.stub'; |
| 40 | +import { createPaginatedList } from '../../../shared/testing/utils.test'; |
| 41 | +import { ItemVersionsComponent } from '../item-versions.component'; |
| 42 | +import { ItemVersionsRowElementVersionComponent } from './item-versions-row-element-version.component'; |
| 43 | + |
| 44 | +describe('ItemVersionsRowElementVersionComponent', () => { |
| 45 | + let component: ItemVersionsRowElementVersionComponent; |
| 46 | + let fixture: ComponentFixture<ItemVersionsRowElementVersionComponent>; |
| 47 | + |
| 48 | + const versionHistory = Object.assign(new VersionHistory(), { |
| 49 | + id: '1', |
| 50 | + draftVersion: true, |
| 51 | + }); |
| 52 | + |
| 53 | + const version = Object.assign(new Version(), { |
| 54 | + id: '1', |
| 55 | + version: 1, |
| 56 | + created: new Date(2020, 1, 1), |
| 57 | + summary: 'first version', |
| 58 | + versionhistory: createSuccessfulRemoteDataObject$(versionHistory), |
| 59 | + _links: { |
| 60 | + self: { |
| 61 | + href: 'version2-url', |
| 62 | + }, |
| 63 | + }, |
| 64 | + }); |
| 65 | + |
| 66 | + versionHistory.versions = createSuccessfulRemoteDataObject$(createPaginatedList([version])); |
| 67 | + |
| 68 | + |
| 69 | + const item = Object.assign(new Item(), { // is a workspace item |
| 70 | + id: 'item-identifier-1', |
| 71 | + uuid: 'item-identifier-1', |
| 72 | + handle: '123456789/1', |
| 73 | + version: createSuccessfulRemoteDataObject$(version), |
| 74 | + _links: { |
| 75 | + self: { |
| 76 | + href: '/items/item-identifier-1', |
| 77 | + }, |
| 78 | + }, |
| 79 | + }); |
| 80 | + |
| 81 | + version.item = createSuccessfulRemoteDataObject$(item); |
| 82 | + |
| 83 | + const versionHistoryServiceSpy = jasmine.createSpyObj('versionHistoryService', { |
| 84 | + getVersions: createSuccessfulRemoteDataObject$(createPaginatedList([version])), |
| 85 | + getVersionHistoryFromVersion$: of(versionHistory), |
| 86 | + getLatestVersionItemFromHistory$: of(item), |
| 87 | + }); |
| 88 | + const authorizationServiceSpy = jasmine.createSpyObj('authorizationService', { |
| 89 | + isAuthorized: observableOf(true), |
| 90 | + }); |
| 91 | + const workspaceItemDataServiceSpy = jasmine.createSpyObj('workspaceItemDataService', { |
| 92 | + findByItem: EMPTY, |
| 93 | + }); |
| 94 | + const workflowItemDataServiceSpy = jasmine.createSpyObj('workflowItemDataService', { |
| 95 | + findByItem: EMPTY, |
| 96 | + }); |
| 97 | + const versionServiceSpy = jasmine.createSpyObj('versionService', { |
| 98 | + findById: EMPTY, |
| 99 | + }); |
| 100 | + const itemDataServiceSpy = jasmine.createSpyObj('itemDataService', { |
| 101 | + delete: createSuccessfulRemoteDataObject$({}), |
| 102 | + }); |
| 103 | + |
| 104 | + beforeEach(async () => { |
| 105 | + await TestBed.configureTestingModule({ |
| 106 | + imports: [TranslateModule.forRoot(), RouterModule.forRoot([ |
| 107 | + { path: 'items/:id/edit/versionhistory', component: {} as any }, |
| 108 | + ]), CommonModule, FormsModule, ReactiveFormsModule, BrowserModule, ItemVersionsComponent], |
| 109 | + providers: [ |
| 110 | + { provide: NotificationsService, useValue: new NotificationsServiceStub() }, |
| 111 | + { provide: AuthorizationDataService, useValue: authorizationServiceSpy }, |
| 112 | + { provide: VersionHistoryDataService, useValue: versionHistoryServiceSpy }, |
| 113 | + { provide: ItemDataService, useValue: itemDataServiceSpy }, |
| 114 | + { provide: VersionDataService, useValue: versionServiceSpy }, |
| 115 | + { provide: WorkspaceitemDataService, useValue: workspaceItemDataServiceSpy }, |
| 116 | + { provide: WorkflowItemDataService, useValue: workflowItemDataServiceSpy }, |
| 117 | + { provide: ActivatedRoute, useValue: new ActivatedRouteStub() }, |
| 118 | + ], |
| 119 | + schemas: [NO_ERRORS_SCHEMA], |
| 120 | + }) |
| 121 | + .compileComponents(); |
| 122 | + |
| 123 | + fixture = TestBed.createComponent(ItemVersionsRowElementVersionComponent); |
| 124 | + component = fixture.componentInstance; |
| 125 | + |
| 126 | + component.version = version; |
| 127 | + component.itemVersion = version; |
| 128 | + component.item = item; |
| 129 | + component.displayActions = true; |
| 130 | + |
| 131 | + fixture.detectChanges(); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should create', () => { |
| 135 | + expect(component).toBeTruthy(); |
| 136 | + }); |
| 137 | + |
| 138 | + it(`should display version ${version.version} in the correct column for version ${version.id}`, () => { |
| 139 | + const id = fixture.debugElement.query(By.css(`.left-column`)); |
| 140 | + expect(id.nativeElement.textContent).toContain(version.version.toString()); |
| 141 | + }); |
| 142 | + |
| 143 | + it(`should displau an asterisk in the correct column for current version`, () => { |
| 144 | + const draft = fixture.debugElement.query(By.css(`.left-column`)); |
| 145 | + expect(draft.nativeElement.textContent).toContain('*'); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should display action buttons in the correct column if displayActions is true', () => { |
| 149 | + fixture.detectChanges(); |
| 150 | + const actions = fixture.debugElement.query(By.css(`.right-column`)); |
| 151 | + expect(actions).toBeTruthy(); |
| 152 | + }); |
| 153 | + |
| 154 | + describe('when deleting a version', () => { |
| 155 | + let deleteButton; |
| 156 | + |
| 157 | + beforeEach(() => { |
| 158 | + deleteButton = fixture.debugElement.queryAll(By.css('.version-row-element-delete'))[0].nativeElement; |
| 159 | + |
| 160 | + itemDataServiceSpy.delete.calls.reset(); |
| 161 | + }); |
| 162 | + |
| 163 | + describe('if confirmed via modal', () => { |
| 164 | + beforeEach(waitForAsync(() => { |
| 165 | + deleteButton.click(); |
| 166 | + fixture.detectChanges(); |
| 167 | + (document as any).querySelector('.modal-footer .confirm').click(); |
| 168 | + })); |
| 169 | + |
| 170 | + it('should call ItemService.delete', () => { |
| 171 | + expect(itemDataServiceSpy.delete).toHaveBeenCalledWith(item.id); |
| 172 | + }); |
| 173 | + }); |
| 174 | + |
| 175 | + describe('if canceled via modal', () => { |
| 176 | + beforeEach(waitForAsync(() => { |
| 177 | + deleteButton.click(); |
| 178 | + fixture.detectChanges(); |
| 179 | + (document as any).querySelector('.modal-footer .cancel').click(); |
| 180 | + })); |
| 181 | + |
| 182 | + it('should not call ItemService.delete', () => { |
| 183 | + expect(itemDataServiceSpy.delete).not.toHaveBeenCalled(); |
| 184 | + }); |
| 185 | + }); |
| 186 | + }); |
| 187 | + |
| 188 | +}); |
0 commit comments