Skip to content

Instantly share code, notes, and snippets.

@helabenkhalfallah
Created May 17, 2024 07:05
Show Gist options
  • Save helabenkhalfallah/6616ad01d544e1c1ed64f879c6e5e485 to your computer and use it in GitHub Desktop.
Save helabenkhalfallah/6616ad01d544e1c1ed64f879c6e5e485 to your computer and use it in GitHub Desktop.
JestHelper
// eslint-disable-next-line import/no-extraneous-dependencies
import ReactDOMServer from 'react-dom/server';
import HtmlStringPrettifier from './HtmlStringPrettifier';
const isHtmlW3CCompliant = (w3cValidation) => w3cValidation && !w3cValidation.error;
const getHtmlW3CComplianceMessage = ({
html,
error,
}) => {
if (error) {
return `
The component html is not W3C compliant.
------------------------------------
Please check the below errors:
${error}
------------------------------------
Input HTML:
${html}.
------------------------------------
`;
}
return `
The component html is W3C compliant.
------------------------------------
Input HTML:
${html}.
------------------------------------
`;
};
const getHtmlPageWrapper = (component) => {
try {
const html = ReactDOMServer.renderToStaticMarkup(component);
const formattedHtml = HtmlStringPrettifier(html?.toString(), 'html');
return `
<!DOCTYPE html>
<html lang="en">
<head>
<title>Component W3C & A11y Compliance</title>
</head>
<body>
<main>
<h1>Component W3C & A11y Compliance</h1>
<div id="app-root">
${formattedHtml || ''}
</div>
</main>
</body>
</html>`;
} catch (e) {
return '';
}
};
const JestHelper = {
isHtmlW3CCompliant,
getHtmlW3CComplianceMessage,
getHtmlPageWrapper,
};
export default JestHelper;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment