Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Chanuka-ChandraYapa committed Oct 6, 2023
1 parent 92e8fc4 commit f25e390
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 8 deletions.
61 changes: 61 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"react-leaflet": "^4.2.1",
"react-loading": "^2.0.3",
"react-paginate": "^8.2.0",
"react-redux": "^8.1.3",
"react-router-dom": "6.11.0",
"react-scripts": "5.0.1",
"react-table": "7.8.0",
Expand Down
132 changes: 132 additions & 0 deletions src/__test__/InputURL.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import React from "react";
import { render, fireEvent, waitFor, screen } from "@testing-library/react";
import InputURL from "layouts/upload/inputURL";
import { BrowserRouter as Router } from "react-router-dom";
import { MaterialUIControllerProvider } from "context";
import UserProvider from "utils/userContext";
import { ThemeProvider } from "@mui/material";
import theme from "assets/theme";

// Mock the useNavigate function
jest.mock("react-router-dom", () => ({
...jest.requireActual("react-router-dom"),
useNavigate: jest.fn(),
}));

describe("InputURL Component Tests", () => {
it("renders InputURL component without crashing", () => {
render(
<Router>
<MaterialUIControllerProvider>
<UserProvider>
<ThemeProvider theme={theme}>
<InputURL />
</ThemeProvider>
</UserProvider>
</MaterialUIControllerProvider>
</Router>
);

expect(screen.getByText("Enter URL to be analyzed")).toBeInTheDocument();
expect(
screen.getByRole("textbox", {
name: "Enter URL to be analyzed Enter URL",
})
).toBeInTheDocument();
expect(screen.getByText("Analyze")).toBeInTheDocument();
});

it("allows user to enter a URL", () => {
render(
<Router>
<MaterialUIControllerProvider>
<UserProvider>
<ThemeProvider theme={theme}>
<InputURL />
</ThemeProvider>
</UserProvider>
</MaterialUIControllerProvider>
</Router>
);

const urlInput = screen.getByRole("textbox", {
name: "Enter URL to be analyzed Enter URL",
});
fireEvent.change(urlInput, { target: { value: "https://example.com" } });
expect(urlInput).toHaveValue("https://example.com");
});

it("submits a URL for analysis and displays results", async () => {
render(
<Router>
<MaterialUIControllerProvider>
<UserProvider>
<ThemeProvider theme={theme}>
<InputURL />
</ThemeProvider>
</UserProvider>
</MaterialUIControllerProvider>
</Router>
);

const urlInput = screen.getByRole("textbox", {
name: "Enter URL to be analyzed Enter URL",
});
const analyzeButton = screen.getByText("Analyze");
fireEvent.change(urlInput, {
target: {
value:
"https://www.hitad.lk/en/ad/1776361-One-Acre-Coconut-Land-For-Sale-In-Kurunegala-Uhumeeya-Hanhamuna-Road?type=house-and-property",
},
});
fireEvent.click(analyzeButton);
expect(screen.queryByText("Analyzing...")).toBeTruthy();

// Wait for the loading state to disappear
await waitFor(
() => {
expect(screen.queryByText("Analyzing...")).toBeNull();
},
{ timeout: 1000 }
);

// Check if the analysis results are displayed
expect(
screen.findByText("One Acre Coconut Land For Sale In Kurunegala Uhumeeya, Hanhamuna Road")
).toBeInTheDocument();
// Add more assertions to check other parts of the results
});

// it("navigates to advertisement_map on 'View Locations' button click", async () => {
// const mockNavigate = jest.fn();
// jest.spyOn(require("react-router-dom"), "useNavigate").mockReturnValue(mockNavigate);

// render(
// <Router>
// <MaterialUIControllerProvider>
// <UserProvider>
// <ThemeProvider theme={theme}>
// <InputURL />
// </ThemeProvider>
// </UserProvider>
// </MaterialUIControllerProvider>
// </Router>
// );

// const urlInput = screen.getByLabelText("Enter URL");
// const analyzeButton = screen.getByText("Analyze");
// fireEvent.change(urlInput, { target: { value: "https://example.com" } });
// fireEvent.click(analyzeButton);

// // Wait for the loading state to disappear
// await waitFor(() => {
// expect(screen.queryByText("Analyzing...")).toBeNull();
// });

// const viewLocationsButton = screen.getByText("View Locations");
// fireEvent.click(viewLocationsButton);

// // Verify that useNavigate was called with the correct path
// expect(mockNavigate).toHaveBeenCalledWith("/advertisement_map?locations=");
// });
});
12 changes: 6 additions & 6 deletions src/__test__/Login.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ jest.mock("react-router-dom", () => ({
describe("Sign In Page", () => {
it("handles login correctly", () => {
render(
<UserProvider>
<MemoryRouter>
<MaterialUIControllerProvider>
<MemoryRouter>
<MaterialUIControllerProvider>
<UserProvider>
<ThemeProvider theme={theme}>
<Basic />
</ThemeProvider>
</MaterialUIControllerProvider>
</MemoryRouter>
</UserProvider>
</UserProvider>
</MaterialUIControllerProvider>
</MemoryRouter>
);

// Mock the fetch function to handle the login request
Expand Down
4 changes: 2 additions & 2 deletions src/layouts/upload/inputURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState, useEffect } from "react";
import { useNavigate } from "react-router-dom";
import { useAppState } from "utils/userContext";

import LinearProgress from "@mui/material/LinearProgress";
// import LinearProgress from "@mui/material/LinearProgress";
import MDBox from "components/MDBox";
import MDButton from "components/MDButton";
import MDTypography from "components/MDTypography";
Expand Down Expand Up @@ -117,7 +117,7 @@ function InputURL() {
{loading && (
<div>
<p>Analyzing...</p>
<LinearProgress />
{/* <LinearProgress /> */}
<Loading />
</div>
)}
Expand Down

0 comments on commit f25e390

Please sign in to comment.