Skip to content

Commit

Permalink
fix: do not execute <PrismicToolbar>'s script during Happy DOM tests (
Browse files Browse the repository at this point in the history
#151)

* fix: do not execute `<PrismicToolbar>`'s script during happy-dom tests

* chore: fix Happy DOM spelling [skip ci]

* test: add test
  • Loading branch information
angeloashmore authored May 25, 2022
1 parent add20dd commit b4a36a4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/PrismicToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ export const PrismicToolbar = ({
script.dataset.repositoryName = repositoryName;
script.dataset.type = type;

// Disable Happy DOM `<script>` evaluation during
// tests.
//
// This is a patch ONLY INCLUDED DURING TESTS. It will
// be pruned during code minification in non-test
// environments.
//
// @see https://github.com/capricorn86/happy-dom/blob/02ae081e36f990c06171eda44f9d885fd9413d73/packages/happy-dom/src/nodes/html-script-element/HTMLScriptElement.ts#L191-L209
if (process.env.NODE_ENV === "test") {
// @ts-expect-error - `_evaluateScript` is a Happy DOM-specific property.
script._evaluateScript = false;
}

document.body.appendChild(script);
}
}, [repositoryName, type, src]);
Expand Down
18 changes: 18 additions & 0 deletions test/PrismicToolbar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,21 @@ test('uses the legacy toolbar if type is set to "legacy"', (t) => {
t.fail("The toolbar script element was not found");
}
});

test("includes a Happy DOM patch to not execute scripts in test environments", (t) => {
const repositoryName = md5(t.title);

renderJSON(<PrismicToolbar repositoryName={repositoryName} type="legacy" />);

const script = getToolbarScript(repositoryName);

if (script instanceof HTMLScriptElement) {
t.is(
// @ts-expect-error - `_evaluateScript` is a Happy DOM-specific property.
script._evaluateScript,
false,
);
} else {
t.fail("The toolbar script element was not found");
}
});

0 comments on commit b4a36a4

Please sign in to comment.