Skip to content
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
6 changes: 6 additions & 0 deletions web/package/agama-web-ui.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
-------------------------------------------------------------------
Mon Jan 13 11:11:49 UTC 2025 - David Diaz <dgonzalez@suse.com>

- Do not allow changing selected product after registering one
(related to gh#agama-project/agama#1891).

-------------------------------------------------------------------
Fri Jan 10 21:22:02 UTC 2025 - Imobach Gonzalez Sosa <igonzalezsosa@suse.com>

Expand Down
16 changes: 15 additions & 1 deletion web/src/components/core/ChangeProductLink.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ import React from "react";
import { screen } from "@testing-library/react";
import { installerRender } from "~/test-utils";
import { PRODUCT as PATHS } from "~/routes/paths";
import { Product } from "~/types/software";
import { Product, RegistrationInfo } from "~/types/software";
import ChangeProductLink from "./ChangeProductLink";
import { useRegistration } from "~/queries/software";

const tumbleweed: Product = {
id: "Tumbleweed",
Expand All @@ -43,9 +44,11 @@ const microos: Product = {
};

let mockUseProduct: { products: Product[]; selectedProduct?: Product };
let registrationInfoMock: RegistrationInfo;

jest.mock("~/queries/software", () => ({
useProduct: () => mockUseProduct,
useRegistration: (): ReturnType<typeof useRegistration> => registrationInfoMock,
}));

describe("ChangeProductLink", () => {
Expand All @@ -59,6 +62,17 @@ describe("ChangeProductLink", () => {
const link = screen.getByRole("link", { name: "Change product" });
expect(link).toHaveAttribute("href", PATHS.changeProduct);
});

describe("but a product is registered", () => {
beforeEach(() => {
registrationInfoMock = { key: "INTERNAL-USE-ONLY-1234-5678", email: "" };
});

it("renders nothing", () => {
const { container } = installerRender(<ChangeProductLink />);
expect(container).toBeEmptyDOMElement();
});
});
});

describe("when there is only one product available", () => {
Expand Down
5 changes: 4 additions & 1 deletion web/src/components/core/ChangeProductLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@

import React from "react";
import { Link, LinkProps } from "react-router-dom";
import { useProduct } from "~/queries/software";
import { useProduct, useRegistration } from "~/queries/software";
import { PRODUCT as PATHS } from "~/routes/paths";
import { _ } from "~/i18n";
import { isEmpty } from "~/utils";

/**
* Link for navigating to the selection product.
*/
export default function ChangeProductLink({ children, ...props }: Omit<LinkProps, "to">) {
const { products } = useProduct();
const registration = useRegistration();

if (products.length <= 1) return null;
if (!isEmpty(registration?.key)) return null;

return (
<Link to={PATHS.changeProduct} {...props}>
Expand Down
18 changes: 16 additions & 2 deletions web/src/components/product/ProductSelectionPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import React from "react";
import { screen } from "@testing-library/react";
import { installerRender, mockNavigateFn } from "~/test-utils";
import { ProductSelectionPage } from "~/components/product";
import { Product } from "~/types/software";
import { useProduct } from "~/queries/software";
import { Product, RegistrationInfo } from "~/types/software";
import { useProduct, useRegistration } from "~/queries/software";

jest.mock("~/components/product/ProductRegistrationAlert", () => () => (
<div>ProductRegistrationAlert Mock</div>
Expand All @@ -50,6 +50,7 @@ const microOs: Product = {
};

let mockSelectedProduct: Product;
let registrationInfoMock: RegistrationInfo;

jest.mock("~/queries/software", () => ({
...jest.requireActual("~/queries/software"),
Expand All @@ -61,11 +62,24 @@ jest.mock("~/queries/software", () => ({
},
useProductChanges: () => jest.fn(),
useConfigMutation: () => ({ mutate: mockConfigMutation }),
useRegistration: (): ReturnType<typeof useRegistration> => registrationInfoMock,
}));

describe("ProductSelectionPage", () => {
beforeEach(() => {
mockSelectedProduct = tumbleweed;
registrationInfoMock = { key: "", email: "" };
});

describe("when there is a registration code set", () => {
beforeEach(() => {
registrationInfoMock = { key: "INTERNAL-USE-ONLY-1234-5678", email: "" };
});

it("navigates to root path", async () => {
installerRender(<ProductSelectionPage />);
await screen.findByText("Navigating to /");
});
});

describe("when there is a product already selected", () => {
Expand Down
9 changes: 7 additions & 2 deletions web/src/components/product/ProductSelectionPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ import {
FormGroup,
Button,
} from "@patternfly/react-core";
import { Navigate, useNavigate } from "react-router-dom";
import { Page } from "~/components/core";
import { Center } from "~/components/layout";
import { useConfigMutation, useProduct } from "~/queries/software";
import { useConfigMutation, useProduct, useRegistration } from "~/queries/software";
import pfTextStyles from "@patternfly/react-styles/css/utilities/Text/text";
import pfRadioStyles from "@patternfly/react-styles/css/components/Radio/radio";
import { sprintf } from "sprintf-js";
import { _ } from "~/i18n";
import { useNavigate } from "react-router-dom";
import { PATHS } from "~/router";
import { isEmpty } from "~/utils";

const ResponsiveGridItem = ({ children }) => (
<GridItem sm={10} smOffset={1} lg={8} lgOffset={2} xl={6} xlOffset={3}>
Expand Down Expand Up @@ -97,10 +99,13 @@ const BackLink = () => {

function ProductSelectionPage() {
const setConfig = useConfigMutation();
const registration = useRegistration();
const { products, selectedProduct } = useProduct({ suspense: true });
const [nextProduct, setNextProduct] = useState(selectedProduct);
const [isLoading, setIsLoading] = useState(false);

if (!isEmpty(registration?.key)) return <Navigate to={PATHS.root} />;

const onSubmit = async (e: React.FormEvent) => {
e.preventDefault();

Expand Down