Skip to content

Commit

Permalink
test: use fixtures and snapshot files for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Sep 17, 2022
1 parent 3117d9f commit fc80d47
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 95 deletions.
6 changes: 5 additions & 1 deletion .lintstagedrc.cjs
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
module.exports = require('@1stg/lint-staged/tsc')
module.exports = {
...require('@1stg/lint-staged/tsc'),
'*.{gif,jpeg,jpg,png,svg,webp}': [],
'test/fixtures/**/*.svg': 'prettier --write',
}
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.type-coverage
.vercel
coverage
dist
lib
/pnpm-lock.yaml
2 changes: 2 additions & 0 deletions .stylelintignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ LICENSE
*.json
*.log
*.patch
*.snap
*.svg
*.yaml
*.yml
43 changes: 43 additions & 0 deletions test/__snapshots__/fixtures.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Vitest Snapshot v1

exports[`fixtures > html 1`] = `
"<html lang=\\"en\\"><head>
<meta charset=\\"UTF-8\\">
<meta http-equiv=\\"X-UA-Compatible\\" content=\\"IE=edge\\">
<meta name=\\"viewport\\" content=\\"width=device-width, initial-scale=1.0\\">
<title>Document</title>
</head>
<body>
<img src=\\"/whatever-error\\">
</body></html>"
`;
exports[`fixtures > svg 1`] = `
"<svg>
<text>Hello World!</text>
</svg>"
`;
exports[`fixtures > svg 2`] = `
"<svg width=\\"24\\" height=\\"16\\" viewBox=\\"0 0 24 16\\" xmlns=\\"http://www.w3.org/2000/svg\\">
<g fill=\\"none\\" fill-rule=\\"evenodd\\">
<image/>
<g transform=\\"translate(3 4)\\" fill-rule=\\"nonzero\\">
11111
<path d=\\"M4.657 3.658 1.472 7.843a.86.86 0 0 1-1.219 0 .86.86 0 0 1 0-1.218L2.83 4.048.253 1.472a.86.86 0 1 1 1.22-1.22L4.656 3.44a.86.86 0 0 1 0 1.219Z\\" fill=\\"#66AFF9\\"/>
<path d=\\"M8.657 4.658 5.472 7.843a.86.86 0 0 1-1.219 0 .86.86 0 0 1 0-1.218L6.83 4.048 4.253 1.472a.86.86 0 1 1 1.22-1.22L8.656 3.44a.86.86 0 0 1 0 1.219Z\\" fill=\\"#007AF5\\"/>
</g>
</g>
</svg>"
`;
exports[`fixtures > svg 3`] = `
"<svg class=\\"icon\\" viewBox=\\"0 0 1024 1024\\" xmlns=\\"http://www.w3.org/2000/svg\\" width=\\"200\\" height=\\"200\\">
<path d=\\"m650.112 266.667 262.06 148.01c25.824 14.448 44.254 43.287 44.254 75.768v295.961c0 28.897-18.43 57.736-44.255 75.768l-262.116 147.953c-11.092 7.28-29.58 10.864-44.312 10.864-14.79 0-29.522-3.64-44.255-10.807l-262.116-148.01a87.6 87.6 0 0 1-44.255-75.768V490.445c0-32.424 14.79-61.32 44.255-75.769l262.116-148.009a96.132 96.132 0 0 1 88.624 0zM765.186 785.61l-318.829 2.105 131.855 93.174 186.974-95.279zm-187.6-288.226-143.174 270.65 139.477-24.574 3.697-246.076zm37.827-101.82-1.365 345.108 214.904 25.313-213.539-370.364z\\"/>
<path d=\\"m500.453 153.868 31.854 55.177-312.116 181.684a54.21 54.21 0 0 0-24.688 27.19c-4.209 10.75-5.688 20.478-4.55 29.351h.398v382.481a102.39 102.39 0 0 0 4.664 24.517c4.096 11.831 13.254 22.525 27.418 32.138l146.36 85.496c10.011 15.642 11.263 28.84 3.868 39.533-7.452 10.694-21.445 14.505-41.98 11.377l-162.514-93.288c-11.32-8.248-21.16-21.104-29.58-38.453-8.418-17.293-12.343-37.713-11.831-61.32h-.17V447.953h-.171a142.89 142.89 0 0 1 9.67-55.29c6.541-16.212 17.235-32.082 32.082-47.554L500.453 153.81zM602.67 0l136.69 104.665-101.764.227v3.47c-1.023 13.823-9.67 28.783-25.824 44.937-18.487 18.544-45.052 36.86-79.693 55.006l-31.457-54.323c29.466-13.595 49.773-26.906 60.979-39.989a54.835 54.835 0 0 0 6.2-8.873l-103.47.17L602.672 0z\\"/>
</svg>"
`;
29 changes: 29 additions & 0 deletions test/fixtures.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

import { sanitize, sanitizeSvg } from 'domiso'

const fixtures = path.resolve(fileURLToPath(import.meta.url), '../fixtures')

describe('fixtures', () => {
test('html', async () => {
const htmlFixtures = path.resolve(fixtures, 'html')
const files = await fs.readdir(htmlFixtures)
for (const file of files) {
expect(
sanitize(await fs.readFile(path.resolve(htmlFixtures, file), 'utf8')),
).toMatchSnapshot()
}
})

test('svg', async () => {
const svgFixtures = path.resolve(fixtures, 'svg')
const files = await fs.readdir(svgFixtures)
for (const file of files) {
expect(
sanitizeSvg(await fs.readFile(path.resolve(svgFixtures, file), 'utf8')),
).toMatchSnapshot()
}
})
})
25 changes: 25 additions & 0 deletions test/fixtures/html/basic.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
http-equiv="X-UA-Compatible"
content="IE=edge"
/>
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<title>Document</title>
</head>
<body>
<img
src="/whatever-error"
onload="alert('onload xss')"
onerror="alert('onerror xss')"
/>
<script>
console.log('XSS')
</script>
</body>
</html>
4 changes: 4 additions & 0 deletions test/fixtures/svg/basic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions test/fixtures/svg/image-href.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions test/fixtures/svg/xml.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 0 additions & 43 deletions test/html.spec.ts

This file was deleted.

51 changes: 0 additions & 51 deletions test/svg.spec.ts

This file was deleted.

0 comments on commit fc80d47

Please sign in to comment.