Created
May 17, 2024 07:05
-
-
Save helabenkhalfallah/6616ad01d544e1c1ed64f879c6e5e485 to your computer and use it in GitHub Desktop.
JestHelper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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