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: Leaving editable tab in error state #37981

Merged
merged 2 commits into from
Dec 6, 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 @@ -151,7 +151,7 @@ describe("EditableName", () => {

await userEvent.click(document.body);

expect(getByRole("tooltip").textContent).toEqual(validationError);
expect(getByRole("tooltip").textContent).toEqual("");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add tests for missing scenarios

While the current changes correctly verify the tooltip behavior, consider adding tests for:

  1. The case where the name hasn't changed (should call exitWithoutSaving)
  2. The transition timing of error states to ensure proper cleanup

Example test structure:

test("exits without saving when name hasn't changed", () => {
  const { exitEditing, getByRole, onNameSave } = setup({
    isEditing: true,
  });
  const inputElement = getByRole("textbox");

  // Set same name as original
  fireEvent.change(inputElement, {
    target: { value: name },
  });

  fireEvent.keyUp(inputElement, KEY_CONFIG.ENTER);

  expect(exitEditing).toHaveBeenCalled();
  expect(onNameSave).not.toHaveBeenCalled();
});

Also applies to: 190-190


expect(exitEditing).toHaveBeenCalled();
expect(onNameSave).not.toHaveBeenCalledWith(invalidTitle);
Expand Down Expand Up @@ -187,7 +187,7 @@ describe("EditableName", () => {
});

fireEvent.focusOut(inputElement);
expect(getByRole("tooltip").textContent).toEqual(validationError);
expect(getByRole("tooltip").textContent).toEqual("");
expect(exitEditing).toHaveBeenCalled();
expect(onNameSave).not.toHaveBeenCalledWith(invalidTitle);
});
Expand Down
16 changes: 14 additions & 2 deletions app/client/src/IDE/Components/EditableName/EditableName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import React, {
useRef,
useState,
} from "react";
import { Spinner, Text, Tooltip } from "@appsmith/ads";
import { Spinner, Text as ADSText, Tooltip } from "@appsmith/ads";
import { useEventCallback, useEventListener } from "usehooks-ts";
import { usePrevious } from "@mantine/hooks";
import { useNameEditor } from "./useNameEditor";
import styled from "styled-components";

interface EditableTextProps {
name: string;
Expand All @@ -29,6 +30,10 @@ interface EditableTextProps {
inputTestId?: string;
}

export const Text = styled(ADSText)`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd move this to styles.

min-width: 3ch;
`;

export const EditableName = ({
exitEditing,
icon,
Expand Down Expand Up @@ -72,10 +77,15 @@ export const EditableName = ({
const nameError = validate(editableName);

if (editableName === name) {
// No change detected
exitWithoutSaving();
} else if (nameError === null) {
// Save the new name
exitEditing();
onNameSave(editableName);
} else {
// Exit edit mode and revert name
exitWithoutSaving();
}
}, [
editableName,
Expand Down Expand Up @@ -119,7 +129,9 @@ export const EditableName = ({
useEventListener(
"focusout",
function handleFocusOut() {
if (isEditing) {
const input = inputRef.current;

if (input) {
attemptSave();
}
},
Expand Down
Loading