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: updating dashboard - initial save button state is "disabled" #196137

Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@ export const ContentEditorFlyoutContent: FC<Props> = ({
const i18nTexts = useMemo(() => getI18nTexts({ entityName }), [entityName]);
const form = useMetadataForm({ item, customValidators });

const hasNoChanges = () => {
const itemTags = item.tags.map((obj) => obj.id).sort();
const formTags = form.tags.value.slice().sort();

const compareTags = (arr1: string[], arr2: string[]) => {
if (arr1.length !== arr2.length) return false;
return arr1.every((tag: string, index) => tag === arr2[index]);
};

return (
item.title === form.title.value &&
item.description === form.description.value &&
compareTags(itemTags, formTags)
);
};

const onClickSave = useCallback(async () => {
if (form.isValid && onSave && !form.getIsChangingValue()) {
const id = item.id;
Expand Down Expand Up @@ -177,7 +193,7 @@ export const ContentEditorFlyoutContent: FC<Props> = ({
onClick={onClickSave}
data-test-subj="saveButton"
fill
disabled={isSubmitted && !form.isValid}
paulinashakirova marked this conversation as resolved.
Show resolved Hide resolved
disabled={(isSubmitted && !form.isValid) || hasNoChanges()}
isLoading={isSubmitting}
>
{i18nTexts.saveButtonLabel}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,32 +104,37 @@ describe('<ContentEditorFlyoutContent />', () => {
expect(find('saveButton').text()).toBe('Update foo');
});

test('should send back the updated item to the onSave() handler', async () => {
test('should save form only if something changes', async () => {
const onSave = jest.fn();

await act(async () => {
testBed = await setup({ onSave, isReadonly: false });
});

const {
find,
component,
form: { setInputValue },
} = testBed!;

await waitForValidationResults();
const { find, component } = testBed!;

await act(async () => {
find('saveButton').simulate('click');
});

expect(onSave).toHaveBeenCalledWith({
paulinashakirova marked this conversation as resolved.
Show resolved Hide resolved
id: '123',
title: 'Foo',
description: 'Some description',
tags: ['id-1', 'id-2'],
component.update();

expect(onSave).not.toHaveBeenCalled();
});

test('should send back the updated item to the onSave() handler', async () => {
const onSave = jest.fn();

await act(async () => {
testBed = await setup({ onSave, isReadonly: false });
});

const {
find,
component,
form: { setInputValue },
} = testBed!;

await act(async () => {
setInputValue('metadataForm.nameInput', 'newTitle');
setInputValue('metadataForm.descriptionInput', 'newDescription');
Expand Down Expand Up @@ -196,7 +201,17 @@ describe('<ContentEditorFlyoutContent />', () => {
testBed = await setup({ onSave, isReadonly: false, services: { notifyError } });
});

const { find, component } = testBed!;
const {
find,
component,
form: { setInputValue },
} = testBed!;

await act(async () => {
setInputValue('metadataForm.nameInput', 'changingTitleToUnblockDisabledButtonState');
});

await waitForValidationResults();

component.update();

Expand Down