58789: MetadataSchemaComponent tests

This commit is contained in:
Kristof De Langhe
2019-01-23 15:27:49 +01:00
parent cba07300d1
commit 374f7acd7a
2 changed files with 50 additions and 9 deletions

View File

@@ -109,5 +109,5 @@ describe('MetadataRegistryComponent', () => {
expect(registryService.cancelEditMetadataSchema).toHaveBeenCalled(); expect(registryService.cancelEditMetadataSchema).toHaveBeenCalled();
}); });
})); }));
}) });
}); });

View File

@@ -1,5 +1,5 @@
import { MetadataSchemaComponent } from './metadata-schema.component'; import { MetadataSchemaComponent } from './metadata-schema.component';
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { async, ComponentFixture, inject, TestBed } from '@angular/core/testing';
import { of as observableOf } from 'rxjs'; import { of as observableOf } from 'rxjs';
import { RemoteData } from '../../../core/data/remote-data'; import { RemoteData } from '../../../core/data/remote-data';
import { PaginatedList } from '../../../core/data/paginated-list'; import { PaginatedList } from '../../../core/data/paginated-list';
@@ -17,8 +17,11 @@ import { HostWindowService } from '../../../shared/host-window.service';
import { RouterStub } from '../../../shared/testing/router-stub'; import { RouterStub } from '../../../shared/testing/router-stub';
import { RouterTestingModule } from '@angular/router/testing'; import { RouterTestingModule } from '@angular/router/testing';
import { ActivatedRouteStub } from '../../../shared/testing/active-router-stub'; import { ActivatedRouteStub } from '../../../shared/testing/active-router-stub';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { NotificationsService } from '../../../shared/notifications/notifications.service';
import { NotificationsServiceStub } from '../../../shared/testing/notifications-service-stub';
describe('MetadataSchemaComponent', () => { fdescribe('MetadataSchemaComponent', () => {
let comp: MetadataSchemaComponent; let comp: MetadataSchemaComponent;
let fixture: ComponentFixture<MetadataSchemaComponent>; let fixture: ComponentFixture<MetadataSchemaComponent>;
let registryService: RegistryService; let registryService: RegistryService;
@@ -67,11 +70,17 @@ describe('MetadataSchemaComponent', () => {
} }
]; ];
const mockSchemas = observableOf(new RemoteData(false, false, true, undefined, new PaginatedList(null, mockSchemasList))); const mockSchemas = observableOf(new RemoteData(false, false, true, undefined, new PaginatedList(null, mockSchemasList)));
/* tslint:disable:no-empty */
const registryServiceStub = { const registryServiceStub = {
getMetadataSchemas: () => mockSchemas, getMetadataSchemas: () => mockSchemas,
getMetadataFieldsBySchema: (schema: MetadataSchema) => observableOf(new RemoteData(false, false, true, undefined, new PaginatedList(null, mockFieldsList.filter((value) => value.schema === schema)))), getMetadataFieldsBySchema: (schema: MetadataSchema) => observableOf(new RemoteData(false, false, true, undefined, new PaginatedList(null, mockFieldsList.filter((value) => value.schema === schema)))),
getMetadataSchemaByName: (schemaName: string) => observableOf(new RemoteData(false, false, true, undefined, mockSchemasList.filter((value) => value.prefix === schemaName)[0])) getMetadataSchemaByName: (schemaName: string) => observableOf(new RemoteData(false, false, true, undefined, mockSchemasList.filter((value) => value.prefix === schemaName)[0])),
getActiveMetadataField: () => observableOf(undefined),
getSelectedMetadataFields: () => observableOf([]),
editMetadataField: (schema) => {},
cancelEditMetadataField: () => {}
}; };
/* tslint:enable:no-empty */
const schemaNameParam = 'mock'; const schemaNameParam = 'mock';
const activatedRouteStub = Object.assign(new ActivatedRouteStub(), { const activatedRouteStub = Object.assign(new ActivatedRouteStub(), {
params: observableOf({ params: observableOf({
@@ -87,8 +96,10 @@ describe('MetadataSchemaComponent', () => {
{ provide: RegistryService, useValue: registryServiceStub }, { provide: RegistryService, useValue: registryServiceStub },
{ provide: ActivatedRoute, useValue: activatedRouteStub }, { provide: ActivatedRoute, useValue: activatedRouteStub },
{ provide: HostWindowService, useValue: new HostWindowServiceStub(0) }, { provide: HostWindowService, useValue: new HostWindowServiceStub(0) },
{ provide: Router, useValue: new RouterStub() } { provide: Router, useValue: new RouterStub() },
] { provide: NotificationsService, useValue: NotificationsServiceStub }
],
schemas: [NO_ERRORS_SCHEMA]
}).compileComponents(); }).compileComponents();
})); }));
@@ -96,9 +107,12 @@ describe('MetadataSchemaComponent', () => {
fixture = TestBed.createComponent(MetadataSchemaComponent); fixture = TestBed.createComponent(MetadataSchemaComponent);
comp = fixture.componentInstance; comp = fixture.componentInstance;
fixture.detectChanges(); fixture.detectChanges();
registryService = (comp as any).service;
}); });
beforeEach(inject([RegistryService], (s) => {
registryService = s;
}));
it('should contain the schema prefix in the header', () => { it('should contain the schema prefix in the header', () => {
const header: HTMLElement = fixture.debugElement.query(By.css('.metadata-schema #header')).nativeElement; const header: HTMLElement = fixture.debugElement.query(By.css('.metadata-schema #header')).nativeElement;
expect(header.textContent).toContain('mock'); expect(header.textContent).toContain('mock');
@@ -110,10 +124,37 @@ describe('MetadataSchemaComponent', () => {
}); });
it('should contain the correct fields', () => { it('should contain the correct fields', () => {
const editorField: HTMLElement = fixture.debugElement.query(By.css('#metadata-fields tr:nth-child(1) td:nth-child(1)')).nativeElement; const editorField: HTMLElement = fixture.debugElement.query(By.css('#metadata-fields tr:nth-child(1) td:nth-child(2)')).nativeElement;
expect(editorField.textContent).toBe('mock.contributor.editor'); expect(editorField.textContent).toBe('mock.contributor.editor');
const illustratorField: HTMLElement = fixture.debugElement.query(By.css('#metadata-fields tr:nth-child(2) td:nth-child(1)')).nativeElement; const illustratorField: HTMLElement = fixture.debugElement.query(By.css('#metadata-fields tr:nth-child(2) td:nth-child(2)')).nativeElement;
expect(illustratorField.textContent).toBe('mock.contributor.illustrator'); expect(illustratorField.textContent).toBe('mock.contributor.illustrator');
}); });
describe('when clicking a metadata field row', () => {
let row: HTMLElement;
beforeEach(() => {
spyOn(registryService, 'editMetadataField');
row = fixture.debugElement.query(By.css('.selectable-row')).nativeElement;
row.click();
fixture.detectChanges();
});
it('should start editing the selected field', async(() => {
fixture.whenStable().then(() => {
expect(registryService.editMetadataField).toHaveBeenCalledWith(mockFieldsList[2]);
});
}));
it('should cancel editing the selected field when clicked again', async(() => {
spyOn(registryService, 'getActiveMetadataField').and.returnValue(observableOf(mockFieldsList[2]));
spyOn(registryService, 'cancelEditMetadataField');
row.click();
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(registryService.cancelEditMetadataField).toHaveBeenCalled();
});
}));
});
}); });