Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { Step, StepItem, StepPanel } from 'primeng/stepper';

import { ComponentFixture, TestBed } from '@angular/core/testing';

import { CedarMetadataRecordModel, CedarMetadataTemplateModel } from '@osf/features/metadata/models';
import {
CedarEditorElement,
CedarMetadataRecordModel,
CedarMetadataTemplateModel,
} from '@osf/features/metadata/models';

import { MOCK_CEDAR_RECORD } from '@testing/data/collections/cedar-metadata.mock';
import { MOCK_CEDAR_METADATA_TEMPLATE } from '@testing/mocks/cedar-metadata-domain.mock';
Expand All @@ -26,6 +30,7 @@ describe('CollectionMetadataStepComponent', () => {
stepperActiveValue?: number;
targetStepValue?: number;
isDisabled?: boolean;
editor?: CedarEditorElement;
} = {}
) {
TestBed.configureTestingModule({
Expand All @@ -36,6 +41,10 @@ describe('CollectionMetadataStepComponent', () => {
fixture = TestBed.createComponent(CollectionMetadataStepComponent);
component = fixture.componentInstance;

if (options.editor) {
vi.spyOn(component, 'cedarEditor').mockReturnValue({ nativeElement: options.editor } as never);
}

fixture.componentRef.setInput('stepperActiveValue', options.stepperActiveValue ?? 0);
fixture.componentRef.setInput('targetStepValue', options.targetStepValue ?? 1);
fixture.componentRef.setInput('isDisabled', options.isDisabled ?? false);
Expand Down Expand Up @@ -121,6 +130,50 @@ describe('CollectionMetadataStepComponent', () => {
expect(component.cedarFormData()).toEqual({ field: 'value' });
});

it('should load template and existing record metadata together when step opens', () => {
const editor = {} as CedarEditorElement;
setup({
cedarTemplate: mockCedarTemplate,
existingCedarRecord: MOCK_CEDAR_RECORD,
stepperActiveValue: 1,
editor,
});

expect(editor.templateAndInstanceObject).toEqual({
templateObject: mockCedarTemplate.template,
instanceObject: { field: 'value' },
});
expect(editor.instanceObject).toBeUndefined();
});

it('should load saved metadata together with template when step is reopened', () => {
const editor = {} as CedarEditorElement;
setup({ cedarTemplate: mockCedarTemplate, editor });

component.cedarFormData.set({ field: 'saved' });
component.collectionMetadataSaved.set(true);
fixture.componentRef.setInput('stepperActiveValue', 1);
fixture.detectChanges();

expect(editor.templateAndInstanceObject).toEqual({
templateObject: mockCedarTemplate.template,
instanceObject: { field: 'saved' },
});
});

it('should load existing record metadata together with template on discard', () => {
const editor = {} as CedarEditorElement;
setup({ cedarTemplate: mockCedarTemplate, existingCedarRecord: MOCK_CEDAR_RECORD, editor });

component.cedarFormData.set({ field: 'edited' });
component.handleDiscardChanges();

expect(editor.templateAndInstanceObject).toEqual({
templateObject: mockCedarTemplate.template,
instanceObject: { field: 'value' },
});
});

it('should populate cedarFormData from existingCedarRecord', () => {
setup({
cedarTemplate: mockCedarTemplate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class CollectionMetadataStepComponent {
handleDiscardChanges() {
const record = this.existingCedarRecord();
this.cedarFormData.set(record?.metadata ? (record.metadata as Record<string, unknown>) : {});
this.syncCedarInstance(this.cedarEditor()?.nativeElement);
this.syncCedarEditor();
this.collectionMetadataSaved.set(false);
}

Expand Down Expand Up @@ -124,7 +124,7 @@ export class CollectionMetadataStepComponent {

if (!record?.metadata && !saved) return;

this.syncCedarInstance(this.cedarEditor()?.nativeElement);
this.syncCedarEditor();
});

effect(() => {
Expand All @@ -133,6 +133,17 @@ export class CollectionMetadataStepComponent {
});
}

private syncCedarEditor(): void {
const editor = this.cedarEditor()?.nativeElement;
const template = this.cedarTemplate()?.template;
if (editor && template) {
editor.templateAndInstanceObject = {
templateObject: template,
instanceObject: untracked(() => this.cedarFormData()),
};
}
}

private syncCedarInstance(element: CedarEditorElement | undefined): void {
if (element) {
element.instanceObject = untracked(() => this.cedarFormData());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ActivatedRoute } from '@angular/router';

import { CedarMetadataHelper } from '@osf/features/metadata/helpers';
import { CedarEditorElement } from '@osf/features/metadata/models';

import { MOCK_CEDAR_METADATA_RECORD, MOCK_CEDAR_METADATA_TEMPLATE } from '@testing/mocks/cedar-metadata-domain.mock';
import { provideOSFCore } from '@testing/osf.testing.provider';
Expand Down Expand Up @@ -102,4 +103,45 @@ describe('CedarTemplateFormComponent', () => {

expect(component.formData()).toEqual(initialFormData);
});

it('should load template and record metadata together into the editor', () => {
const editor = {} as CedarEditorElement;
vi.spyOn(component, 'cedarEditor').mockReturnValue({ nativeElement: editor } as never);

fixture.componentRef.setInput('existingRecord', mockRecord);
fixture.detectChanges();

expect(editor.templateAndInstanceObject).toEqual({
templateObject: mockTemplate.template,
instanceObject: mockRecord.metadata,
});
expect(editor.instanceObject).toBeUndefined();
});

it('should publish on submit when the editor report is valid', () => {
const editor = {
currentMetadata: { field: 'value' },
dataQualityReport: { isValid: true },
} as unknown as CedarEditorElement;
vi.spyOn(component, 'cedarEditor').mockReturnValue({ nativeElement: editor } as never);
const emitSpy = vi.spyOn(component.emitData, 'emit');
component.isValid = false;

component.onSubmit();

expect(emitSpy).toHaveBeenCalledWith({ data: { field: 'value' }, id: mockTemplate.id, isPublished: true });
});

it('should save as draft on submit when the editor report is not valid', () => {
const editor = {
currentMetadata: { field: 'value' },
dataQualityReport: { isValid: false },
} as unknown as CedarEditorElement;
vi.spyOn(component, 'cedarEditor').mockReturnValue({ nativeElement: editor } as never);
const emitSpy = vi.spyOn(component.emitData, 'emit');

component.onSubmit();

expect(emitSpy).toHaveBeenCalledWith({ data: { field: 'value' }, id: mockTemplate.id, isPublished: false });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ export class CedarTemplateFormComponent {
this.initializeFormData();

if (metadata) {
if (editor) editor.instanceObject = metadata;
const template = this.template()?.template;
if (editor && template) editor.templateAndInstanceObject = { templateObject: template, instanceObject: metadata };
if (viewer) viewer.instanceObject = metadata;
}

Expand Down Expand Up @@ -182,6 +183,7 @@ export class CedarTemplateFormComponent {
onSubmit() {
const editor = this.cedarEditor()?.nativeElement;
if (editor && typeof editor.currentMetadata !== 'undefined') {
this.validateCedarMetadata();
const finalData = { data: editor.currentMetadata, id: this.template().id, isPublished: this.isValid };
this.formData.set(finalData);
this.emitData.emit(finalData as CedarRecordDataBinding);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export interface CedarEditorElement extends HTMLElement {
currentMetadata?: unknown;
instanceObject?: unknown;
templateAndInstanceObject?: {
templateObject: unknown;
instanceObject: unknown;
};
dataQualityReport?: {
isValid: boolean;
};
Expand Down
1 change: 1 addition & 0 deletions src/assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1827,6 +1827,7 @@
"publishedText": "This metadata has a status of 'Published' and is publicly viewable.",
"recordCreatedSuccessfully": "Record created successfully",
"recordUpdatedSuccessfully": "Record updated successfully",
"saveDraft": "Save draft",
"select": "Select",
"selectTemplate": "Select a Metadata Template",
"title": "Add community metadata record",
Expand Down
Loading