Skip to content

Commit

Permalink
fix: search and filter panel no longer opens when dismissing a filter (
Browse files Browse the repository at this point in the history
  • Loading branch information
jmuzina authored Sep 12, 2024
1 parent c971b10 commit c4804ea
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/components/Chip/Chip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export type Props = PropsWithSpread<
/**
* Function for handling dismissing a chip.
*/
onDismiss?: () => void;
onDismiss?: (event: MouseEvent<HTMLButtonElement>) => void;
/**
* Whether the chip is selected.
*/
Expand Down
38 changes: 37 additions & 1 deletion src/components/SearchAndFilter/SearchAndFilter.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, screen, waitFor } from "@testing-library/react";
import { render, screen, waitFor, within } from "@testing-library/react";
import React from "react";

import SearchAndFilter from "./SearchAndFilter";
Expand Down Expand Up @@ -413,4 +413,40 @@ describe("Search and filter", () => {
});
expect(onExpandChange).toHaveBeenCalled();
});

it("does not toggle the panel when a filter is dismissed", async () => {
const returnSearchData = jest.fn();
const onExpandChange = jest.fn();
const onPanelToggle = jest.fn();
render(
<SearchAndFilter
filterPanelData={sampleData}
returnSearchData={returnSearchData}
onExpandChange={onExpandChange}
onPanelToggle={onPanelToggle}
existingSearchData={[
{ lead: "Cloud", value: "Google" },
{ lead: "Region", value: "eu-west-1" },
]}
/>,
);

// onPanelToggle is called on initial render, so we need to clear the mock before asserting
onPanelToggle.mockClear();

// Dismiss the Cloud: Google filter chip
await waitFor(async () => {
const cloudChip: HTMLElement = screen
.getByText("CLOUD")
.closest(".p-chip");

const dismissButton = within(cloudChip).getByRole("button", {
name: "Dismiss",
});

await userEvent.click(dismissButton);
});

expect(onPanelToggle).not.toHaveBeenCalled();
});
});
6 changes: 5 additions & 1 deletion src/components/SearchAndFilter/SearchAndFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,11 @@ const SearchAndFilter = ({
lead={chip.lead}
value={chip.value}
key={`search-${chip.lead}+${chip.value}`}
onDismiss={() => removeFromSelected(chip)}
onDismiss={(event) => {
// Prevent filter chip dismissals from bubbling up and triggering the parent onClick handler
event.stopPropagation();
removeFromSelected(chip);
}}
selected={true}
quoteValue={chip.quoteValue}
/>
Expand Down

0 comments on commit c4804ea

Please sign in to comment.