Skip to content

Commit

Permalink
Test RuleModal
Browse files Browse the repository at this point in the history
  • Loading branch information
poulch committed Dec 12, 2023
1 parent 96d6b34 commit 0ee3770
Show file tree
Hide file tree
Showing 6 changed files with 405 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export const Rule = ({
setValue("conditions", [{ ...intialConditionValues }]);
}}
size="small"
data-test-id="channel-dropdown"
label={intl.formatMessage(commonMessages.channel)}
options={channelOptions}
disabled={disabled || channelfield.disabled}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const DiscountTypeSwitch = ({
id="fixed"
disabled={disabled}
value={RewardValueTypeEnum.FIXED}
name="fixed"
marginLeft={0.5}
paddingY={2}
>
Expand All @@ -48,6 +49,7 @@ export const DiscountTypeSwitch = ({
<Switch.Item
id="percentage"
disabled={disabled}
name="percentage"
value={RewardValueTypeEnum.PERCENTAGE}
marginRight={0.5}
paddingY={2}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export const RuleConditionRow = ({
updateCondition(conditionIndex, { ...condition, values: [] });
typeField.onChange(e);
}}
data-test-id="rule-type"
onBlur={typeField.onBlur}
disabled={disabled}
/>
Expand All @@ -101,6 +102,7 @@ export const RuleConditionRow = ({
<RuleInputWrapper>
<Multiselect
alwaysFetchOnFocus
data-test-id="rule-values"
value={condition.values}
fetchOptions={fetch}
fetchMore={fetchMoreProps}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
import { MockedProvider } from "@apollo/client/testing";
import { mockResizeObserver } from "@dashboard/components/Datagrid/testUtils";
import { ChannelFragment, RewardValueTypeEnum } from "@dashboard/graphql";
import { ThemeProvider as LegacyThemeProvider } from "@saleor/macaw-ui";
import { ThemeProvider } from "@saleor/macaw-ui-next";
import { act, render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import React, { ReactNode } from "react";

import {
searchCategoriesMock,
searchProductsMock,
seatchCollectionMock,
seatchVariantsMock,
} from "./mocks";
import { RuleModal } from "./RuleModal";

jest.mock("react-intl", () => ({
useIntl: jest.fn(() => ({
formatMessage: jest.fn(x => x.defaultMessage),
})),
defineMessages: jest.fn(x => x),
FormattedMessage: ({ defaultMessage }: { defaultMessage: string }) => (
<>{defaultMessage}</>
),
}));

const Wrapper = ({ children }: { children: ReactNode }) => {
return (
<MockedProvider
mocks={[
searchCategoriesMock,
seatchCollectionMock,
searchProductsMock,
seatchVariantsMock,
]}
>
<LegacyThemeProvider>
<ThemeProvider>{children}</ThemeProvider>
</LegacyThemeProvider>
</MockedProvider>
);
};

describe("RuleModal", () => {
beforeAll(() => {
Object.defineProperty(window, "matchMedia", {
writable: true,
value: jest.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // Deprecated
removeListener: jest.fn(), // Deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});

const mockIntersectionObserver = jest.fn();
mockIntersectionObserver.mockReturnValue({
observe: () => null,
unobserve: () => null,
disconnect: () => null,
});
window.IntersectionObserver = mockIntersectionObserver;

mockResizeObserver();
});

it("should prevent form submit when there are form error errors", async () => {
// Arrange
const onSubmit = jest.fn();

render(
<RuleModal
channels={[]}
confimButtonState="default"
errors={[]}
onClose={jest.fn()}
open={true}
onSubmit={onSubmit}
/>,
{ wrapper: Wrapper },
);

// Act
userEvent.click(screen.getByRole("button", { name: /save/i }));

// Assert
expect(onSubmit).not.toHaveBeenCalled();
});

it("should allow to create new Rule", async () => {
// Arrange
const onSubmit = jest.fn();

render(
<RuleModal
channels={
[
{
currencyCode: "$",
id: "1",
name: "PLN",
slug: "PLN",
isActive: true,
},
] as ChannelFragment[]
}
confimButtonState="default"
errors={[]}
onClose={jest.fn()}
open={true}
onSubmit={onSubmit}
/>,
{ wrapper: Wrapper },
);

// Act
await userEvent.type(
screen.getByRole("input", { name: "Name" }),
"Name 123",
);
await userEvent.click(screen.getByRole("combobox"));
expect(await screen.findByText(/pln/i)).toBeInTheDocument();

await act(async () => {
await userEvent.click(screen.getAllByTestId("select-option")[0]);
});

await userEvent.click(await screen.findByTestId(/rule-type/i));
await userEvent.click(screen.getAllByTestId("select-option")[0]);

await userEvent.click(await screen.findByTestId(/rule-value/i));
await userEvent.click(await screen.getAllByTestId("select-option")[0]);

await userEvent.type(
screen.getByRole("input", { name: "Discount value" }),
"22",
);

await userEvent.click(screen.getByRole("button", { name: /save/i }));

// Assert
expect(onSubmit).toHaveBeenCalledWith({
name: "Name 123",
channel: {
label: "PLN",
value: "1",
},
conditions: [
{
condition: "is",
type: "product",
values: [
{
label: "Bean Juice",
value: "UHJvZHVjdDo3OQ==",
},
],
},
],
description: "",
rewardValue: 22,
rewardValueType: "PERCENTAGE",
});
});

it("should allow to update new Rule", async () => {
// Arrange
const onSubmit = jest.fn();

render(
<RuleModal
channels={
[
{
currencyCode: "$",
id: "1",
name: "PLN",
slug: "PLN",
isActive: true,
},
] as ChannelFragment[]
}
confimButtonState="default"
errors={[]}
onClose={jest.fn()}
open={true}
onSubmit={onSubmit}
initialFormValues={{
name: "Name 123",
channel: {
label: "PLN",
value: "1",
},
conditions: [
{
type: "product",
condition: "is",
values: [
{
label: "Bean Juice",
value: "UHJvZHVjdDo3OQ==",
},
],
},
],
rewardValue: 22,
rewardValueType: RewardValueTypeEnum.PERCENTAGE,
description: "",
}}
/>,
{ wrapper: Wrapper },
);

// Act
const nameField = screen.getByRole("input", { name: "Name" });
await userEvent.clear(nameField);
await userEvent.type(nameField, "New name");

await userEvent.click(screen.getByRole("radio", { name: "$" }));

const discountValueField = screen.getByRole("input", {
name: "Discount value",
});
await userEvent.clear(discountValueField);
await userEvent.type(discountValueField, "122");

await userEvent.click(screen.getByRole("button", { name: /save/i }));

// Assert
expect(onSubmit).toHaveBeenCalledWith({
name: "New name",
channel: {
label: "PLN",
value: "1",
},
conditions: [
{
condition: "is",
type: "product",
values: [
{
label: "Bean Juice",
value: "UHJvZHVjdDo3OQ==",
},
],
},
],
description: "",
rewardValue: 122,
rewardValueType: "FIXED",
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export const RuleModal = ({
<FormattedMessage {...buttonMessages.close} />
</Button>
<ConfirmButton
data-test-id="saveRuleButton"
transitionState={confimButtonState}
onClick={methods.handleSubmit(handleSubmit)}
>
Expand Down
Loading

0 comments on commit 0ee3770

Please sign in to comment.