Skip to content

Commit

Permalink
feat: add browser context to server tests, improve errors
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Oct 11, 2019
1 parent 4921936 commit e94c651
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/__tests__/render.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@ import LegacyCounter from "./fixtures/legacy-counter";
import Clickable from "./fixtures/clickable.marko";
import HelloName from "./fixtures/hello-name.marko";

test("renders static content in a document without a browser context", async () => {
test("renders static content in a document with a browser context", async () => {
const { getByText } = await render(Counter);
expect(getByText("Value: 0")).toHaveProperty(
["ownerDocument", "defaultView"],
null
);
expect(
expect(getByText("Value: 0")).toHaveProperty([
"ownerDocument",
"defaultView"
])
).not.toBeNull();
});

test("renders static content from a Marko 3 component", async () => {
const { getByText } = await render(LegacyCounter);
expect(getByText("Value: 0")).toHaveProperty(
["ownerDocument", "defaultView"],
null
);
expect(
expect(getByText("Value: 0")).toHaveProperty([
"ownerDocument",
"defaultView"
])
).not.toBeNull();
});

test("fails when rerendering", async () => {
Expand All @@ -41,6 +45,6 @@ test("fails when emitting events", async () => {
await expect(
fireEvent.click(getByText("Increment"))
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Unable to find the \\"window\\" object for the given node. fireEvent currently supports firing events on DOM nodes, document, and window. Please file an issue with the code that's causing you to see this error: https://github.com/testing-library/dom-testing-library/issues/new"`
`"Cannot fire events when testing on the server side. Please use @marko/testing-library in a browser environment."`
);
});
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ export async function render<T extends Template>(
)
)
);

const {
window: { document }
} = new JSDOM();
const container = JSDOM.fragment(html);
document.adoptNode(container);
(container as any).outerHTML = html; // Fixes prettyDOM for container

return {
Expand Down
10 changes: 10 additions & 0 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ export type FireObject = {
};

export const fireEvent = (async (...params) => {
failIfNoWindow();
originalFireEvent(...params);
await wait();
}) as FireFunction & FireObject;

Object.keys(originalFireEvent).forEach((eventName: EventType) => {
const fire = originalFireEvent[eventName];
fireEvent[eventName] = async (...params) => {
failIfNoWindow();
fire(...params);

// TODO: this waits for a possible update using setTimeout(0) which should
Expand All @@ -66,3 +68,11 @@ export type AsyncReturnValue<
> = Parameters<
NonNullable<Parameters<ReturnType<AsyncFunction>["then"]>[0]>
>[0];

function failIfNoWindow() {
if (typeof window === "undefined") {
throw new Error(
"Cannot fire events when testing on the server side. Please use @marko/testing-library in a browser environment."
);
}
}

0 comments on commit e94c651

Please sign in to comment.