item-versions.component.spec.ts (dspace-angular-dspace-7.0) | : | item-versions.component.spec.ts (dspace-angular-dspace-7.1) | ||
---|---|---|---|---|
skipping to change at line 14 | skipping to change at line 14 | |||
import { TranslateModule } from '@ngx-translate/core'; | import { TranslateModule } from '@ngx-translate/core'; | |||
import { RouterTestingModule } from '@angular/router/testing'; | import { RouterTestingModule } from '@angular/router/testing'; | |||
import { NO_ERRORS_SCHEMA } from '@angular/core'; | import { NO_ERRORS_SCHEMA } from '@angular/core'; | |||
import { Item } from '../../../core/shared/item.model'; | import { Item } from '../../../core/shared/item.model'; | |||
import { Version } from '../../../core/shared/version.model'; | import { Version } from '../../../core/shared/version.model'; | |||
import { VersionHistory } from '../../../core/shared/version-history.model'; | import { VersionHistory } from '../../../core/shared/version-history.model'; | |||
import { VersionHistoryDataService } from '../../../core/data/version-history-da ta.service'; | import { VersionHistoryDataService } from '../../../core/data/version-history-da ta.service'; | |||
import { By } from '@angular/platform-browser'; | import { By } from '@angular/platform-browser'; | |||
import { createSuccessfulRemoteDataObject$ } from '../../remote-data.utils'; | import { createSuccessfulRemoteDataObject$ } from '../../remote-data.utils'; | |||
import { createPaginatedList } from '../../testing/utils.test'; | import { createPaginatedList } from '../../testing/utils.test'; | |||
import { PaginationComponentOptions } from '../../pagination/pagination-componen | import { EMPTY, of, of as observableOf } from 'rxjs'; | |||
t-options.model'; | ||||
import { SortDirection, SortOptions } from '../../../core/cache/models/sort-opti | ||||
ons.model'; | ||||
import { of as observableOf } from 'rxjs'; | ||||
import { PaginationService } from '../../../core/pagination/pagination.service'; | import { PaginationService } from '../../../core/pagination/pagination.service'; | |||
import { PaginationServiceStub } from '../../testing/pagination-service.stub'; | import { PaginationServiceStub } from '../../testing/pagination-service.stub'; | |||
import { AuthService } from '../../../core/auth/auth.service'; | ||||
import { VersionDataService } from '../../../core/data/version-data.service'; | ||||
import { ItemDataService } from '../../../core/data/item-data.service'; | ||||
import { FormBuilder } from '@angular/forms'; | ||||
import { NotificationsService } from '../../notifications/notifications.service' | ||||
; | ||||
import { NotificationsServiceStub } from '../../testing/notifications-service.st | ||||
ub'; | ||||
import { AuthorizationDataService } from '../../../core/data/feature-authorizati | ||||
on/authorization-data.service'; | ||||
import { FeatureID } from '../../../core/data/feature-authorization/feature-id'; | ||||
import { WorkspaceitemDataService } from '../../../core/submission/workspaceitem | ||||
-data.service'; | ||||
import { WorkflowItemDataService } from '../../../core/submission/workflowitem-d | ||||
ata.service'; | ||||
describe('ItemVersionsComponent', () => { | describe('ItemVersionsComponent', () => { | |||
let component: ItemVersionsComponent; | let component: ItemVersionsComponent; | |||
let fixture: ComponentFixture<ItemVersionsComponent>; | let fixture: ComponentFixture<ItemVersionsComponent>; | |||
let authenticationService: AuthService; | ||||
let authorizationService: AuthorizationDataService; | ||||
let versionHistoryService: VersionHistoryDataService; | ||||
let workspaceItemDataService: WorkspaceitemDataService; | ||||
let workflowItemDataService: WorkflowItemDataService; | ||||
let versionService: VersionDataService; | ||||
const versionHistory = Object.assign(new VersionHistory(), { | const versionHistory = Object.assign(new VersionHistory(), { | |||
id: '1' | id: '1', | |||
draftVersion: true, | ||||
}); | }); | |||
const version1 = Object.assign(new Version(), { | const version1 = Object.assign(new Version(), { | |||
id: '1', | id: '1', | |||
version: 1, | version: 1, | |||
created: new Date(2020, 1, 1), | created: new Date(2020, 1, 1), | |||
summary: 'first version', | summary: 'first version', | |||
versionhistory: createSuccessfulRemoteDataObject$(versionHistory) | versionhistory: createSuccessfulRemoteDataObject$(versionHistory), | |||
_links: { | ||||
self: { | ||||
href: 'version2-url', | ||||
}, | ||||
}, | ||||
}); | }); | |||
const version2 = Object.assign(new Version(), { | const version2 = Object.assign(new Version(), { | |||
id: '2', | id: '2', | |||
version: 2, | version: 2, | |||
summary: 'second version', | summary: 'second version', | |||
created: new Date(2020, 1, 2), | created: new Date(2020, 1, 2), | |||
versionhistory: createSuccessfulRemoteDataObject$(versionHistory) | versionhistory: createSuccessfulRemoteDataObject$(versionHistory), | |||
_links: { | ||||
self: { | ||||
href: 'version2-url', | ||||
}, | ||||
}, | ||||
}); | }); | |||
const versions = [version1, version2]; | const versions = [version1, version2]; | |||
versionHistory.versions = createSuccessfulRemoteDataObject$(createPaginatedLis t(versions)); | versionHistory.versions = createSuccessfulRemoteDataObject$(createPaginatedLis t(versions)); | |||
const item1 = Object.assign(new Item(), { | ||||
const item1 = Object.assign(new Item(), { // is a workspace item | ||||
uuid: 'item-identifier-1', | uuid: 'item-identifier-1', | |||
handle: '123456789/1', | handle: '123456789/1', | |||
version: createSuccessfulRemoteDataObject$(version1) | version: createSuccessfulRemoteDataObject$(version1), | |||
_links: { | ||||
self: { | ||||
href: '/items/item-identifier-1' | ||||
} | ||||
} | ||||
}); | }); | |||
const item2 = Object.assign(new Item(), { | const item2 = Object.assign(new Item(), { | |||
uuid: 'item-identifier-2', | uuid: 'item-identifier-2', | |||
handle: '123456789/2', | handle: '123456789/2', | |||
version: createSuccessfulRemoteDataObject$(version2) | version: createSuccessfulRemoteDataObject$(version2), | |||
_links: { | ||||
self: { | ||||
href: '/items/item-identifier-2' | ||||
} | ||||
} | ||||
}); | }); | |||
const items = [item1, item2]; | const items = [item1, item2]; | |||
version1.item = createSuccessfulRemoteDataObject$(item1); | version1.item = createSuccessfulRemoteDataObject$(item1); | |||
version2.item = createSuccessfulRemoteDataObject$(item2); | version2.item = createSuccessfulRemoteDataObject$(item2); | |||
const versionHistoryService = jasmine.createSpyObj('versionHistoryService', { | ||||
getVersions: createSuccessfulRemoteDataObject$(createPaginatedList(versions) | ||||
) | ||||
}); | ||||
const paginationService = new PaginationServiceStub(); | const versionHistoryServiceSpy = jasmine.createSpyObj('versionHistoryService', | |||
{ | ||||
getVersions: createSuccessfulRemoteDataObject$(createPaginatedList(versions) | ||||
), | ||||
}); | ||||
const authenticationServiceSpy = jasmine.createSpyObj('authenticationService', | ||||
{ | ||||
isAuthenticated: observableOf(true), | ||||
setRedirectUrl: {} | ||||
}); | ||||
const authorizationServiceSpy = jasmine.createSpyObj('authorizationService', [ | ||||
'isAuthorized']); | ||||
const workspaceItemDataServiceSpy = jasmine.createSpyObj('workspaceItemDataSer | ||||
vice', { | ||||
findByItem: EMPTY, | ||||
}); | ||||
const workflowItemDataServiceSpy = jasmine.createSpyObj('workflowItemDataServi | ||||
ce', { | ||||
findByItem: EMPTY, | ||||
}); | ||||
const versionServiceSpy = jasmine.createSpyObj('versionService', { | ||||
findById: EMPTY, | ||||
}); | ||||
beforeEach(waitForAsync(() => { | beforeEach(waitForAsync(() => { | |||
TestBed.configureTestingModule({ | TestBed.configureTestingModule({ | |||
declarations: [ItemVersionsComponent, VarDirective], | declarations: [ItemVersionsComponent, VarDirective], | |||
imports: [TranslateModule.forRoot(), RouterTestingModule.withRoutes([])], | imports: [TranslateModule.forRoot(), RouterTestingModule.withRoutes([])], | |||
providers: [ | providers: [ | |||
{ provide: VersionHistoryDataService, useValue: versionHistoryService }, | {provide: PaginationService, useValue: new PaginationServiceStub()}, | |||
{ provide: PaginationService, useValue: paginationService } | {provide: FormBuilder, useValue: new FormBuilder()}, | |||
{provide: NotificationsService, useValue: new NotificationsServiceStub() | ||||
}, | ||||
{provide: AuthService, useValue: authenticationServiceSpy}, | ||||
{provide: AuthorizationDataService, useValue: authorizationServiceSpy}, | ||||
{provide: VersionHistoryDataService, useValue: versionHistoryServiceSpy} | ||||
, | ||||
{provide: ItemDataService, useValue: {}}, | ||||
{provide: VersionDataService, useValue: versionServiceSpy}, | ||||
{provide: WorkspaceitemDataService, useValue: workspaceItemDataServiceSp | ||||
y}, | ||||
{provide: WorkflowItemDataService, useValue: workflowItemDataServiceSpy} | ||||
, | ||||
], | ], | |||
schemas: [NO_ERRORS_SCHEMA] | schemas: [NO_ERRORS_SCHEMA] | |||
}).compileComponents(); | }).compileComponents(); | |||
versionHistoryService = TestBed.inject(VersionHistoryDataService); | ||||
authenticationService = TestBed.inject(AuthService); | ||||
authorizationService = TestBed.inject(AuthorizationDataService); | ||||
workspaceItemDataService = TestBed.inject(WorkspaceitemDataService); | ||||
workflowItemDataService = TestBed.inject(WorkflowItemDataService); | ||||
versionService = TestBed.inject(VersionDataService); | ||||
})); | })); | |||
beforeEach(() => { | beforeEach(() => { | |||
fixture = TestBed.createComponent(ItemVersionsComponent); | fixture = TestBed.createComponent(ItemVersionsComponent); | |||
component = fixture.componentInstance; | component = fixture.componentInstance; | |||
component.item = item1; | component.item = item1; | |||
component.displayActions = true; | ||||
fixture.detectChanges(); | fixture.detectChanges(); | |||
}); | }); | |||
it(`should display ${versions.length} rows`, () => { | it(`should display ${versions.length} rows`, () => { | |||
const rows = fixture.debugElement.queryAll(By.css('tbody tr')); | const rows = fixture.debugElement.queryAll(By.css('tbody tr')); | |||
expect(rows.length).toBe(versions.length); | expect(rows.length).toBe(versions.length); | |||
}); | }); | |||
versions.forEach((version: Version, index: number) => { | versions.forEach((version: Version, index: number) => { | |||
const versionItem = items[index]; | const versionItem = items[index]; | |||
it(`should display version ${version.version} in the correct column for vers ion ${version.id}`, () => { | it(`should display version ${version.version} in the correct column for vers ion ${version.id}`, () => { | |||
const id = fixture.debugElement.query(By.css(`#version-row-${version.id} . version-row-element-version`)); | const id = fixture.debugElement.query(By.css(`#version-row-${version.id} . version-row-element-version`)); | |||
expect(id.nativeElement.textContent).toEqual('' + version.version); | expect(id.nativeElement.textContent).toContain(version.version.toString()) ; | |||
}); | }); | |||
it(`should display item handle ${versionItem.handle} in the correct column f | // Check if the current version contains an asterisk | |||
or version ${version.id}`, () => { | ||||
const item = fixture.debugElement.query(By.css(`#version-row-${version.id} | ||||
.version-row-element-item`)); | ||||
expect(item.nativeElement.textContent).toContain(versionItem.handle); | ||||
}); | ||||
// This version's item is equal to the component's item (the selected item) | ||||
// Check if the handle contains an asterisk | ||||
if (item1.uuid === versionItem.uuid) { | if (item1.uuid === versionItem.uuid) { | |||
it('should add an asterisk to the handle of the selected item', () => { | it('should add an asterisk to the version of the selected item', () => { | |||
const item = fixture.debugElement.query(By.css(`#version-row-${version.i | const item = fixture.debugElement.query(By.css(`#version-row-${version.i | |||
d} .version-row-element-item`)); | d} .version-row-element-version`)); | |||
expect(item.nativeElement.textContent).toContain('*'); | expect(item.nativeElement.textContent).toContain('*'); | |||
}); | }); | |||
} | } | |||
it(`should display date ${version.created} in the correct column for version ${version.id}`, () => { | it(`should display date ${version.created} in the correct column for version ${version.id}`, () => { | |||
const date = fixture.debugElement.query(By.css(`#version-row-${version.id} .version-row-element-date`)); | const date = fixture.debugElement.query(By.css(`#version-row-${version.id} .version-row-element-date`)); | |||
expect(date.nativeElement.textContent).toEqual('' + version.created); | switch (versionItem.uuid) { | |||
case item1.uuid: | ||||
expect(date.nativeElement.textContent.trim()).toEqual('2020-02-01 00:0 | ||||
0:00'); | ||||
break; | ||||
case item2.uuid: | ||||
expect(date.nativeElement.textContent.trim()).toEqual('2020-02-02 00:0 | ||||
0:00'); | ||||
break; | ||||
default: | ||||
throw new Error('Unexpected versionItem'); | ||||
} | ||||
}); | }); | |||
it(`should display summary ${version.summary} in the correct column for vers ion ${version.id}`, () => { | it(`should display summary ${version.summary} in the correct column for vers ion ${version.id}`, () => { | |||
const summary = fixture.debugElement.query(By.css(`#version-row-${version. id} .version-row-element-summary`)); | const summary = fixture.debugElement.query(By.css(`#version-row-${version. id} .version-row-element-summary`)); | |||
expect(summary.nativeElement.textContent).toEqual(version.summary); | expect(summary.nativeElement.textContent).toEqual(version.summary); | |||
}); | }); | |||
}); | }); | |||
describe('when the user can only delete a version', () => { | ||||
beforeAll(waitForAsync(() => { | ||||
const canDelete = (featureID: FeatureID, url: string ) => of(featureID === | ||||
FeatureID.CanDeleteVersion); | ||||
authorizationServiceSpy.isAuthorized.and.callFake(canDelete); | ||||
})); | ||||
it('should not disable the delete button', () => { | ||||
const deleteButtons = fixture.debugElement.queryAll(By.css(`.version-row-e | ||||
lement-delete`)); | ||||
deleteButtons.forEach((btn) => { | ||||
expect(btn.nativeElement.disabled).toBe(false); | ||||
}); | ||||
}); | ||||
it('should disable other buttons', () => { | ||||
const createButtons = fixture.debugElement.queryAll(By.css(`.version-row-e | ||||
lement-create`)); | ||||
createButtons.forEach((btn) => { | ||||
expect(btn.nativeElement.disabled).toBe(true); | ||||
}); | ||||
const editButtons = fixture.debugElement.queryAll(By.css(`.version-row-ele | ||||
ment-create`)); | ||||
editButtons.forEach((btn) => { | ||||
expect(btn.nativeElement.disabled).toBe(true); | ||||
}); | ||||
}); | ||||
}); | ||||
describe('when page is changed', () => { | ||||
it('should call getAllVersions', () => { | ||||
spyOn(component, 'getAllVersions'); | ||||
component.onPageChange(); | ||||
expect(component.getAllVersions).toHaveBeenCalled(); | ||||
}); | ||||
}); | ||||
describe('when onSummarySubmit() is called', () => { | ||||
const id = 'version-being-edited-id'; | ||||
beforeEach(() => { | ||||
component.versionBeingEditedId = id; | ||||
}); | ||||
it('should call versionService.findById', () => { | ||||
component.onSummarySubmit(); | ||||
expect(versionService.findById).toHaveBeenCalledWith(id); | ||||
}); | ||||
}); | ||||
describe('when editing is enabled for an item', () => { | ||||
beforeEach(() => { | ||||
component.enableVersionEditing(version1); | ||||
}); | ||||
it('should set all variables', () => { | ||||
expect(component.versionBeingEditedSummary).toEqual('first version'); | ||||
expect(component.versionBeingEditedNumber).toEqual(1); | ||||
expect(component.versionBeingEditedId).toEqual('1'); | ||||
}); | ||||
it('isAnyBeingEdited should be true', () => { | ||||
expect(component.isAnyBeingEdited()).toBeTrue(); | ||||
}); | ||||
it('isThisBeingEdited should be true for version1', () => { | ||||
expect(component.isThisBeingEdited(version1)).toBeTrue(); | ||||
}); | ||||
it('isThisBeingEdited should be false for version2', () => { | ||||
expect(component.isThisBeingEdited(version2)).toBeFalse(); | ||||
}); | ||||
}); | ||||
describe('when editing is disabled', () => { | ||||
beforeEach(() => { | ||||
component.disableVersionEditing(); | ||||
}); | ||||
it('should unset all variables', () => { | ||||
expect(component.versionBeingEditedSummary).toBeUndefined(); | ||||
expect(component.versionBeingEditedNumber).toBeUndefined(); | ||||
expect(component.versionBeingEditedId).toBeUndefined(); | ||||
}); | ||||
it('isAnyBeingEdited should be false', () => { | ||||
expect(component.isAnyBeingEdited()).toBeFalse(); | ||||
}); | ||||
it('isThisBeingEdited should be false for all versions', () => { | ||||
expect(component.isThisBeingEdited(version1)).toBeFalse(); | ||||
expect(component.isThisBeingEdited(version2)).toBeFalse(); | ||||
}); | ||||
}); | ||||
}); | }); | |||
End of changes. 21 change blocks. | ||||
32 lines changed or deleted | 200 lines changed or added |