Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added beforeRendering to screenshot and testing. #211

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 55 additions & 15 deletions src/screenshot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,32 @@ import handlebars from "handlebars";
import { makeScreenshot } from "./screenshot";
import { Screenshot } from "./models/Screenshot";

describe("beforeRendering", () => {
let page;
const buffer = Symbol("Buffer");

beforeEach(() => {
page = {
setContent: jest.fn(),
setDefaultTimeout: jest.fn(),
$: jest.fn(() => ({ screenshot: jest.fn(() => buffer) })),
};
});

it("should call beforeRendering with page and return screenshot with a buffer", async () => {
const beforeRendering = jest.fn();
const screenshot = await makeScreenshot(page, {
beforeRendering,
screenshot: new Screenshot({
html: "<html><body>Hello world!</body></html>",
}),
});

expect(beforeRendering).toHaveBeenCalledWith(page);
expect(screenshot.buffer).toEqual(buffer);
});
});

describe("beforeScreenshot", () => {
let page;
const buffer = Symbol("Buffer");
Expand Down Expand Up @@ -123,8 +149,7 @@ describe("handlebarsHelpers", () => {
}
});

const compactHtml = (htmlString) =>
htmlString.replace(/((^|\n)\s+)/gm, "");
const compactHtml = (htmlString) => htmlString.replace(/((^|\n)\s+)/gm, "");

describe("if no logic is given in the template", () => {
const html = "<html><body><h1>Hello world!</h1></body></html>";
Expand Down Expand Up @@ -160,7 +185,8 @@ describe("handlebarsHelpers", () => {
},
},
{
label: "all helpers are functions but content has not the sought variable",
label:
"all helpers are functions but content has not the sought variable",
options: {
content: { myOtherVar: "bar" },
handlebarsHelpers: {
Expand All @@ -174,17 +200,20 @@ describe("handlebarsHelpers", () => {
for (const test of cleanTests) {
it(`if no logic is given in the template, it should not throw error when ${test.label}`, async () => {
await expect(
makeScreenshot(page, { screenshot: new Screenshot(test.options), handlebarsHelpers: test.options.handlebarsHelpers })
makeScreenshot(page, {
screenshot: new Screenshot(test.options),
handlebarsHelpers: test.options.handlebarsHelpers,
})
).resolves.not.toThrow();
});

it(`if no logic is given in the template, it should render the original template when ${test.label}`, async () => {
const p = jest.fn(() => page);
await makeScreenshot(p(), { screenshot: new Screenshot(test.options), handlebarsHelpers: test.options.handlebarsHelpers });
expect(p().setContent).toHaveBeenCalledWith(
html,
expect.anything()
);
await makeScreenshot(p(), {
screenshot: new Screenshot(test.options),
handlebarsHelpers: test.options.handlebarsHelpers,
});
expect(p().setContent).toHaveBeenCalledWith(html, expect.anything());
});
}

Expand All @@ -198,7 +227,7 @@ describe("handlebarsHelpers", () => {
screenshot: new Screenshot({
content: { myVar: "foo" },
html: html,
})
}),
})
).rejects.toThrow(/Some helper is not a valid function/);
});
Expand Down Expand Up @@ -239,7 +268,8 @@ describe("handlebarsHelpers", () => {
error: /Missing helper: "equals"/,
},
{
label: "handlebarsHelpers is an object, but some helper is not a function",
label:
"handlebarsHelpers is an object, but some helper is not a function",
options: {
handlebarsHelpers: {
equals: (a, b) => a === b,
Expand All @@ -254,7 +284,10 @@ describe("handlebarsHelpers", () => {
for (const test of errorTests) {
it(`if logic is given in the template, it should throw error when ${test.label}`, async () => {
await expect(
makeScreenshot(page, { screenshot: new Screenshot(test.options), handlebarsHelpers: test.options.handlebarsHelpers })
makeScreenshot(page, {
screenshot: new Screenshot(test.options),
handlebarsHelpers: test.options.handlebarsHelpers,
})
).rejects.toThrow(test.error);
});
}
Expand Down Expand Up @@ -292,7 +325,8 @@ describe("handlebarsHelpers", () => {
expectedHtml: emptyHtml,
},
{
label: "all helpers are functions but content has not the sought variable",
label:
"all helpers are functions but content has not the sought variable",
options: {
content: { myOtherVar: "bar" },
handlebarsHelpers: {
Expand Down Expand Up @@ -336,13 +370,19 @@ describe("handlebarsHelpers", () => {
for (const test of validTests) {
it(`if logic is given in the template, it should not throw error when ${test.label}`, async () => {
await expect(
makeScreenshot(page, { screenshot: new Screenshot(test.options), handlebarsHelpers: test.options.handlebarsHelpers })
makeScreenshot(page, {
screenshot: new Screenshot(test.options),
handlebarsHelpers: test.options.handlebarsHelpers,
})
).resolves.not.toThrow();
});

it(`if logic is given in the template, it should render the expected template when ${test.label}`, async () => {
const p = jest.fn(() => page);
await makeScreenshot(p(), { screenshot: new Screenshot(test.options), handlebarsHelpers: test.options.handlebarsHelpers });
await makeScreenshot(p(), {
screenshot: new Screenshot(test.options),
handlebarsHelpers: test.options.handlebarsHelpers,
});
expect(p().setContent).toHaveBeenCalledWith(
test.expectedHtml,
expect.anything()
Expand Down
10 changes: 6 additions & 4 deletions src/screenshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export async function makeScreenshot(
page: Page,
{
screenshot,
beforeRendering,
beforeScreenshot,
waitUntil = "networkidle0",
timeout,
Expand All @@ -17,22 +18,23 @@ export async function makeScreenshot(
const hasHelpers = handlebarsHelpers && typeof handlebarsHelpers === "object";
if (hasHelpers) {
if (
Object.values(handlebarsHelpers).every(
(h) => typeof h === "function"
)
Object.values(handlebarsHelpers).every((h) => typeof h === "function")
) {
handlebars.registerHelper(handlebarsHelpers);
} else {
throw Error("Some helper is not a valid function");
}
}


if (screenshot?.content || hasHelpers) {
const template = compile(screenshot.html);
screenshot.setHTML(template(screenshot.content));
}

if (isFunction(beforeRendering)) {
await beforeRendering(page);
}

await page.setContent(screenshot.html, { waitUntil });
const element = await page.$(screenshot.selector);
if (!element) {
Expand Down
14 changes: 10 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { Page, PuppeteerLifeCycleEvent, PuppeteerNodeLaunchOptions } from "puppeteer";
import type {
Page,
PuppeteerLifeCycleEvent,
PuppeteerNodeLaunchOptions,
} from "puppeteer";
import type { Screenshot } from "./models/Screenshot";

export type Content = Array<{ output: string; selector?: string }> | object;
Expand All @@ -20,17 +24,19 @@ export interface Options extends ScreenshotParams {
puppeteerArgs?: PuppeteerNodeLaunchOptions;
// https://github.com/thomasdondorf/puppeteer-cluster/blob/b5b098aed84b8d2c170b3f9d0ac050f53582df45/src/Cluster.ts#L30
// eslint-disable-next-line @typescript-eslint/no-explicit-any
puppeteer?: any,
puppeteer?: any;
waitUntil?: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[];
beforeRendering?: (page: Page) => void;
beforeScreenshot?: (page: Page) => void;
timeout?: number
timeout?: number;
}

export interface MakeScreenshotParams {
screenshot: Screenshot;
waitUntil?: PuppeteerLifeCycleEvent | PuppeteerLifeCycleEvent[];
beforeRendering?: (page: Page) => void;
beforeScreenshot?: (page: Page) => void;
handlebarsHelpers?: { [helpers: string]: (...args: any[]) => any };

timeout?: number
timeout?: number;
}