Skip to content

Commit

Permalink
[web] Adapt product/ProductSelector to core/Selector
Browse files Browse the repository at this point in the history
  • Loading branch information
dgdavid committed Feb 10, 2024
1 parent 37dc119 commit 263616b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
10 changes: 5 additions & 5 deletions web/src/components/product/ProductPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,10 @@ describe("when the button for changing the product is clicked", () => {

const popup = await screen.findByRole("dialog");
within(popup).getByText("Choose a product");
within(popup).getByRole("radio", { name: "Test Product1" });
const radio = within(popup).getByRole("radio", { name: "Test Product2" });
within(popup).getByRole("row", { name: /Test Product1/ });
const productOption = within(popup).getByRole("row", { name: /Test Product2/ });

await user.click(radio);
await user.click(productOption);
const accept = within(popup).getByRole("button", { name: "Accept" });
await user.click(accept);

Expand All @@ -220,9 +220,9 @@ describe("when the button for changing the product is clicked", () => {
await user.click(button);

const popup = await screen.findByRole("dialog");
const radio = within(popup).getByRole("radio", { name: "Test Product2" });
const productOption = within(popup).getByRole("row", { name: /Test Product2/ });

await user.click(radio);
await user.click(productOption);
const cancel = within(popup).getByRole("button", { name: "Cancel" });
await user.click(cancel);

Expand Down
2 changes: 1 addition & 1 deletion web/src/components/product/ProductSelectionPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function ProductSelectionPage() {
// TRANSLATORS: page title
<Page icon="home_storage" title={_("Product selection")}>
<Form id="productSelectionForm" onSubmit={onSubmit}>
<FormGroup isStack label={_("Choose a product")} role="radiogroup">
<FormGroup isStack label={_("Choose a product")}>
<ProductSelector value={newProductId} products={products} onChange={setNewProductId} />
</FormGroup>
</Form>
Expand Down
32 changes: 16 additions & 16 deletions web/src/components/product/ProductSelector.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,30 @@
*/

import React from "react";
import { Card, CardBody, Radio } from "@patternfly/react-core";
import { Selector } from "~/components/core";

import { _ } from "~/i18n";
import { noop } from "~/utils";

export default function ProductSelector({ value, products = [], onChange = noop }) {
if (products.length === 0) return <p>{_("No products available for selection")}</p>;

const isSelected = (product) => product.id === value;
const onSelectionChange = (selection) => onChange(selection[0]);

return (
products.map((p) => (
<Card key={p.id} className={isSelected(p) && "selected-product"}>
<CardBody>
<Radio
id={p.id}
name="product"
label={p.name}
description={p.description}
isChecked={isSelected(p)}
onClick={() => onChange(p.id)}
/>
</CardBody>
</Card>
))
<Selector
aria-label={_("Available products")}
selectedIds={[value]}
onSelectionChange={onSelectionChange}
>
{ products.map(p => (
<Selector.Option id={p.id} key={p.id}>
<div className="stack">
<h3>{p.name}</h3>
<p>{p.description}</p>
</div>
</Selector.Option>
))}
</Selector>
);
}
14 changes: 8 additions & 6 deletions web/src/components/product/ProductSelector.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,23 @@ beforeEach(() => {

it("shows an option for each product", async () => {
installerRender(<ProductSelector products={products} />);
await screen.findByRole("radio", { name: "ALP Dolomite" });
await screen.findByRole("radio", { name: "openSUSE Tumbleweed" });
await screen.findByRole("radio", { name: "openSUSE MicroOS" });

await screen.findByRole("grid", { name: "Available products" });
screen.getByRole("row", { name: /ALP Dolomite/ });
screen.getByRole("row", { name: /openSUSE Tumbleweed/ });
screen.getByRole("row", { name: /openSUSE MicroOS/ });
});

it("selects the given value", async () => {
installerRender(<ProductSelector value="Tumbleweed" products={products} />);
await screen.findByRole("radio", { name: "openSUSE Tumbleweed", clicked: true });
await screen.findByRole("row", { name: /openSUSE Tumbleweed/, selected: true });
});

it("calls onChange if a new option is clicked", async () => {
const onChangeFn = jest.fn();
const { user } = installerRender(<ProductSelector products={products} onChange={onChangeFn} />);
const radio = await screen.findByRole("radio", { name: "openSUSE Tumbleweed" });
await user.click(radio);
const productOption = await screen.findByRole("row", { name: /openSUSE Tumbleweed/ });
await user.click(productOption);
expect(onChangeFn).toHaveBeenCalledWith("Tumbleweed");
});

Expand Down

0 comments on commit 263616b

Please sign in to comment.