Skip to content

Commit 19b9ff0

Browse files
committed
updated tests
1 parent 4f08a15 commit 19b9ff0

File tree

2 files changed

+56
-33
lines changed

2 files changed

+56
-33
lines changed

apps/frontend/ui/src/navigation/partials/OptionConcepts/stories.tsx

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,14 @@ import OptionConcepts from "@app-ui/navigation/partials/OptionConcepts";
66
const meta: Meta = {
77
title: "App/UI/Navigation/Partials/OptionConcepts",
88
component: OptionConcepts,
9-
args: {
10-
concepts: ["Partiality", "Signaling", "Connectivity", "Transformativity"],
11-
onSearch: () => {},
12-
},
139
};
1410

1511
export default meta;
1612

1713
type Story = StoryObj<typeof OptionConcepts>;
1814

1915
export const Index: Story = {
20-
render: () => {
16+
render: (props) => {
2117
const [checkedValues, setCheckedValues] = useState<
2218
Record<string, string[]>
2319
>({
@@ -39,8 +35,12 @@ export const Index: Story = {
3935
setIsFetching(false);
4036
}, 2000);
4137
}}
42-
isReqestingConcepts={isFetching}
43-
disableButton={checkedValues.concept.length === 0 || isFetching}
38+
isReqestingConcepts={isFetching || props.isReqestingConcepts}
39+
disableButton={
40+
checkedValues.concept.length === 0 ||
41+
isFetching ||
42+
props.disableButton
43+
}
4444
checkedValuesState={checkedValues}
4545
setCheckedValuesState={setCheckedValues}
4646
/>
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
1+
import { useState } from "react";
2+
13
import { render, screen, fireEvent } from "@tests-unit-browser";
24
import "@testing-library/jest-dom";
35

46
import OptionConcepts from "@app-ui/navigation/partials/OptionConcepts";
57

8+
function Wrapper() {
9+
const [checkedValues, setCheckedValues] = useState<Record<string, string[]>>({
10+
concept: [],
11+
});
12+
const [isFetching, setIsFetching] = useState(false);
13+
14+
return (
15+
<OptionConcepts
16+
concepts={["concept1", "concept2", "concept3"]}
17+
onSearch={() => {
18+
setIsFetching(true);
19+
setTimeout(() => {
20+
setIsFetching(false);
21+
}, 3000);
22+
}}
23+
isReqestingConcepts={isFetching}
24+
disableButton={checkedValues.concept.length === 0 || isFetching}
25+
checkedValuesState={checkedValues}
26+
setCheckedValuesState={setCheckedValues}
27+
/>
28+
);
29+
}
30+
631
describe("OptionConcepts", () => {
732
it("should render with given concepts", () => {
8-
const onSearch = jest.fn((value: string) => value);
9-
10-
render(
11-
<OptionConcepts
12-
concepts={["concept1", "concept2", "concept3"]}
13-
onSearch={onSearch}
14-
/>,
15-
);
33+
render(<Wrapper />);
1634

1735
const menuList = screen.getByRole("menu");
1836
expect(menuList).toBeInTheDocument();
@@ -26,40 +44,45 @@ describe("OptionConcepts", () => {
2644
});
2745

2846
it("should be able to select different concepts", () => {
29-
const onSearch = jest.fn((value: string) => value);
30-
31-
render(
32-
<OptionConcepts
33-
concepts={["concept1", "concept2", "concept3"]}
34-
onSearch={onSearch}
35-
/>,
36-
);
47+
render(<Wrapper />);
3748

3849
const concept1 = screen.getByText("concept1");
39-
expect(concept1).toBeInTheDocument();
4050
const concept2 = screen.getByText("concept2");
41-
expect(concept2).toBeInTheDocument();
4251
const concept3 = screen.getByText("concept3");
43-
expect(concept3).toBeInTheDocument();
4452

4553
const searchButton = screen.getByRole("button", { name: "Search" });
4654
expect(searchButton).toBeInTheDocument();
4755
expect(searchButton).toBeDisabled();
4856

4957
fireEvent.click(concept1);
58+
// has aria-checked attribute, when checked
59+
expect(concept1.parentElement).toHaveAttribute("aria-checked", "true");
5060
expect(searchButton).toBeEnabled();
5161

52-
fireEvent.click(searchButton);
53-
expect(onSearch).toHaveBeenCalledWith("concept1");
54-
5562
fireEvent.click(concept2);
56-
fireEvent.click(searchButton);
57-
expect(onSearch).toHaveBeenCalledWith("concept2");
63+
expect(concept2.parentElement).toHaveAttribute("aria-checked", "true");
64+
// concept1 should be unchecked -> exclusitivity
65+
expect(concept1.parentElement).toHaveAttribute("aria-checked", "false");
5866

5967
fireEvent.click(concept3);
68+
expect(concept3.parentElement).toHaveAttribute("aria-checked", "true");
69+
expect(concept2.parentElement).toHaveAttribute("aria-checked", "false");
70+
});
71+
72+
it("should show loading state and disable search button", () => {
73+
render(<Wrapper />);
74+
75+
const concept1 = screen.getByText("concept1");
76+
const searchButton = screen.getByRole("button", { name: "Search" });
77+
78+
fireEvent.click(concept1);
79+
expect(searchButton).toBeEnabled();
80+
6081
fireEvent.click(searchButton);
61-
expect(onSearch).toHaveBeenCalledWith("concept3");
82+
expect(searchButton).toBeDisabled();
6283

63-
expect(onSearch).toHaveBeenCalledTimes(3);
84+
// role progressbar is used for loading state
85+
const loading = screen.getByRole("progressbar");
86+
expect(loading).toBeInTheDocument();
6487
});
6588
});

0 commit comments

Comments
 (0)