Skip to content

Commit

Permalink
Modify the testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
Shubh942 committed Jul 20, 2024
1 parent 77dd6c9 commit 02d0263
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 37 deletions.
44 changes: 25 additions & 19 deletions webapp/src/App.test.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// App.test.jsx
import React from "react";
import { render, screen, fireEvent } from "@testing-library/react";
import { render, screen } from "@testing-library/react";
import { MemoryRouter } from "react-router-dom";
import App from "./App.jsx";

// App.jsx
import { DataProvider } from "./context/DataContext"; // Import the DataProvider

test("renders Footer component", () => {
render(
<MemoryRouter>
<App />
<DataProvider>
<App />
</DataProvider>
</MemoryRouter>
);
expect(
Expand All @@ -20,58 +20,64 @@ test("renders Footer component", () => {
test("renders Threads component within Debug route", () => {
render(
<MemoryRouter initialEntries={["/debug/threads"]}>
<App />
<DataProvider>
<App />
</DataProvider>
</MemoryRouter>
);
expect(screen.getByText(/Threads/i)).toBeInTheDocument();
const threadsComponent = screen.getByText(/Threads/i);
expect(threadsComponent).toBeInTheDocument();
expect(threadsComponent).toHaveClass("gdb-header-content active");
// Add any other checks specific to the Threads component
});

test("renders LocalVariable component within Debug route", () => {
render(
<MemoryRouter initialEntries={["/debug/localVariable"]}>
<App />
<DataProvider>
<App />
</DataProvider>
</MemoryRouter>
);
expect(screen.getByText(/Local Variable/i)).toBeInTheDocument();
const localVariableComponent = screen.getByText(/Local Variable/i);
expect(localVariableComponent).toBeInTheDocument();
expect(localVariableComponent).toHaveClass("gdb-header-content active");
// Add any other checks specific to the LocalVariable component
});

test("renders Context component within Debug route", () => {
render(
<MemoryRouter initialEntries={["/debug/context"]}>
<App />
<DataProvider>
<App />
</DataProvider>
</MemoryRouter>
);
expect(screen.getByText(/Context/i)).toBeInTheDocument();
const contextComponent = screen.getByText(/Context/i);
expect(contextComponent).toBeInTheDocument();
expect(contextComponent).toHaveClass("gdb-header-content active");
// Add any other checks specific to the Context component
});

test("renders MemoryMap component within Debug route", () => {
render(
<MemoryRouter initialEntries={["/debug/memoryMap"]}>
<App />
<DataProvider>
<App />
</DataProvider>
</MemoryRouter>
);
expect(screen.getByText(/Memory Map/i)).toBeInTheDocument();
const memoryMapComponent = screen.getByText(/Memory Map/i);
expect(memoryMapComponent).toBeInTheDocument();
expect(memoryMapComponent).toHaveClass("gdb-header-content active");
// Add any other checks specific to the MemoryMap component
});

test("renders BreakPoints component within Debug route", () => {
render(
<MemoryRouter initialEntries={["/debug/breakPoints"]}>
<App />
<DataProvider>
<App />
</DataProvider>
</MemoryRouter>
);
const breakpointsComponent = screen.getByText(/Break Points/i);
expect(breakpointsComponent).toBeInTheDocument();
expect(breakpointsComponent).toHaveClass("gdb-header-content active");
// Add any other checks specific to the BreakPoints component
});
4 changes: 2 additions & 2 deletions webapp/src/components/DebugHeader/DebugHeader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import {
import { IoReload } from "react-icons/io5";
import { MdSkipNext, MdSkipPrevious } from "react-icons/md";
import { BsArrowRightSquareFill } from "react-icons/bs";
import { DataContext } from "../../context/DataContext";
import { DataState } from "../../context/DataContext";

import "./DebugHeader.css";

const DebugHeader = () => {
const { refresh, setRefresh } = useContext(DataContext);
const { refresh, setRefresh } = DataState();

return (
<div className="parent-debug-header">
Expand Down
46 changes: 38 additions & 8 deletions webapp/src/components/DebugHeader/__tests__/DebugHeader.test.jsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,56 @@
// DebugHeader.test.jsx

import React from "react";
import { render, screen, fireEvent } from "@testing-library/react";
import DebugHeader from "../DebugHeader.jsx";
import { DataContext } from "../../../context/DataContext.jsx";

describe("DebugHeader Component", () => {
test("renders DebugHeader component with icons and filename", async () => {
render(<DebugHeader />);
test("renders DebugHeader component with icons and filename", () => {
render(
<DataContext.Provider value={{ refresh: false, setRefresh: vi.fn() }}>
<DebugHeader />
</DataContext.Provider>
);

const filenameContent = screen.getByText(/filename/i);
expect(filenameContent).toBeInTheDocument();

const saveContent = screen.getByRole("button");
const saveContent = screen.getByRole("button", { name: /save/i });
expect(saveContent).toBeInTheDocument();
});

test("clicking Save button triggers save action", () => {
render(<DebugHeader />);
const mockSetRefresh = vi.fn();

render(
<DataContext.Provider
value={{ refresh: false, setRefresh: mockSetRefresh }}
>
<DebugHeader />
</DataContext.Provider>
);

const saveButton = screen.getByRole("button", { name: /save/i });
expect(saveButton).toBeInTheDocument();

fireEvent.click(saveButton);
expect(mockSetRefresh).toHaveBeenCalledWith(true);
});

test("clicking Save button when refresh is true shows 'Saving..'", () => {
const mockSetRefresh = vi.fn();

render(
<DataContext.Provider
value={{ refresh: true, setRefresh: mockSetRefresh }}
>
<DebugHeader />
</DataContext.Provider>
);

const saveButton = screen.getByText(/Save/i);
const saveButton = screen.getByRole("button", { name: /saving\.\./i });
expect(saveButton).toBeInTheDocument();

// fireEvent.click(saveButton);
fireEvent.click(saveButton);
expect(mockSetRefresh).toHaveBeenCalledWith(false);
});
});
30 changes: 26 additions & 4 deletions webapp/src/context/DataContext.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import React, { createContext, useState, useEffect, useCallback } from "react";
import React, {
createContext,
useState,
useEffect,
useCallback,
useContext,
} from "react";
import axios from "axios";

export const DataContext = createContext();
Expand All @@ -11,20 +17,36 @@ export const DataProvider = ({ children }) => {
const fetchData = useCallback(async () => {
if (refresh) {
try {
// Your data fetching logic here
const stackResponse = await axios.get("/api/stack");
const functionsResponse = await axios.get("/api/functions");

setStack(stackResponse.data);
setFunctions(functionsResponse.data);

// Reset refresh after data is fetched
setRefresh(false);
} catch (error) {
console.error("Error fetching data:", error);
setRefresh(!refresh);
// Optional: set refresh to false on error as well
setRefresh(false);
}
}
});
}, [refresh]);

useEffect(() => {
fetchData();
}, [refresh]);
}, [fetchData]);

return (
<DataContext.Provider value={{ refresh, setRefresh, stack, functions }}>
{children}
</DataContext.Provider>
);
};

export const DataState = () => {
return useContext(DataContext);
};

export default DataProvider;
8 changes: 4 additions & 4 deletions webapp/src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import App from "./App";
import "./index.css";

ReactDOM.render(
<DataProvider>
<BrowserRouter>
<BrowserRouter>
<DataProvider>
<React.StrictMode>
<App />
</React.StrictMode>
</BrowserRouter>
</DataProvider>,
</DataProvider>
</BrowserRouter>,
document.getElementById("root")
);

0 comments on commit 02d0263

Please sign in to comment.