mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-17 23:13:04 +00:00
58789: metadata-schema and field form-components tests
This commit is contained in:
@@ -1,16 +1,46 @@
|
|||||||
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
import { async, ComponentFixture, inject, TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
import { MetadataSchemaFormComponent } from './metadata-schema-form.component';
|
import { MetadataSchemaFormComponent } from './metadata-schema-form.component';
|
||||||
|
import { NO_ERRORS_SCHEMA } from '@angular/core';
|
||||||
|
import { CommonModule } from '@angular/common';
|
||||||
|
import { RouterTestingModule } from '@angular/router/testing';
|
||||||
|
import { TranslateModule } from '@ngx-translate/core';
|
||||||
|
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
|
||||||
|
import { EnumKeysPipe } from '../../../../shared/utils/enum-keys-pipe';
|
||||||
|
import { RegistryService } from '../../../../core/registry/registry.service';
|
||||||
|
import { FormBuilderService } from '../../../../shared/form/builder/form-builder.service';
|
||||||
|
import { of as observableOf } from 'rxjs/internal/observable/of';
|
||||||
|
import { MetadataSchema } from '../../../../core/metadata/metadataschema.model';
|
||||||
|
|
||||||
describe('MetadataSchemaFormComponent', () => {
|
describe('MetadataSchemaFormComponent', () => {
|
||||||
let component: MetadataSchemaFormComponent;
|
let component: MetadataSchemaFormComponent;
|
||||||
let fixture: ComponentFixture<MetadataSchemaFormComponent>;
|
let fixture: ComponentFixture<MetadataSchemaFormComponent>;
|
||||||
|
let registryService: RegistryService;
|
||||||
|
|
||||||
|
/* tslint:disable:no-empty */
|
||||||
|
const registryServiceStub = {
|
||||||
|
getActiveMetadataSchema: () => observableOf(undefined),
|
||||||
|
createOrUpdateMetadataSchema: (schema: MetadataSchema) => observableOf(schema)
|
||||||
|
};
|
||||||
|
const formBuilderServiceStub = {
|
||||||
|
createFormGroup: () => {
|
||||||
|
return {
|
||||||
|
patchValue: () => {}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
/* tslint:enable:no-empty */
|
||||||
|
|
||||||
beforeEach(async(() => {
|
beforeEach(async(() => {
|
||||||
TestBed.configureTestingModule({
|
TestBed.configureTestingModule({
|
||||||
declarations: [ MetadataSchemaFormComponent ]
|
imports: [CommonModule, RouterTestingModule.withRoutes([]), TranslateModule.forRoot(), NgbModule.forRoot()],
|
||||||
})
|
declarations: [ MetadataSchemaFormComponent, EnumKeysPipe ],
|
||||||
.compileComponents();
|
providers: [
|
||||||
|
{ provide: RegistryService, useValue: registryServiceStub },
|
||||||
|
{ provide: FormBuilderService, useValue: formBuilderServiceStub }
|
||||||
|
],
|
||||||
|
schemas: [NO_ERRORS_SCHEMA]
|
||||||
|
}).compileComponents();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@@ -19,7 +49,57 @@ describe('MetadataSchemaFormComponent', () => {
|
|||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create', () => {
|
beforeEach(inject([RegistryService], (s) => {
|
||||||
expect(component).toBeTruthy();
|
registryService = s;
|
||||||
|
}));
|
||||||
|
|
||||||
|
describe('when submitting the form', () => {
|
||||||
|
const namespace = 'fake namespace';
|
||||||
|
const prefix = 'fake';
|
||||||
|
|
||||||
|
const expected = Object.assign(new MetadataSchema(), {
|
||||||
|
namespace: namespace,
|
||||||
|
prefix: prefix
|
||||||
|
});
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
spyOn(component.submitForm, 'emit');
|
||||||
|
component.name.value = prefix;
|
||||||
|
component.namespace.value = namespace;
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('without an active schema', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
spyOn(registryService, 'getActiveMetadataSchema').and.returnValue(observableOf(undefined));
|
||||||
|
component.onSubmit();
|
||||||
|
fixture.detectChanges();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should emit a new schema using the correct values', async(() => {
|
||||||
|
fixture.whenStable().then(() => {
|
||||||
|
expect(component.submitForm.emit).toHaveBeenCalledWith(expected);
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('with an active schema', () => {
|
||||||
|
const expectedWithId = Object.assign(new MetadataSchema(), {
|
||||||
|
id: 1,
|
||||||
|
namespace: namespace,
|
||||||
|
prefix: prefix
|
||||||
|
});
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
spyOn(registryService, 'getActiveMetadataSchema').and.returnValue(observableOf(expectedWithId));
|
||||||
|
component.onSubmit();
|
||||||
|
fixture.detectChanges();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should edit the existing schema using the correct values', async(() => {
|
||||||
|
fixture.whenStable().then(() => {
|
||||||
|
expect(component.submitForm.emit).toHaveBeenCalledWith(expectedWithId);
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@@ -1,25 +1,119 @@
|
|||||||
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
import { async, ComponentFixture, inject, TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
import { MetadataFieldFormComponent } from './metadata-field-form.component';
|
import { MetadataFieldFormComponent } from './metadata-field-form.component';
|
||||||
|
import { RegistryService } from '../../../../core/registry/registry.service';
|
||||||
|
import { of as observableOf } from 'rxjs/internal/observable/of';
|
||||||
|
import { MetadataField } from '../../../../core/metadata/metadatafield.model';
|
||||||
|
import { CommonModule } from '@angular/common';
|
||||||
|
import { RouterTestingModule } from '@angular/router/testing';
|
||||||
|
import { TranslateModule } from '@ngx-translate/core';
|
||||||
|
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
|
||||||
|
import { EnumKeysPipe } from '../../../../shared/utils/enum-keys-pipe';
|
||||||
|
import { FormBuilderService } from '../../../../shared/form/builder/form-builder.service';
|
||||||
|
import { NO_ERRORS_SCHEMA } from '@angular/core';
|
||||||
|
import { MetadataSchema } from '../../../../core/metadata/metadataschema.model';
|
||||||
|
|
||||||
describe('MetadataFieldFormComponent', () => {
|
describe('MetadataFieldFormComponent', () => {
|
||||||
let component: MetadataFieldFormComponent;
|
let component: MetadataFieldFormComponent;
|
||||||
let fixture: ComponentFixture<MetadataFieldFormComponent>;
|
let fixture: ComponentFixture<MetadataFieldFormComponent>;
|
||||||
|
let registryService: RegistryService;
|
||||||
|
|
||||||
|
const metadataSchema = Object.assign(new MetadataSchema(), {
|
||||||
|
id: 1,
|
||||||
|
namespace: 'fake schema',
|
||||||
|
prefix: 'fake'
|
||||||
|
});
|
||||||
|
|
||||||
|
/* tslint:disable:no-empty */
|
||||||
|
const registryServiceStub = {
|
||||||
|
getActiveMetadataField: () => observableOf(undefined),
|
||||||
|
createOrUpdateMetadataField: (field: MetadataField) => observableOf(field)
|
||||||
|
};
|
||||||
|
const formBuilderServiceStub = {
|
||||||
|
createFormGroup: () => {
|
||||||
|
return {
|
||||||
|
patchValue: () => {}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
/* tslint:enable:no-empty */
|
||||||
|
|
||||||
beforeEach(async(() => {
|
beforeEach(async(() => {
|
||||||
TestBed.configureTestingModule({
|
TestBed.configureTestingModule({
|
||||||
declarations: [ MetadataFieldFormComponent ]
|
imports: [CommonModule, RouterTestingModule.withRoutes([]), TranslateModule.forRoot(), NgbModule.forRoot()],
|
||||||
})
|
declarations: [ MetadataFieldFormComponent, EnumKeysPipe ],
|
||||||
.compileComponents();
|
providers: [
|
||||||
|
{ provide: RegistryService, useValue: registryServiceStub },
|
||||||
|
{ provide: FormBuilderService, useValue: formBuilderServiceStub }
|
||||||
|
],
|
||||||
|
schemas: [NO_ERRORS_SCHEMA]
|
||||||
|
}).compileComponents();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
fixture = TestBed.createComponent(MetadataFieldFormComponent);
|
fixture = TestBed.createComponent(MetadataFieldFormComponent);
|
||||||
component = fixture.componentInstance;
|
component = fixture.componentInstance;
|
||||||
|
component.metadataSchema = metadataSchema;
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create', () => {
|
beforeEach(inject([RegistryService], (s) => {
|
||||||
expect(component).toBeTruthy();
|
registryService = s;
|
||||||
|
}));
|
||||||
|
|
||||||
|
describe('when submitting the form', () => {
|
||||||
|
const element = 'fakeElement';
|
||||||
|
const qualifier = 'fakeQualifier';
|
||||||
|
const scopeNote = 'fakeScopeNote';
|
||||||
|
|
||||||
|
const expected = Object.assign(new MetadataField(), {
|
||||||
|
schema: metadataSchema,
|
||||||
|
element: element,
|
||||||
|
qualifier: qualifier,
|
||||||
|
scopeNote: scopeNote
|
||||||
|
});
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
spyOn(component.submitForm, 'emit');
|
||||||
|
component.element.value = element;
|
||||||
|
component.qualifier.value = qualifier;
|
||||||
|
component.scopeNote.value = scopeNote;
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('without an active field', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
spyOn(registryService, 'getActiveMetadataField').and.returnValue(observableOf(undefined));
|
||||||
|
component.onSubmit();
|
||||||
|
fixture.detectChanges();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should emit a new field using the correct values', async(() => {
|
||||||
|
fixture.whenStable().then(() => {
|
||||||
|
expect(component.submitForm.emit).toHaveBeenCalledWith(expected);
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('with an active field', () => {
|
||||||
|
const expectedWithId = Object.assign(new MetadataField(), {
|
||||||
|
id: 1,
|
||||||
|
schema: metadataSchema,
|
||||||
|
element: element,
|
||||||
|
qualifier: qualifier,
|
||||||
|
scopeNote: scopeNote
|
||||||
|
});
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
spyOn(registryService, 'getActiveMetadataField').and.returnValue(observableOf(expectedWithId));
|
||||||
|
component.onSubmit();
|
||||||
|
fixture.detectChanges();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should edit the existing field using the correct values', async(() => {
|
||||||
|
fixture.whenStable().then(() => {
|
||||||
|
expect(component.submitForm.emit).toHaveBeenCalledWith(expectedWithId);
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user