Skip to content

Commit

Permalink
馃 backported "Trim clipboard data in filter widgets" (#40720)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexander Polyankin <alexander.polyankin@metabase.com>
  • Loading branch information
metabase-bot[bot] and ranquild committed Mar 28, 2024
1 parent 36189f1 commit 8fb857b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "__support__/server-mocks";
import {
act,
createMockClipboardData,
renderWithProviders,
screen,
waitForLoaderToBeRemoved,
Expand Down Expand Up @@ -539,6 +540,23 @@ describe("StringFilterValuePicker", () => {
expect(screen.queryByText("a@b")).not.toBeInTheDocument();
expect(onChange).toHaveBeenLastCalledWith(["a@b.com", "a@b"]);
});

it("should trim clipboard data", async () => {
const { onChange } = await setupStringPicker({
query,
stageIndex,
column,
values: [],
});

const clipboardData = createMockClipboardData({
getData: () => " abc\r\ndef",
});
userEvent.paste(screen.getByLabelText("Filter value"), "", {
clipboardData,
});
expect(onChange).toHaveBeenLastCalledWith(["abc", "def"]);
});
});

describe("no values", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,9 @@ export function MultiAutocomplete({
const text = event.clipboardData.getData("Text");
const values = text.split(/[\n,]/g);
if (values.length > 1) {
const uniqueValues = [...new Set(values)];
const validValues = uniqueValues.filter(value =>
shouldCreate?.(value, []),
);
const validValues = [...new Set(values)]
.map(value => value.trim())
.filter(value => shouldCreate?.(value, []));
if (validValues.length > 0) {
event.preventDefault();
const newSelectedValues = [...lastSelectedValues, ...validValues];
Expand Down
39 changes: 39 additions & 0 deletions frontend/test/__support__/ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,43 @@ export const waitForLoaderToBeRemoved = async () => {
});
};

/**
* jsdom doesn't have getBoundingClientRect, so we need to mock it
*/
export const mockGetBoundingClientRect = (options: Partial<DOMRect> = {}) => {
jest
.spyOn(window.Element.prototype, "getBoundingClientRect")
.mockImplementation(() => {
return {
height: 200,
width: 200,
top: 0,
left: 0,
bottom: 0,
right: 0,
x: 0,
y: 0,
toJSON: () => {},
...options,
};
});
};

/**
* jsdom doesn't have scrollBy, so we need to mock it
*/
export const mockScrollBy = () => {
window.Element.prototype.scrollBy = jest.fn();
};

/**
* jsdom doesn't have DataTransfer
*/
export function createMockClipboardData(
opts?: Partial<DataTransfer>,
): DataTransfer {
const clipboardData = { ...opts };
return clipboardData as unknown as DataTransfer;
}

export * from "@testing-library/react";

0 comments on commit 8fb857b

Please sign in to comment.