Skip to content

Commit def4d5b

Browse files
committed
Fix added unit tests
1 parent 99a57b0 commit def4d5b

10 files changed

Lines changed: 281 additions & 290 deletions

File tree

static/js/publisher/pages/Listing/AdditionalInformation/__tests__/AdditionalInformation.test.tsx

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,33 @@ import "@testing-library/jest-dom";
33

44
import { mockListingData } from "../../../../test-utils";
55
import AdditionalInformation from "../AdditionalInformation";
6+
import { FieldValues, useForm } from "react-hook-form";
7+
import { getDefaultListingData } from "../../../../utils";
68

7-
const useFormMocks = {
8-
register: jest.fn(),
9-
getValues: jest.fn(),
10-
setValue: jest.fn(),
11-
watch: jest.fn(),
12-
};
9+
function TestAdditionalInformation() {
10+
const { register, setValue, watch, getValues } = useForm<FieldValues>({
11+
defaultValues: getDefaultListingData(mockListingData),
12+
});
13+
14+
return (
15+
<form>
16+
<AdditionalInformation
17+
data={mockListingData}
18+
register={register}
19+
getValues={getValues}
20+
setValue={setValue}
21+
watch={watch}
22+
/>
23+
</form>
24+
);
25+
}
1326

1427
function renderComponent() {
1528
window.SNAP_LISTING_DATA = {
1629
DNS_VERIFICATION_TOKEN: "test-dns-verification-token",
1730
};
1831

19-
return render(
20-
<AdditionalInformation
21-
data={mockListingData}
22-
register={useFormMocks.register}
23-
getValues={useFormMocks.getValues}
24-
setValue={useFormMocks.setValue}
25-
watch={useFormMocks.watch}
26-
/>,
27-
);
32+
return render(<TestAdditionalInformation />);
2833
}
2934

3035
afterEach(() => {
@@ -40,27 +45,23 @@ describe("AdditionalInformation", () => {
4045

4146
test("Fields displayed and registered with useForm", () => {
4247
renderComponent();
43-
expect(useFormMocks.register).toHaveBeenCalledTimes(3);
4448
expect(
4549
screen.getByLabelText("Display public popularity charts"),
4650
).toBeVisible();
4751
expect(screen.getByLabelText("World map")).toBeVisible();
4852
expect(screen.getByLabelText("Linux distributions")).toBeVisible();
4953
});
5054

51-
test("if public metrics is disabled all other fields are disabled", () => {
52-
useFormMocks.getValues.mockImplementation((valueKey: string) =>
53-
valueKey === "public_metrics_enabled" ? false : true,
54-
);
55+
test("if public metrics is unchecked all other fields are disabled", () => {
5556
renderComponent();
5657
expect(
57-
screen.getByRole("checkbox", { name: "public-metrics-checkbox" }),
58-
).toBeDisabled();
59-
expect(
60-
screen.getByRole("checkbox", { name: "world-map-checkbox" }),
61-
).toBeDisabled();
58+
screen.getByRole("checkbox", {
59+
name: "Display public popularity charts",
60+
}),
61+
).not.toBeChecked();
62+
expect(screen.getByRole("checkbox", { name: "World map" })).toBeDisabled();
6263
expect(
63-
screen.getByRole("checkbox", { name: "linux-distributions-checkbox" }),
64+
screen.getByRole("checkbox", { name: "Linux distributions" }),
6465
).toBeDisabled();
6566
});
6667
});

static/js/publisher/pages/Listing/AdditionalInformation/__tests__/LicenseInputs.test.tsx

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,34 @@ import "@testing-library/jest-dom";
55
import { mockListingData } from "../../../../test-utils";
66
import LicenseInputs from "../LicenseInputs";
77
import LicenseSearch from "../LicenseSearch";
8-
9-
const useFormMocks = {
10-
register: jest.fn(),
11-
getValues: jest.fn(),
12-
setValue: jest.fn(),
13-
watch: jest.fn(),
14-
};
8+
import { FieldValues, useForm } from "react-hook-form";
9+
import { getDefaultListingData } from "../../../../utils";
1510

1611
jest.mock("../LicenseSearch");
1712

13+
function TestLicenseInputs() {
14+
const { register, setValue, watch } = useForm<FieldValues>({
15+
defaultValues: getDefaultListingData(mockListingData),
16+
});
17+
18+
return (
19+
<form>
20+
<LicenseInputs
21+
listingData={mockListingData}
22+
register={register}
23+
setValue={setValue}
24+
watch={watch}
25+
/>
26+
</form>
27+
);
28+
}
29+
1830
function renderComponent() {
1931
window.SNAP_LISTING_DATA = {
2032
DNS_VERIFICATION_TOKEN: "test-dns-verification-token",
2133
};
2234

23-
return render(
24-
<LicenseInputs
25-
listingData={mockListingData}
26-
register={useFormMocks.register}
27-
setValue={useFormMocks.setValue}
28-
watch={useFormMocks.watch}
29-
/>,
30-
);
35+
return render(<TestLicenseInputs />);
3136
}
3237

3338
afterEach(() => {
@@ -38,7 +43,7 @@ describe("LicenseInputs", () => {
3843
test("if license is simple the search is displayed", () => {
3944
renderComponent();
4045
expect(screen.getByLabelText("Simple")).toBeChecked();
41-
expect(screen.getByText("The license(s) under which")).toBeVisible();
46+
expect(screen.getByText(/^The license\(s\) under which/)).toBeVisible();
4247
expect(LicenseSearch).toHaveBeenCalled();
4348
});
4449

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
1-
import { getByText, render, screen, waitFor } from "@testing-library/react";
1+
import { render, screen, waitFor } from "@testing-library/react";
22
import userEvent from "@testing-library/user-event";
33
import "@testing-library/jest-dom";
44

55
import { mockListingData } from "../../../../test-utils";
6-
import LicenseSearch from "../LicenseSearch";
6+
import LicenseInputs from "../LicenseInputs";
7+
import { FieldValues, useForm } from "react-hook-form";
8+
import { getDefaultListingData } from "../../../../utils";
79

8-
const mocks = {
9-
register: jest.fn(),
10-
setValue: jest.fn(),
11-
setLicense: jest.fn(),
12-
};
10+
function TestLicenseSearch() {
11+
const { register, setValue, watch } = useForm<FieldValues>({
12+
defaultValues: getDefaultListingData(mockListingData),
13+
});
1314

14-
function renderComponent(license = mockListingData.license) {
15+
return (
16+
<form>
17+
<LicenseInputs
18+
listingData={mockListingData}
19+
register={register}
20+
setValue={setValue}
21+
watch={watch}
22+
/>
23+
</form>
24+
);
25+
}
26+
27+
function renderComponent() {
1528
window.SNAP_LISTING_DATA = {
1629
DNS_VERIFICATION_TOKEN: "test-dns-verification-token",
1730
};
1831

19-
return render(
20-
<LicenseSearch
21-
licenses={mockListingData.licenses}
22-
license={license}
23-
register={mocks.register}
24-
setValue={mocks.setValue}
25-
setLicense={mocks.setLicense}
26-
originalLicense={mockListingData.license}
27-
/>,
28-
);
32+
return render(<TestLicenseSearch />);
2933
}
3034

3135
afterEach(() => {
@@ -45,15 +49,17 @@ describe("LicenseSearch", () => {
4549
document.getElementsByClassName("p-multiselect__input").item(0)!,
4650
);
4751

48-
const suggestions = await waitFor(() => {
49-
const suggestionsElem = document.getElementsByClassName("p-list");
52+
await waitFor(() => {
53+
const suggestionsElem = document.getElementsByClassName(
54+
"p-list",
55+
)[0]! as HTMLUListElement;
5056
expect(suggestionsElem).toBeVisible();
51-
expect(suggestionsElem).toHaveLength(1);
52-
return suggestionsElem[0] as HTMLUListElement;
57+
for (const child of suggestionsElem.children) {
58+
const text = child.textContent;
59+
// the current license should be filtered and not displayed in the dropdown
60+
expect(text).not.toEqual("testing-license");
61+
}
5362
});
54-
expect(getByText(suggestions, "random-license")).toBeVisible();
55-
// the current license should be filtered and not displayed in the dropdown
56-
expect(getByText(suggestions, "testing-license")).not.toBeInTheDocument();
5763
});
5864

5965
test("removing focus from search field closes the licenses list", async () => {
@@ -64,17 +70,17 @@ describe("LicenseSearch", () => {
6470
);
6571

6672
expect(await screen.findByText("random-license")).toBeVisible();
67-
user.click(container);
68-
expect(await screen.findByText("random-license")).not.toBeVisible();
73+
await user.click(container);
74+
const randomLicense = screen.queryByText("random-license");
75+
expect(randomLicense).toBeNull();
6976
});
7077

7178
test("remove license from list", async () => {
7279
renderComponent();
7380
const user = userEvent.setup();
7481
await user.click(screen.getByRole("button"));
7582

76-
expect(await screen.findByText("testing-license")).not.toBeVisible();
77-
expect(mocks.setLicense).toHaveBeenCalled();
78-
expect(mocks.setValue).toHaveBeenCalled();
83+
const removedElem = screen.queryByText(/testing-license/);
84+
expect(removedElem).toBeNull();
7985
});
8086
});
Lines changed: 34 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,47 @@
1-
import { Control, FieldValues } from "react-hook-form";
1+
import { FieldValues, useForm } from "react-hook-form";
22
import { render, screen, waitFor } from "@testing-library/react";
33
import "@testing-library/jest-dom";
44

55
import ContactFields from "../ContactFields";
66
import userEvent from "@testing-library/user-event";
7+
import { getDefaultListingData } from "../../../../utils";
8+
import { mockListingData } from "../../../../test-utils";
79

8-
const propsMocks = {
9-
register: jest.fn(),
10-
control: {} as Control<FieldValues>,
11-
getValues: jest.fn(),
12-
};
13-
14-
const useFieldArrayMock = jest.fn();
10+
function TestContactFields() {
11+
const { register, getValues, control } = useForm<FieldValues>({
12+
defaultValues: getDefaultListingData(mockListingData),
13+
});
1514

16-
jest.mock("react-hook-form", () => ({
17-
...jest.requireActual("react-hook-form"),
18-
useFieldArray: useFieldArrayMock,
19-
}));
15+
return (
16+
<form>
17+
<ContactFields
18+
register={register}
19+
control={control}
20+
labelName={"Contacts"}
21+
fieldName={"contacts"}
22+
getValues={getValues}
23+
/>
24+
</form>
25+
);
26+
}
2027

2128
function renderComponent() {
2229
window.SNAP_LISTING_DATA = {
2330
DNS_VERIFICATION_TOKEN: "test-dns-verification-token",
2431
};
2532

26-
return render(
27-
<ContactFields
28-
register={propsMocks.register}
29-
control={propsMocks.control}
30-
labelName={"Test"}
31-
fieldName={"test"}
32-
getValues={propsMocks.getValues}
33-
/>,
34-
);
33+
return render(<TestContactFields />);
3534
}
3635

37-
afterEach(() => {
38-
jest.clearAllMocks();
39-
});
40-
4136
describe("ContactFields", () => {
42-
test("when no data, add field is displayed", () => {
43-
const useFieldArrayResult = {
44-
fields: [],
45-
append: jest.fn(),
46-
remove: jest.fn(),
47-
};
48-
useFieldArrayMock.mockReturnValue(useFieldArrayResult);
49-
renderComponent();
50-
51-
expect(screen.getByRole("button", { name: "Add field" })).toBeVisible();
52-
expect(
53-
screen.getByRole("button", { name: "Remove this link" }),
54-
).not.toBeInTheDocument();
55-
});
56-
5737
test("data displayed and add field at the bottom", () => {
58-
const exampleUrl = "https://example.com/contact";
59-
const useFieldArrayResult = {
60-
fields: [{ url: exampleUrl }],
61-
append: jest.fn(),
62-
remove: jest.fn(),
63-
};
64-
useFieldArrayMock.mockReturnValue(useFieldArrayResult);
65-
propsMocks.getValues.mockReturnValue(exampleUrl);
6638
renderComponent();
6739

68-
const fieldElement = screen.getByDisplayValue(exampleUrl);
69-
const addFieldElement = screen.getByRole("button", { name: "Add field" });
40+
const fieldElement = screen.getByDisplayValue(
41+
"https://example.com/contact",
42+
);
43+
const addFieldElement = screen.getByText(/Add field/);
44+
7045
expect(fieldElement).toBeVisible();
7146
expect(addFieldElement).toBeVisible();
7247
expect(fieldElement.compareDocumentPosition(addFieldElement)).toEqual(
@@ -75,14 +50,6 @@ describe("ContactFields", () => {
7550
});
7651

7752
test("data can be removed", async () => {
78-
const exampleUrl = "https://example.com/contact";
79-
const useFieldArrayResult = {
80-
fields: [{ url: exampleUrl }],
81-
append: jest.fn(),
82-
remove: jest.fn(),
83-
};
84-
useFieldArrayMock.mockReturnValue(useFieldArrayResult);
85-
propsMocks.getValues.mockReturnValue(exampleUrl);
8653
const user = userEvent.setup();
8754
renderComponent();
8855

@@ -92,21 +59,21 @@ describe("ContactFields", () => {
9259
expect(deleteButton).toBeVisible();
9360

9461
await user.click(deleteButton);
95-
await waitFor(() => expect(useFieldArrayResult.remove).toHaveBeenCalled());
62+
await waitFor(() => {
63+
const links = screen.queryAllByRole("textbox");
64+
expect(links).toHaveLength(0);
65+
});
9666
});
9767

9868
test("data can be added", async () => {
99-
const useFieldArrayResult = {
100-
fields: [],
101-
append: jest.fn(),
102-
remove: jest.fn(),
103-
};
104-
useFieldArrayMock.mockReturnValue(useFieldArrayResult);
10569
const user = userEvent.setup();
10670
renderComponent();
10771

108-
const addButton = screen.getByRole("button", { name: "Add field" });
72+
const addButton = screen.getByText(/Add field/);
10973
await user.click(addButton);
110-
await waitFor(() => expect(useFieldArrayResult.append).toHaveBeenCalled());
74+
await waitFor(() => {
75+
const links = screen.queryAllByRole("textbox");
76+
expect(links).toHaveLength(2);
77+
});
11178
});
11279
});

0 commit comments

Comments
 (0)