Skip to content

Commit ae145bc

Browse files
author
shubhamBansal
committed
Unit tests for browsing page component
1 parent e1124bd commit ae145bc

File tree

11 files changed

+234
-10
lines changed

11 files changed

+234
-10
lines changed

dashboard/package.json

+10
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@
4040
"test": "react-scripts test",
4141
"eject": "react-scripts eject"
4242
},
43+
"jest":{
44+
"transform": {
45+
"^.+\\.[t|j]sx?$": "babel-jest"
46+
},
47+
"transformIgnorePatterns":["node_modules/(?!@patternfly)/"]
48+
}
49+
,
4350
"eslintConfig": {
4451
"extends": [
4552
"react-app",
@@ -59,7 +66,10 @@
5966
]
6067
},
6168
"devDependencies": {
69+
"@babel/preset-env": "^7.17.10",
70+
"babel-jest": "^28.1.0",
6271
"css-loader": "^6.7.1",
72+
"jest": "^28.1.0",
6373
"less": "^4.1.2",
6474
"less-loader": "^10.2.0",
6575
"style-loader": "^3.3.1"

dashboard/public/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
Notice the use of %PUBLIC_URL% in the tags above.
3131
It will be replaced with the URL of the `public` folder during the build.
3232
Only files inside the `public` folder can be referenced from the HTML.
33-
33+
3434
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
3535
work correctly both with client-side routing and a non-root public URL.
3636
Learn how to configure a non-root public URL by running `npm run build`.

dashboard/src/App.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import MainLayout from 'modules/containers/MainLayout';
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

-8
This file was deleted.
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+
});
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+
});
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+
});
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+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React,{useState} from "react";
2+
import "./index.css"
3+
import { InputGroup, TextInput, Button } from "@patternfly/react-core";
4+
import SearchIcon from "@patternfly/react-icons/dist/esm/icons/search-icon";
5+
import { filterData } from "utils/filterDataset";
6+
function SearchBox({
7+
dataArray,
8+
setPublicData,
9+
startDate,
10+
endDate,
11+
setControllerName,
12+
}) {
13+
const [controllerValue, setControllerValue] = useState("");
14+
const searchController = () => {
15+
setPublicData(filterData(dataArray, startDate, endDate, controllerValue));
16+
setControllerName(controllerValue);
17+
};
18+
return (
19+
<InputGroup className="searchInputGroup">
20+
<TextInput
21+
type="text"
22+
placeholder="Search Controllers"
23+
onChange={(e) => setControllerValue(e)}
24+
></TextInput>
25+
<Button variant="control" onClick={searchController} aria-label="searchButton">
26+
<SearchIcon />
27+
</Button>
28+
</InputGroup>
29+
);
30+
}
31+
32+
export default SearchBox;
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/utils/mockData.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
export const MOCK_DATA = [
2+
{
3+
controller: "dhcp1",
4+
name: "pbench_user_benchmark1",
5+
metadata: {
6+
"dataset.created": "2022-02-16T13:21:29+00:00",
7+
},
8+
},
9+
{
10+
controller: "dhcp2",
11+
name: "pbench_user_benchmark2",
12+
metadata: {
13+
"dataset.created": "2022-02-18T13:21:29+00:00",
14+
},
15+
},
16+
{
17+
controller: "dhcp3",
18+
name: "pbench_user_benchmark3",
19+
metadata: {
20+
"dataset.created": "2022-02-20T13:21:29+00:00",
21+
},
22+
},
23+
{
24+
controller: "dhcp4",
25+
name: "pbench_user_benchmark4",
26+
metadata: {
27+
"dataset.created": "2022-02-25T13:21:29+00:00",
28+
},
29+
},
30+
{
31+
controller: "dhcp5",
32+
name: "pbench_user_benchmark5",
33+
metadata: {
34+
"dataset.created": "2022-03-08T13:21:29+00:00",
35+
},
36+
},
37+
];

0 commit comments

Comments
 (0)