Skip to content

Commit 739e5dc

Browse files
author
shubhamBansal
committed
Unit tests for browsing page component
1 parent 84ace80 commit 739e5dc

File tree

11 files changed

+204
-10
lines changed

11 files changed

+204
-10
lines changed

dashboard/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737
"test": "react-scripts test",
3838
"eject": "react-scripts eject"
3939
},
40+
"jest":{
41+
"transform": {
42+
"^.+\\.[t|j]sx?$": "babel-jest"
43+
},
44+
"transformIgnorePatterns":["node_modules/(?!@patternfly)/"]
45+
}
46+
,
4047
"eslintConfig": {
4148
"extends": [
4249
"react-app",
@@ -56,7 +63,10 @@
5663
]
5764
},
5865
"devDependencies": {
66+
"@babel/preset-env": "^7.17.10",
67+
"babel-jest": "^28.1.0",
5968
"css-loader": "^6.7.1",
69+
"jest": "^28.1.0",
6070
"less": "^4.1.2",
6171
"less-loader": "^10.2.0",
6272
"style-loader": "^3.3.1"

dashboard/src/App.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { TableWithFavorite } from 'modules/components/TableComponent';
1010
function App() {
1111
useEffect(() => {
1212
const faviconLogo = document.getElementById("favicon");
13-
faviconLogo.setAttribute("href", favicon);
13+
faviconLogo?.setAttribute("href", favicon);
1414
}, []);
1515

1616
return (

dashboard/src/App.test.js

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Provider } from "react-redux";
2+
import store from "store/store";
3+
import App from "../../../App";
4+
const { render, screen, fireEvent } = require("@testing-library/react");
5+
const AppWrapper = () => {
6+
return (
7+
<Provider store={store}>
8+
<App />
9+
</Provider>
10+
);
11+
};
12+
test("Alert message is displayed on initial load", () => {
13+
render(<AppWrapper />);
14+
const alert = screen.getByText(/want to see your own data/i);
15+
expect(alert).toBeInTheDocument();
16+
});
17+
18+
test("Alert message is closed on clicking close button", () => {
19+
render(<AppWrapper />);
20+
const alert = screen.getByText(/want to see your own data/i);
21+
const closeButton = screen.getByRole("button", {
22+
name: /close info alert/i,
23+
});
24+
fireEvent.click(closeButton);
25+
expect(alert).not.toBeVisible();
26+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Provider } from "react-redux";
2+
import store from "store/store";
3+
import { MOCK_DATA } from "utils/mockData";
4+
import App from "../../../App";
5+
const { render, screen, fireEvent } = require("@testing-library/react");
6+
const AppWrapper = () => {
7+
return (
8+
<Provider store={store}>
9+
<App />
10+
</Provider>
11+
);
12+
};
13+
jest.mock("utils/api", () => {
14+
return {
15+
get: () => ({
16+
data: MOCK_DATA,
17+
}),
18+
};
19+
});
20+
test("data is filtered based on date range selected from date picker", async () => {
21+
render(<AppWrapper />);
22+
await screen.findByText("dhcp1");
23+
const datePickerInput = screen.getAllByPlaceholderText(/yyyy-mm-dd/i);
24+
fireEvent.change(datePickerInput[0], { target: { value: "2022-02-16" } });
25+
fireEvent.change(datePickerInput[1], { target: { value: "2022-02-20" } });
26+
const updateBtn = screen.getByRole("button", { name: /update/i });
27+
fireEvent.click(updateBtn);
28+
const cells = screen.getAllByRole("cell");
29+
expect(cells).toHaveLength(12);
30+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Provider } from "react-redux";
2+
import store from "store/store";
3+
import App from "../../../App";
4+
const { render, screen } = require("@testing-library/react");
5+
const AppWrapper = () => {
6+
return (
7+
<Provider store={store}>
8+
<App />
9+
</Provider>
10+
);
11+
};
12+
test("Page heading is displayed on initial load", () => {
13+
render(<AppWrapper />);
14+
const heading = screen.getByRole("heading", { name: /controllers/i });
15+
expect(heading).toBeInTheDocument();
16+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Provider } from "react-redux";
2+
import store from "store/store";
3+
import { MOCK_DATA } from "utils/mockData";
4+
import App from "../../../App";
5+
const { render, screen, fireEvent } = require("@testing-library/react");
6+
const AppWrapper = () => {
7+
return (
8+
<Provider store={store}>
9+
<App />
10+
</Provider>
11+
);
12+
};
13+
jest.mock("utils/api", () => {
14+
return {
15+
get: () => ({
16+
data: MOCK_DATA,
17+
}),
18+
};
19+
});
20+
test("data is filtered based on value in search box", async () => {
21+
render(<AppWrapper />);
22+
await screen.findByText("dhcp1");
23+
const searchBox = screen.getByPlaceholderText(/search controllers/i);
24+
fireEvent.change(searchBox, { target: { value: "dhcp2" } });
25+
const searchBtn = screen.getByRole("button", {
26+
name: /searchButton/i,
27+
});
28+
fireEvent.click(searchBtn);
29+
const controllerTwo = screen.queryByText("dhcp2");
30+
const controllerThree = screen.queryByText("dhcp3");
31+
expect(controllerTwo).toBeInTheDocument();
32+
expect(controllerThree).not.toBeInTheDocument();
33+
});

dashboard/src/modules/components/SearchComponent/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function SearchBox({
2222
placeholder="Search Controllers"
2323
onChange={(e) => setControllerValue(e)}
2424
></TextInput>
25-
<Button variant="control" onClick={searchController}>
25+
<Button variant="control" onClick={searchController} aria-label="searchButton">
2626
<SearchIcon />
2727
</Button>
2828
</InputGroup>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Provider } from "react-redux";
2+
import store from "store/store";
3+
import { MOCK_DATA } from "utils/mockData";
4+
import App from "../../../App";
5+
const {
6+
render,
7+
screen,
8+
waitFor,
9+
fireEvent,
10+
} = require("@testing-library/react");
11+
const AppWrapper = () => {
12+
return (
13+
<Provider store={store}>
14+
<App />
15+
</Provider>
16+
);
17+
};
18+
jest.mock("utils/api", () => {
19+
return {
20+
get: () => ({
21+
data: MOCK_DATA,
22+
}),
23+
};
24+
});
25+
26+
test("data from API is displayed on initial load", async () => {
27+
render(<AppWrapper />);
28+
const benchmarkName = await screen.findByText("pbench_user_benchmark1");
29+
const cells = await screen.findAllByRole("cell");
30+
await waitFor(() => expect(benchmarkName).toBeInTheDocument());
31+
await waitFor(() => expect(cells).toHaveLength(20));
32+
});
33+
34+
test("row is favorited after clicking on favorite icon", async () => {
35+
render(<AppWrapper />);
36+
await screen.findByText("dhcp1");
37+
const starBtn = screen.getAllByRole("button", {
38+
name: /not starred/i,
39+
});
40+
fireEvent.click(starBtn[0]);
41+
fireEvent.click(starBtn[1]);
42+
const favoriteBtn = screen.getByRole("button", {
43+
name: /see favorites button/i,
44+
});
45+
fireEvent.click(favoriteBtn);
46+
const favoriteCell = screen.getAllByRole("cell");
47+
expect(favoriteCell).toHaveLength(8);
48+
});

dashboard/src/modules/components/TableComponent/index.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,15 @@ export const TableWithFavorite = () => {
149149
isSelected={isSelected === "controllerListButton"}
150150
onChange={handleButtonClick}
151151
className="controllerListButton"
152+
aria-label="See Controller Button"
152153
/>
153154
<ToggleGroupItem
154155
text={`Favorites(${favoriteRepoNames.length})`}
155156
buttonId="favoriteListButton"
156157
isSelected={isSelected === "favoriteListButton"}
157158
onChange={handleButtonClick}
158159
className="favoriteListButton"
160+
aria-label="See Favorites Button"
159161
/>
160162
</ToggleGroup>
161163
<TableComposable aria-label="Favoritable table" variant="compact">

0 commit comments

Comments
 (0)