Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: if a data model field binding is removed, delete the prop #14297

Merged
merged 6 commits into from
Dec 18, 2024
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 @@ -272,7 +272,7 @@ describe('EditBinding without featureFlag', () => {
);
});

it('should call handleComponentChange when click on delete button', async () => {
it('should set the data model binding to the default value when click on delete button', async () => {
window.confirm = jest.fn(() => true);
const user = userEvent.setup();
const handleComponentChange = jest.fn();
Expand Down Expand Up @@ -312,6 +312,37 @@ describe('EditBinding without featureFlag', () => {
},
);
});

it('should delete the data model binding when click on delete button and no default value is defined', async () => {
window.confirm = jest.fn(() => true);
const user = userEvent.setup();
const handleComponentChange = jest.fn();
renderEditBinding({
editBindingProps: {
...defaultEditBinding,
component: componentMocks[ComponentType.FileUpload],
handleComponentChange,
},
queries: {
getAppMetadataModelIds: getAppMetadataModelIdsMock,
getDataModelMetadata: getDataModelMetadataMock,
},
});

await waitForElementToBeRemoved(() =>
screen.queryByTitle(textMock('ux_editor.modal_properties_loading')),
);

const deleteButton = screen.getByRole('button', {
name: textMock('general.delete'),
});
await user.click(deleteButton);

expect(handleComponentChange).toHaveBeenCalledTimes(1);
expect(handleComponentChange).toHaveBeenCalledWith(componentMocks[ComponentType.FileUpload], {
onSuccess: expect.any(Function),
});
});
});

describe('EditBinding with featureFlag', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { EditBindingButtons } from './EditBindingButtons';
import { useValidDataModels } from '@altinn/ux-editor/hooks/useValidDataModels';
import { StudioSpinner } from '@studio/components';
import { useTranslation } from 'react-i18next';
import { formItemConfigs } from '@altinn/ux-editor/data/formItemConfig';

export type EditBindingProps = {
bindingKey: string;
Expand All @@ -43,17 +44,26 @@ export const EditBinding = ({
internalBindingFormat.dataType,
);

const handleBindingChange = (updatedBinding: InternalBindingFormat) => {
const selectedDataFieldElement = updatedBinding.field;
const handleBindingChange = (updatedBinding?: InternalBindingFormat) => {
const selectedDataFieldElement = updatedBinding?.field;

const value =
(shouldDisplayFeature(FeatureFlag.MultipleDataModelsPerTask)
? updatedBinding
: selectedDataFieldElement) ??
formItemConfigs[component.type]?.defaultProperties?.['dataModelBindings']?.[bindingKey];

const dataModelBindings = { ...component.dataModelBindings };
if (value === undefined || value === null) {
delete dataModelBindings[bindingKey];
} else {
dataModelBindings[bindingKey] = value;
}

handleComponentChange(
{
...component,
dataModelBindings: {
...component.dataModelBindings,
[bindingKey]: shouldDisplayFeature(FeatureFlag.MultipleDataModelsPerTask)
? updatedBinding
: selectedDataFieldElement,
},
dataModelBindings: Object.keys(dataModelBindings).length ? dataModelBindings : undefined,
required: getMinOccursFromDataModelFields(selectedDataFieldElement, dataModelMetadata),
timeStamp: getXsdDataTypeFromDataModelFields(
component.type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ export const EditBindingButtons = ({
const { t } = useTranslation();

const handleDelete = () => {
const updatedDataModelBinding = {
field: '',
dataType: '',
};
handleBindingChange(updatedDataModelBinding);
handleBindingChange(undefined);
onSetDataModelSelectVisible(false);
};

Expand Down
4 changes: 2 additions & 2 deletions frontend/packages/ux-editor/src/testing/componentMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const subformComponent: FormComponent<ComponentType.Subform> = {
};
const fileUploadComponent: FormComponent<ComponentType.FileUpload> = {
...commonProps(ComponentType.FileUpload),
dataModelBindings: { simpleBinding: '' },
dataModelBindings: undefined,
description: 'test',
displayMode: 'list',
hasCustomFileEndings: false,
Expand All @@ -107,7 +107,7 @@ const fileUploadComponent: FormComponent<ComponentType.FileUpload> = {
};
const fileUploadWithTagComponent: FormComponent<ComponentType.FileUploadWithTag> = {
...commonProps(ComponentType.FileUploadWithTag),
dataModelBindings: { simpleBinding: '' },
dataModelBindings: undefined,
description: 'test',
displayMode: 'list',
hasCustomFileEndings: false,
Expand Down
Loading