100 lines
2.7 KiB
TypeScript
100 lines
2.7 KiB
TypeScript
import {
|
|
optionsToUrlParams,
|
|
withBrowserOpened,
|
|
withPageOpened,
|
|
} from "../browser";
|
|
|
|
const pageCloseMock = jest.fn();
|
|
|
|
const browserCloseMock = jest.fn();
|
|
|
|
const pageMock = {
|
|
close: pageCloseMock,
|
|
};
|
|
|
|
const browserMock = {
|
|
newPage: () => Promise.resolve(pageMock),
|
|
close: browserCloseMock,
|
|
};
|
|
|
|
jest.mock("puppeteer", () => {
|
|
return {
|
|
launch: () => Promise.resolve(browserMock),
|
|
};
|
|
});
|
|
|
|
describe("withPageOpened", () => {
|
|
it("should launch argument function once with page as argument", async () => {
|
|
const mockFunc = jest.fn();
|
|
|
|
await withPageOpened(mockFunc);
|
|
expect(mockFunc).toHaveBeenCalledTimes(1);
|
|
expect(mockFunc).toHaveBeenCalledWith(pageMock);
|
|
});
|
|
|
|
it("should throw on error if argument function throws", async () => {
|
|
const error = new Error("test error");
|
|
|
|
const mockFunc = jest.fn().mockRejectedValueOnce(error);
|
|
|
|
await expect(() => withPageOpened(mockFunc)).rejects.toThrow(error);
|
|
});
|
|
|
|
it("should close page before resolve", async () => {
|
|
await withPageOpened(() => Promise.resolve(undefined));
|
|
expect(pageCloseMock).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
|
|
describe("withBrowserOpen", () => {
|
|
it("should launch argument function with browser as argument", async () => {
|
|
const mockFunc = jest.fn();
|
|
|
|
await withBrowserOpened(mockFunc);
|
|
expect(mockFunc).toHaveBeenCalledTimes(1);
|
|
expect(mockFunc).toHaveBeenCalledWith(browserMock);
|
|
});
|
|
|
|
it("should close browser before resolve", async () => {
|
|
await withBrowserOpened(() => Promise.resolve(undefined));
|
|
expect(browserCloseMock).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("should throw on error if argument function throws", async () => {
|
|
const error = new Error("test error");
|
|
|
|
const mockFunc = jest.fn().mockRejectedValueOnce(error);
|
|
|
|
await expect(() => withBrowserOpened(mockFunc)).rejects.toThrow(error);
|
|
});
|
|
});
|
|
describe("optionsToUrlParams", () => {
|
|
it("should return correct string", async () => {
|
|
expect(
|
|
optionsToUrlParams({ test: 1, truc: "2", machin: true }, [
|
|
"test",
|
|
"truc",
|
|
"machin",
|
|
])
|
|
).toEqual("test=1&truc=2&machin=true");
|
|
});
|
|
it("should hide value against the second param", async () => {
|
|
expect(
|
|
optionsToUrlParams({ test: 1, truc: "2", machin: true }, ["test", "truc"])
|
|
).toEqual("test=1&truc=2");
|
|
});
|
|
it("should repeat value if it is an array", async () => {
|
|
expect(optionsToUrlParams({ truc: ["2", "4", "5"] }, ["truc"])).toEqual(
|
|
"truc=2&truc=4&truc=5"
|
|
);
|
|
});
|
|
it("should", async () => {
|
|
expect(
|
|
optionsToUrlParams({ test: 1, truc: "2", machin: true }, [
|
|
"bidule" as "machin",
|
|
"truc",
|
|
])
|
|
).toEqual("truc=2");
|
|
});
|
|
});
|