-
Notifications
You must be signed in to change notification settings - Fork 4.5k
fix:- added elipsis and tooltip to the button content in table widget component #36865
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
Merged
yatinappsmith
merged 8 commits into
appsmithorg:release
from
skjameela:fix/button-tooltip-in-table_widgetV2
Oct 30, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9b27dfc
fix:- added elipsis and tooltip to the button content in table widget…
skjameela 48d92df
resolved review comments
skjameela 92a26c9
resolved review comments for button tooltip
skjameela 8ffde6e
resolved the prettier and linting issues
skjameela 8f9d8d8
resolved review comments and added test cases
skjameela 4798350
removed duplicate test cases
skjameela fa303f9
Resolved merge conflict in AutoTooltipComponent.test.tsx
skjameela 36bfa53
resolved linting and prettier issues
skjameela File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
app/client/src/widgets/TableWidgetV2/component/cellComponents/AutoTooltipComponent.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| import React from "react"; | ||
| import { fireEvent, render, screen } from "@testing-library/react"; | ||
| import AutoToolTipComponent from "./AutoToolTipComponent"; | ||
| import { ColumnTypes } from "widgets/TableWidgetV2/constants"; | ||
| import "@testing-library/jest-dom"; | ||
| import { isButtonTextTruncated } from "./AutoToolTipComponent"; | ||
|
|
||
| jest.mock("react", () => { | ||
| const actualReact = jest.requireActual("react"); | ||
|
|
||
| return { | ||
| ...actualReact, | ||
| useState: jest.fn((initial) => [initial, jest.fn()]), | ||
| }; | ||
| }); | ||
|
|
||
| test.each([ | ||
| ["truncated text", "This is a long text that will be truncated"], | ||
| [ | ||
| "truncated button text", | ||
| "This is a long text that will be truncated in the button", | ||
| ], | ||
| ])("shows tooltip for %s", (_, longText) => { | ||
| const { getByText } = render( | ||
| <AutoToolTipComponent columnType={ColumnTypes.BUTTON} title={longText}> | ||
| <span | ||
| style={{ | ||
| width: "50px", | ||
| display: "inline-block", | ||
| overflow: "hidden", | ||
| textOverflow: "ellipsis", | ||
| whiteSpace: "nowrap", | ||
| }} | ||
| > | ||
| {longText} | ||
| </span> | ||
| </AutoToolTipComponent>, | ||
| ); | ||
|
|
||
| fireEvent.mouseEnter(getByText(longText)); | ||
| expect(getByText(longText)).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| test("does not show tooltip for non-button types", () => { | ||
| const { getByText } = render( | ||
| <AutoToolTipComponent columnType={ColumnTypes.URL} title="Not a button"> | ||
| <a href="#">Not a button</a> | ||
| </AutoToolTipComponent>, | ||
| ); | ||
|
|
||
| expect(getByText("Not a button")).toBeInTheDocument(); | ||
| expect(screen.queryByRole("dialog")).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| test("handles empty tooltip", () => { | ||
| const { getByText } = render( | ||
| <AutoToolTipComponent columnType={ColumnTypes.BUTTON} title=""> | ||
| <button>Empty button</button> | ||
| </AutoToolTipComponent>, | ||
| ); | ||
|
|
||
| expect(getByText("Empty button")).toBeInTheDocument(); | ||
| expect(screen.queryByRole("dialog")).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| test("renders content without tooltip for normal text", () => { | ||
| const { getByText } = render( | ||
| <AutoToolTipComponent title="Normal Text"> | ||
| <span>Normal Text</span> | ||
| </AutoToolTipComponent>, | ||
| ); | ||
|
|
||
| expect(getByText("Normal Text")).toBeInTheDocument(); | ||
| expect(screen.queryByRole("dialog")).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| test("does not show tooltip for non-truncated text", () => { | ||
| const shortText = "Short text"; | ||
| const { getByText } = render( | ||
| <AutoToolTipComponent columnType={ColumnTypes.BUTTON} title={shortText}> | ||
| <span>{shortText}</span> | ||
| </AutoToolTipComponent>, | ||
| ); | ||
|
|
||
| fireEvent.mouseEnter(getByText(shortText)); | ||
| expect(screen.queryByRole("dialog")).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| test("opens a new tab for URL column type when clicked", () => { | ||
| const openSpy = jest.spyOn(window, "open").mockImplementation(() => null); | ||
|
|
||
| render( | ||
| <AutoToolTipComponent | ||
| columnType={ColumnTypes.URL} | ||
| title="Go to Google" | ||
| url="https://www.google.com" | ||
| > | ||
| <span>Go to Google</span> | ||
| </AutoToolTipComponent>, | ||
| ); | ||
|
|
||
| fireEvent.click(screen.getByText("Go to Google")); | ||
| expect(openSpy).toHaveBeenCalledWith("https://www.google.com", "_blank"); | ||
|
|
||
| openSpy.mockRestore(); | ||
| }); | ||
|
|
||
| describe("isButtonTextTruncated", () => { | ||
| function mockElementWidths( | ||
| offsetWidth: number, | ||
| scrollWidth: number, | ||
| ): HTMLElement { | ||
| const spanElement = document.createElement("span"); | ||
|
|
||
| Object.defineProperty(spanElement, "offsetWidth", { value: offsetWidth }); | ||
| Object.defineProperty(spanElement, "scrollWidth", { value: scrollWidth }); | ||
| const container = document.createElement("div"); | ||
|
|
||
| container.appendChild(spanElement); | ||
|
|
||
| return container; | ||
| } | ||
|
|
||
| test("returns true when text is truncated (scrollWidth > offsetWidth)", () => { | ||
| const element = mockElementWidths(100, 150); | ||
|
|
||
| expect(isButtonTextTruncated(element)).toBe(true); | ||
| }); | ||
|
|
||
| test("returns false when text is not truncated (scrollWidth <= offsetWidth)", () => { | ||
| const element = mockElementWidths(150, 150); | ||
|
|
||
| expect(isButtonTextTruncated(element)).toBe(false); | ||
| }); | ||
|
|
||
| test("returns false when no span element is found", () => { | ||
| const element = document.createElement("div"); | ||
|
|
||
| expect(isButtonTextTruncated(element)).toBe(false); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Well done on testing the utility function! Here's some extra credit work 🌟
Your tests for
isButtonTextTruncatedshow excellent attention to detail. For extra points, consider adding these additional test cases: