import { ItemVersionsComponent } from './item-versions.component'; import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; import { VarDirective } from '../../utils/var.directive'; import { TranslateModule } from '@ngx-translate/core'; import { RouterTestingModule } from '@angular/router/testing'; import { NO_ERRORS_SCHEMA } from '@angular/core'; import { Item } from '../../../core/shared/item.model'; import { Version } from '../../../core/shared/version.model'; import { VersionHistory } from '../../../core/shared/version-history.model'; import { VersionHistoryDataService } from '../../../core/data/version-history-data.service'; import { By } from '@angular/platform-browser'; import { createSuccessfulRemoteDataObject$ } from '../../remote-data.utils'; import { createPaginatedList } from '../../testing/utils.test'; import { PaginationComponentOptions } from '../../pagination/pagination-component-options.model'; import { SortDirection, SortOptions } from '../../../core/cache/models/sort-options.model'; import { of as observableOf } from 'rxjs'; import { PaginationService } from '../../../core/pagination/pagination.service'; describe('ItemVersionsComponent', () => { let component: ItemVersionsComponent; let fixture: ComponentFixture; const pagination = Object.assign(new PaginationComponentOptions(), { currentPage: 1, pageSize: 20 }); const sort = new SortOptions('score', SortDirection.DESC); const versionHistory = Object.assign(new VersionHistory(), { id: '1' }); const version1 = Object.assign(new Version(), { id: '1', version: 1, created: new Date(2020, 1, 1), summary: 'first version', versionhistory: createSuccessfulRemoteDataObject$(versionHistory) }); const version2 = Object.assign(new Version(), { id: '2', version: 2, summary: 'second version', created: new Date(2020, 1, 2), versionhistory: createSuccessfulRemoteDataObject$(versionHistory) }); const versions = [version1, version2]; versionHistory.versions = createSuccessfulRemoteDataObject$(createPaginatedList(versions)); const item1 = Object.assign(new Item(), { uuid: 'item-identifier-1', handle: '123456789/1', version: createSuccessfulRemoteDataObject$(version1) }); const item2 = Object.assign(new Item(), { uuid: 'item-identifier-2', handle: '123456789/2', version: createSuccessfulRemoteDataObject$(version2) }); const items = [item1, item2]; version1.item = createSuccessfulRemoteDataObject$(item1); version2.item = createSuccessfulRemoteDataObject$(item2); const versionHistoryService = jasmine.createSpyObj('versionHistoryService', { getVersions: createSuccessfulRemoteDataObject$(createPaginatedList(versions)) }); const paginationService = jasmine.createSpyObj('PaginationService', { getCurrentPagination: observableOf(pagination), getCurrentSort: observableOf(sort), getRouteParameterValue: observableOf('') }); beforeEach(waitForAsync(() => { TestBed.configureTestingModule({ declarations: [ItemVersionsComponent, VarDirective], imports: [TranslateModule.forRoot(), RouterTestingModule.withRoutes([])], providers: [ { provide: VersionHistoryDataService, useValue: versionHistoryService }, { provide: PaginationService, useValue: paginationService } ], schemas: [NO_ERRORS_SCHEMA] }).compileComponents(); })); beforeEach(() => { fixture = TestBed.createComponent(ItemVersionsComponent); component = fixture.componentInstance; component.item = item1; fixture.detectChanges(); }); it(`should display ${versions.length} rows`, () => { const rows = fixture.debugElement.queryAll(By.css('tbody tr')); expect(rows.length).toBe(versions.length); }); versions.forEach((version: Version, index: number) => { const versionItem = items[index]; it(`should display version ${version.version} in the correct column for version ${version.id}`, () => { const id = fixture.debugElement.query(By.css(`#version-row-${version.id} .version-row-element-version`)); expect(id.nativeElement.textContent).toEqual('' + version.version); }); it(`should display item handle ${versionItem.handle} in the correct column for 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) { it('should add an asterisk to the handle of the selected item', () => { const item = fixture.debugElement.query(By.css(`#version-row-${version.id} .version-row-element-item`)); expect(item.nativeElement.textContent).toContain('*'); }); } 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`)); expect(date.nativeElement.textContent).toEqual('' + version.created); }); it(`should display summary ${version.summary} in the correct column for version ${version.id}`, () => { const summary = fixture.debugElement.query(By.css(`#version-row-${version.id} .version-row-element-summary`)); expect(summary.nativeElement.textContent).toEqual(version.summary); }); }); });