Skip to content

Commit d6595da

Browse files
author
Andrea Barbasso
committed
[DURACOM-248] move tests
1 parent 87cff6c commit d6595da

2 files changed

Lines changed: 163 additions & 54 deletions

File tree

Lines changed: 163 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,188 @@
1+
import { CommonModule } from '@angular/common';
2+
import { NO_ERRORS_SCHEMA } from '@angular/core';
13
import {
24
ComponentFixture,
35
TestBed,
6+
waitForAsync,
47
} 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';
526

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';
642
import { ItemVersionsRowElementVersionComponent } from './item-versions-row-element-version.component';
743

844
describe('ItemVersionsRowElementVersionComponent', () => {
945
let component: ItemVersionsRowElementVersionComponent;
1046
let fixture: ComponentFixture<ItemVersionsRowElementVersionComponent>;
1147

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+
12104
beforeEach(async () => {
13105
await TestBed.configureTestingModule({
14-
imports: [ItemVersionsRowElementVersionComponent]
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],
15120
})
16121
.compileComponents();
17122

18123
fixture = TestBed.createComponent(ItemVersionsRowElementVersionComponent);
19124
component = fixture.componentInstance;
125+
126+
component.version = version;
127+
component.itemVersion = version;
128+
component.item = item;
129+
component.displayActions = true;
130+
20131
fixture.detectChanges();
21132
});
22133

23134
it('should create', () => {
24135
expect(component).toBeTruthy();
25136
});
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+
26188
});

src/app/item-page/versions/item-versions.component.spec.ts

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -206,19 +206,6 @@ describe('ItemVersionsComponent', () => {
206206
versions.forEach((version: Version, index: number) => {
207207
const versionItem = items[index];
208208

209-
it(`should display version ${version.version} in the correct column for version ${version.id}`, () => {
210-
const id = fixture.debugElement.query(By.css(`#version-row-${version.id} .version-row-element-version`));
211-
expect(id.nativeElement.textContent).toContain(version.version.toString());
212-
});
213-
214-
// Check if the current version contains an asterisk
215-
if (item1.uuid === versionItem.uuid) {
216-
it('should add an asterisk to the version of the selected item', () => {
217-
const item = fixture.debugElement.query(By.css(`#version-row-${version.id} .version-row-element-version`));
218-
expect(item.nativeElement.textContent).toContain('*');
219-
});
220-
}
221-
222209
it(`should display date ${version.created} in the correct column for version ${version.id}`, () => {
223210
const date = fixture.debugElement.query(By.css(`#version-row-${version.id} .version-row-element-date`));
224211
switch (versionItem.uuid) {
@@ -319,44 +306,4 @@ describe('ItemVersionsComponent', () => {
319306
expect(component.isThisBeingEdited(version2)).toBeFalse();
320307
});
321308
});
322-
323-
describe('when deleting a version', () => {
324-
let deleteButton;
325-
326-
beforeEach(() => {
327-
const canDelete = (featureID: FeatureID, url: string ) => of(featureID === FeatureID.CanDeleteVersion);
328-
authorizationServiceSpy.isAuthorized.and.callFake(canDelete);
329-
330-
fixture.detectChanges();
331-
332-
// delete the last version in the table (version2 → item2)
333-
deleteButton = fixture.debugElement.queryAll(By.css('.version-row-element-delete'))[1].nativeElement;
334-
335-
itemDataServiceSpy.delete.calls.reset();
336-
});
337-
338-
describe('if confirmed via modal', () => {
339-
beforeEach(waitForAsync(() => {
340-
deleteButton.click();
341-
fixture.detectChanges();
342-
(document as any).querySelector('.modal-footer .confirm').click();
343-
}));
344-
345-
it('should call ItemService.delete', () => {
346-
expect(itemDataServiceSpy.delete).toHaveBeenCalledWith(item2.id);
347-
});
348-
});
349-
350-
describe('if canceled via modal', () => {
351-
beforeEach(waitForAsync(() => {
352-
deleteButton.click();
353-
fixture.detectChanges();
354-
(document as any).querySelector('.modal-footer .cancel').click();
355-
}));
356-
357-
it('should not call ItemService.delete', () => {
358-
expect(itemDataServiceSpy.delete).not.toHaveBeenCalled();
359-
});
360-
});
361-
});
362309
});

0 commit comments

Comments
 (0)