Skip to content

Commit

Permalink
"Added new tests for Layout component, including auth branching cases…
Browse files Browse the repository at this point in the history
…, and updated existing tests for copying URL to clipboard"
  • Loading branch information
Somesh-Microsoft committed Oct 21, 2024
1 parent 597fddc commit ed1a8b7
Showing 1 changed file with 93 additions and 12 deletions.
105 changes: 93 additions & 12 deletions code/frontend/src/pages/layout/Layout.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {
import Layout from "./Layout";

import { BrowserRouter } from "react-router-dom";
import { getUserInfo } from "../../api/api";
import { before } from "lodash";
import { hostname } from "os";

const DefaultLayoutProps = {
children: <div> Layout Children </div>,
Expand All @@ -17,6 +20,19 @@ const DefaultLayoutProps = {
showHistoryPanel: true,
};

const DefaultLayoutPropsloderfalse = {
children: <div> Layout Children </div>,
toggleSpinner: false,
onSetShowHistoryPanel: jest.fn(),
showHistoryBtn: true,
showHistoryPanel: false,
};

jest.mock('../../api/api', () => ({
getUserInfo: jest.fn()
}));


describe("Layout Component", () => {
beforeAll(() => {
// Mocking navigator.clipboard
Expand All @@ -27,6 +43,8 @@ describe("Layout Component", () => {
});
});



/*
commented test case due to chat history feature code merging
test("renders Layout component", () => {
Expand All @@ -46,6 +64,38 @@ describe("Layout Component", () => {
});
*/

test('test the auth branching auth is true case', async () => {
const mocklist: any[] = [];
Object.defineProperty(window, "location", {
value: {
hostname: "NonDeloyed"
},
});
;(getUserInfo as jest.Mock).mockResolvedValue(mocklist)
await act(async () => {
render(
<BrowserRouter>
<Layout {...DefaultLayoutPropsloderfalse} />
</BrowserRouter>
);
});
expect(screen.getByText(/authentication not configured/i)).toBeInTheDocument();
});

test('test the auth branching auth is false case', async () => {
const mocklist: any[] = [1,2,3];
;(getUserInfo as jest.Mock).mockResolvedValue(mocklist)
await act(async () => {
render(
<BrowserRouter>
<Layout {...DefaultLayoutPropsloderfalse} />
</BrowserRouter>
);
});

expect(screen.getByText(/Show chat history/i)).toBeInTheDocument();
});

test("opens share panel", async () => {
await act(async () => {
render(
Expand Down Expand Up @@ -166,28 +216,59 @@ describe("Layout Component", () => {
expect(screen.getByText("Copied URL")).toBeInTheDocument();
});

/*
commented test case due to chat history feature code merging
test("copies URL to clipboard on other key", async () => {
render(
<BrowserRouter>
<Layout {...DefaultLayoutProps} />
</BrowserRouter>
);
test("copies URL to clipboard on space key", async () => {
await act(async () => {
render(
<BrowserRouter>
<Layout {...DefaultLayoutProps} />
</BrowserRouter>
);
});

const shareButton = screen.getByLabelText("Share");

fireEvent.click(shareButton);

const copyButton = screen.getByLabelText("Copy");

fireEvent.keyDown(copyButton, { key: "a", code: "KeyA" });
fireEvent.keyDown(copyButton, { key: ' ' });

// Wait for the state update

await waitFor(() => {
expect(screen.getByText("Copy URL")).toBeInTheDocument();
await screen.findByText("Copied URL");

expect(navigator.clipboard.writeText).toHaveBeenCalledWith(
window.location.href
);

expect(screen.getByText("Copied URL")).toBeInTheDocument();
});

test("copies URL to clipboard on A key", async () => {
await act(async () => {
render(
<BrowserRouter>
<Layout {...DefaultLayoutProps} />
</BrowserRouter>
);
});

const shareButton = screen.getByLabelText("Share");

fireEvent.click(shareButton);

const copyButton = screen.getByLabelText("Copy");

fireEvent.keyDown(copyButton, { key: "A" });

// Wait for the state update

await screen.findByText("Copy URL");

expect(navigator.clipboard.writeText).toHaveBeenCalledWith(
window.location.href
);

expect(screen.getByText("Copy URL")).toBeInTheDocument();
});
*/
});

0 comments on commit ed1a8b7

Please sign in to comment.