Skip to content

Commit

Permalink
Merge pull request #3 from saracrz/feature/add-test-cases
Browse files Browse the repository at this point in the history
Feature/add test cases
  • Loading branch information
saracrz authored Nov 7, 2023
2 parents d24d730 + 2cce90a commit e2e85f4
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 2 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
testEnvironment: "jsdom",
setupFilesAfterEnv: ["<rootDir>/tests/setupTests.ts"],
testMatch: ["<rootDir>/tests/**/*.(test).(ts|tsx)"],
testMatch: ["<rootDir>/src/**/*.(test).(ts|tsx)"],
testPathIgnorePatterns: ["<rootDir>/tests/e2e/"],
transform: {
"^.+\\.(js|jsx|ts|tsx)$": [
Expand Down
112 changes: 112 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"private": true,
"version": "1.0.0",
"dependencies": {
"jest-fetch-mock": "^3.0.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"styled-components": "^6.1.0"
Expand Down Expand Up @@ -56,6 +57,12 @@
}
]
},
"jest": {
"automock": false,
"setupFiles": [
"./setupJest.js"
]
},
"browserslist": {
"production": [
">0.2%",
Expand Down
2 changes: 1 addition & 1 deletion tests/App.test.tsx → src/App.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { render, screen } from "@testing-library/react";

import { App } from "../src/App";
import { App } from "./App";

test("App component display header", () => {
render(<App />);
Expand Down
37 changes: 37 additions & 0 deletions src/pages/DashboardPage.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* eslint-disable jest/no-mocks-import */
import { render, screen } from "@testing-library/react";

import { mockedDashboards } from "../../tests/fixtures/index";
import {
mockUseGetDashboards,
mockUseGetDashboardsLoadingData,
} from "../../tests/fixtures/useGetDashboards";
import { DashboardPage } from "./DashboardPage";

jest.mock("../hooks/useGetDashboards", () => ({
useGetDashboards: jest.fn(),
}));

test("Dashboard component displays dashboards titles", () => {
mockUseGetDashboards(mockedDashboards);
render(<DashboardPage />);

expect(screen.getByText("Antenatal Care")).toBeInTheDocument();
expect(screen.getByText("Cases Malaria")).toBeInTheDocument();
expect(screen.getByText("Delivery")).toBeInTheDocument();
expect(screen.getByText("Disease Surveillance")).toBeInTheDocument();
expect(screen.getByText("Immunization")).toBeInTheDocument();
});

test("Dashboard component when loading data should not display", () => {
mockUseGetDashboardsLoadingData([]);
render(<DashboardPage />);

expect(screen.queryByText("Antenatal Care")).not.toBeInTheDocument();
expect(screen.queryByText("Cases Malaria")).not.toBeInTheDocument();
expect(screen.queryByText("Delivery")).not.toBeInTheDocument();
expect(screen.queryByText("Disease Surveillance")).not.toBeInTheDocument();
expect(screen.queryByText("Immunization")).not.toBeInTheDocument();

expect(screen.getByText("Loading")).toBeInTheDocument();
});
24 changes: 24 additions & 0 deletions tests/fixtures/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { IDashboards } from "../../src/types";

export const mockedDashboards: IDashboards[] = [
{
displayName: "Antenatal Care",
id: "nghVC4wtyzi",
},
{
displayName: "Cases Malaria",
id: "JW7RlN5xafN",
},
{
displayName: "Delivery",
id: "iMnYyBfSxmM",
},
{
displayName: "Disease Surveillance",
id: "vqh4MBWOTi4",
},
{
displayName: "Immunization",
id: "TAMlzYkstb7",
},
];
16 changes: 16 additions & 0 deletions tests/fixtures/useGetDashboards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useGetDashboards } from "../../src/hooks/useGetDashboards";
import { IDashboards } from "../../src/types";

export const mockUseGetDashboards = (mockedDashboards: IDashboards[]): void => {
(useGetDashboards as jest.Mock).mockReturnValue({
dashboards: mockedDashboards,
loading: false,
});
};

export const mockUseGetDashboardsLoadingData = (mockedDashboards: IDashboards[]): void => {

Check warning on line 11 in tests/fixtures/useGetDashboards.ts

View workflow job for this annotation

GitHub Actions / lint

'mockedDashboards' is defined but never used. Allowed unused args must match /^_/u
(useGetDashboards as jest.Mock).mockReturnValue({
dashboards: [],
loading: true,
});
};
1 change: 1 addition & 0 deletions tests/setupTests.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import "@testing-library/jest-dom";
import "jest-fetch-mock";

0 comments on commit e2e85f4

Please sign in to comment.