Skip to content
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

Wd-16440 Restrict channel users from editing billing information #14547

Merged
merged 4 commits into from
Jan 9, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
FormContext,
defaultValues,
} from "advantage/distributor/utils/FormContext";
import { distributorProduct } from "advantage/subscribe/checkout/utils/test/Mocks";
import { DistributorProduct } from "advantage/subscribe/checkout/utils/test/Mocks";
import { ChannelOfferFactory } from "advantage/offers/tests/factories/channelOffers";
import { UserSubscriptionMarketplace } from "advantage/api/enum";

Expand All @@ -27,7 +27,7 @@ const mockSubscription: SubscriptionItem = {
const mockContextValue = {
...defaultValues,
subscriptionList: [mockSubscription] as SubscriptionItem[],
products: [distributorProduct] as ChannelProduct[],
products: [DistributorProduct] as ChannelProduct[],
offer: ChannelOfferFactory.build({ id: "offer-id-1" }),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
FormContext,
defaultValues,
} from "advantage/distributor/utils/FormContext";
import { distributorProduct } from "advantage/subscribe/checkout/utils/test/Mocks";
import { DistributorProduct } from "advantage/subscribe/checkout/utils/test/Mocks";

const mockSubscription: SubscriptionItem = {
id: "mocked-id-1",
Expand All @@ -25,7 +25,7 @@ const mockContextValue = {
...defaultValues,
subscriptionList: [mockSubscription] as SubscriptionItem[],
setSubscriptionList: jest.fn(),
products: [distributorProduct] as ChannelProduct[],
products: [DistributorProduct] as ChannelProduct[],
duration: 1,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { Formik } from "formik";
import { Elements } from "@stripe/react-stripe-js";
import { loadStripe } from "@stripe/stripe-js";
import { fireEvent, render, screen } from "@testing-library/react";
import { UAProduct } from "../../utils/test/Mocks";
import { render, screen } from "@testing-library/react";
import { DistributorProduct, UAProduct } from "../../utils/test/Mocks";
import Taxes from "./Taxes";
import userEvent from "@testing-library/user-event";
import { UserSubscriptionMarketplace } from "advantage/api/enum";

describe("TaxesTests", () => {
let queryClient: QueryClient;
Expand All @@ -14,6 +16,10 @@ describe("TaxesTests", () => {
queryClient = new QueryClient();
});

afterEach(() => {
jest.clearAllMocks();
});

it("renders country select correctly", () => {
const products = [
{
Expand Down Expand Up @@ -53,12 +59,11 @@ describe("TaxesTests", () => {
);

expect(screen.getByTestId("select-country")).toBeInTheDocument();
fireEvent.change(screen.getByTestId("select-country"), {
target: { value: "JP" },
});
userEvent.selectOptions(screen.getByTestId("select-country"), "JP");
expect(screen.queryByText("VAT number:")).not.toBeInTheDocument();
});

it("When VAT country is selected, VAT Number input displays", () => {
it("When VAT country is selected, VAT Number input displays", async () => {
const products = [
{
product: UAProduct,
Expand All @@ -76,14 +81,11 @@ describe("TaxesTests", () => {
);

expect(screen.getByTestId("select-country")).toBeInTheDocument();
fireEvent.change(screen.getByTestId("select-country"), {
target: { value: "FR" },
});

expect(screen.getByText("VAT number:")).toBeInTheDocument();
await userEvent.selectOptions(screen.getByTestId("select-country"), "FR");
expect(screen.queryByText("VAT number:")).toBeInTheDocument();
});

it("When USA is selected, State select displays", () => {
it("When USA is selected, State select displays", async () => {
const products = [
{
product: UAProduct,
Expand All @@ -99,20 +101,17 @@ describe("TaxesTests", () => {
</Formik>
</QueryClientProvider>,
);
fireEvent.change(getByTestId("select-country"), {
target: { value: "US" },
});
await userEvent.selectOptions(getByTestId("select-country"), "US");
expect(screen.getByText("State:")).toBeInTheDocument();
fireEvent.change(getByTestId("select-state"), {
target: { value: "Texas" },
});
await userEvent.selectOptions(getByTestId("select-state"), "Texas");
expect(screen.queryByText("VAT number:")).not.toBeInTheDocument();
});

it("sets status right if country is stored", () => {
global.window = Object.create(window);
Object.defineProperty(window, "accountId", { value: "ABCDEF" });

const intialValues = {
const initialValues = {
country: "GB",
};
const products = [
Expand All @@ -123,7 +122,7 @@ describe("TaxesTests", () => {
];
render(
<QueryClientProvider client={queryClient}>
<Formik initialValues={intialValues} onSubmit={jest.fn()}>
<Formik initialValues={initialValues} onSubmit={jest.fn()}>
<Elements stripe={stripePromise}>
<Taxes products={products} setError={jest.fn()} />
</Elements>
Expand Down Expand Up @@ -154,11 +153,11 @@ describe("TaxesTests", () => {
expect(screen.getByRole("button", { name: "Save" })).toBeInTheDocument();
});

it("cancel button resets tax step values", () => {
it("cancel button resets tax step values", async () => {
global.window = Object.create(window);
Object.defineProperty(window, "accountId", { value: "ABCDEF" });

const intialValues = {
const initialValues = {
country: "GB",
VATNumber: "GB123123123",
};
Expand All @@ -171,45 +170,127 @@ describe("TaxesTests", () => {
];
render(
<QueryClientProvider client={queryClient}>
<Formik initialValues={intialValues} onSubmit={jest.fn()}>
<Formik initialValues={initialValues} onSubmit={jest.fn()}>
<Elements stripe={stripePromise}>
<Taxes products={products} setError={jest.fn()} />
</Elements>
</Formik>
</QueryClientProvider>,
);

fireEvent.click(screen.getByRole("button", { name: "Edit" }));
fireEvent.change(screen.getByTestId("select-country"), {
target: { value: "FR" },
});
fireEvent.change(screen.getByTestId("field-vat-number"), {
target: { value: "FR123123123" },
});
fireEvent.click(screen.getByRole("button", { name: "Cancel" }));
await userEvent.click(screen.getByRole("button", { name: "Edit" }));
await userEvent.selectOptions(screen.getByTestId("select-country"), "FR");
await userEvent.type(screen.getByTestId("field-vat-number"), "FR123123123");
await userEvent.click(screen.getByRole("button", { name: "Cancel" }));
expect(screen.getByTestId("country")).toHaveTextContent("United Kingdom");
expect(screen.getByTestId("vat-number")).toHaveTextContent("GB123123123");

fireEvent.click(screen.getByRole("button", { name: "Edit" }));
fireEvent.change(screen.getByTestId("select-country"), {
target: { value: "US" },
});
fireEvent.change(screen.getByTestId("select-state"), {
target: { value: "AL" },
});
fireEvent.click(screen.getByRole("button", { name: "Cancel" }));
await userEvent.click(screen.getByRole("button", { name: "Edit" }));
await userEvent.selectOptions(screen.getByTestId("select-country"), "US");
await userEvent.selectOptions(screen.getByTestId("select-state"), "AL");
await userEvent.click(screen.getByRole("button", { name: "Cancel" }));
expect(screen.getByTestId("country")).toHaveTextContent("United Kingdom");
expect(screen.getByTestId("vat-number")).toHaveTextContent("GB123123123");

fireEvent.click(screen.getByRole("button", { name: "Edit" }));
fireEvent.change(screen.getByTestId("select-country"), {
target: { value: "CA" },
});
fireEvent.change(screen.getByTestId("select-ca-province"), {
target: { value: "AL" },
});
fireEvent.click(screen.getByRole("button", { name: "Cancel" }));
await userEvent.click(screen.getByRole("button", { name: "Edit" }));
await userEvent.selectOptions(screen.getByTestId("select-country"), "CA");
await userEvent.selectOptions(
screen.getByTestId("select-ca-province"),
"AB",
);
await userEvent.click(screen.getByRole("button", { name: "Cancel" }));
expect(screen.getByTestId("country")).toHaveTextContent("United Kingdom");
expect(screen.getByTestId("vat-number")).toHaveTextContent("GB123123123");
});

it("Edit should be available for pro users", async () => {
global.window = Object.create(window);
Object.defineProperty(window, "accountId", { value: "ABCDEF" });

const initialValues = {
country: "GB",
VATNumber: "GB123123123",
marketPlace: "canonical-ua",
};

const products = [
{
product: UAProduct,
quantity: 1,
},
];
render(
<QueryClientProvider client={queryClient}>
<Formik initialValues={initialValues} onSubmit={jest.fn()}>
<Elements stripe={stripePromise}>
<Taxes products={products} setError={jest.fn()} />
</Elements>
</Formik>
</QueryClientProvider>,
);

expect(screen.queryByRole("button", { name: "Edit" })).toBeInTheDocument();
await userEvent.click(screen.getByRole("button", { name: "Edit" }));

const countryField = await screen.findByTestId("select-country");
expect(countryField).toBeInTheDocument();
expect(screen.getByTestId("field-vat-number")).toBeInTheDocument();
expect(screen.getByRole("button", { name: "Save" })).toBeInTheDocument();
});

it("Edit button should not be displayed for channel users", () => {
global.window = Object.create(window);
Object.defineProperty(window, "accountId", { value: "ABCDEF" });
const initialValues = {
country: "GB",
VATNumber: "GB123123123",
marketPlace: UserSubscriptionMarketplace.CanonicalProChannel,
};
const products = [
{
product: DistributorProduct,
quantity: 1,
},
];
render(
<QueryClientProvider client={queryClient}>
<Formik initialValues={initialValues} onSubmit={jest.fn()}>
<Elements stripe={stripePromise}>
<Taxes products={products} setError={jest.fn()} />
</Elements>
</Formik>
</QueryClientProvider>,
);

expect(screen.queryByTestId("tax-edit-button")).toBeInTheDocument();
});

it("New channel users should be able to add their tax info", async () => {
global.window = Object.create(window);
Object.defineProperty(window, "accountId", { value: "ABCDEF" });

const initialValues = {
country: "",
VATNumber: "",
marketPlace: UserSubscriptionMarketplace.CanonicalProChannel,
};

const products = [
{
product: DistributorProduct,
quantity: 1,
},
];
render(
<QueryClientProvider client={queryClient}>
<Formik initialValues={initialValues} onSubmit={jest.fn()}>
<Elements stripe={stripePromise}>
<Taxes products={products} setError={jest.fn()} />
</Elements>
</Formik>
</QueryClientProvider>,
);

expect(screen.queryByTestId("tax-edit-button")).not.toBeInTheDocument();
expect(screen.getByRole("button", { name: "Save" })).toBeInTheDocument();
});
});
Loading
Loading