1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Updating topbar tests

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev 2021-08-30 09:52:42 +03:00
parent ff2d302b21
commit 1bcadafac9
2 changed files with 78 additions and 8 deletions

View File

@ -20,11 +20,46 @@
*/ */
import React from "react"; import React from "react";
import { render } from "@testing-library/react"; import { render, fireEvent } from "@testing-library/react";
import "@testing-library/jest-dom/extend-expect"; import "@testing-library/jest-dom/extend-expect";
import { TopBar } from "../topbar"; import { TopBar } from "../topbar";
import { TopBarRegistry } from "../../../../extensions/registries"; import { TopBarRegistry } from "../../../../extensions/registries";
jest.mock(
"electron",
() => ({
ipcRenderer: {
on: jest.fn(
(channel: string, listener: (event: any, ...args: any[]) => void) => {
if (channel === "history:can-go-back") {
listener({}, true);
}
if (channel === "history:can-go-forward") {
listener({}, true);
}
}
),
},
})
);
const goBack = jest.fn();
const goForward = jest.fn();
jest.mock("@electron/remote", () => {
return {
webContents: {
getFocusedWebContents: () => {
return {
goBack,
goForward
};
}
}
};
});
describe("<TopBar/>", () => { describe("<TopBar/>", () => {
beforeEach(() => { beforeEach(() => {
TopBarRegistry.createInstance(); TopBarRegistry.createInstance();
@ -35,15 +70,38 @@ describe("<TopBar/>", () => {
}); });
it("renders w/o errors", () => { it("renders w/o errors", () => {
const { container } = render(<TopBar label="test bar" />); const { container } = render(<TopBar/>);
expect(container).toBeInstanceOf(HTMLElement); expect(container).toBeInstanceOf(HTMLElement);
}); });
it("renders title", async () => { it("renders history arrows", async () => {
const { getByTestId } = render(<TopBar label="topbar" />); const { getByTestId } = render(<TopBar/>);
expect(await getByTestId("topbarLabel")).toHaveTextContent("topbar"); expect(await getByTestId("history-back")).toBeInTheDocument();
expect(await getByTestId("history-forward")).toBeInTheDocument();
});
it("enables arrow by ipc event", async () => {
const { getByTestId } = render(<TopBar/>);
expect(await getByTestId("history-back")).not.toHaveClass("disabled");
expect(await getByTestId("history-forward")).not.toHaveClass("disabled");
});
it("triggers browser history back and forward", async () => {
const { getByTestId } = render(<TopBar/>);
const prevButton = await getByTestId("history-back");
const nextButton = await getByTestId("history-forward");
fireEvent.click(prevButton);
expect(goBack).toBeCalled();
fireEvent.click(nextButton);
expect(goForward).toBeCalled();
}); });
it("renders items", async () => { it("renders items", async () => {
@ -58,7 +116,7 @@ describe("<TopBar/>", () => {
} }
]); ]);
const { getByTestId } = render(<TopBar label="topbar" />); const { getByTestId } = render(<TopBar/>);
expect(await getByTestId(testId)).toHaveTextContent(text); expect(await getByTestId(testId)).toHaveTextContent(text);
}); });

View File

@ -85,8 +85,20 @@ export const TopBar = observer(({ children, ...rest }: Props) => {
return ( return (
<div className={styles.topBar} {...rest}> <div className={styles.topBar} {...rest}>
<div className={styles.history}> <div className={styles.history}>
<Icon svg="flat_arrow" className={styles.prevArrow} onClick={goBack} disabled={!prevEnabled.get()}/> <Icon
<Icon svg="flat_arrow" className="ml-5" onClick={goForward} disabled={!nextEnabled.get()}/> data-testid="history-back"
svg="flat_arrow"
className={styles.prevArrow}
onClick={goBack}
disabled={!prevEnabled.get()}
/>
<Icon
data-testid="history-forward"
svg="flat_arrow"
className="ml-5"
onClick={goForward}
disabled={!nextEnabled.get()}
/>
</div> </div>
<div className={styles.controls}> <div className={styles.controls}>
{renderRegisteredItems()} {renderRegisteredItems()}