From fc80d4778b32d43f05caffda9bfe5051c171028e Mon Sep 17 00:00:00 2001 From: JounQin Date: Fri, 16 Sep 2022 20:47:09 +0800 Subject: [PATCH] test: use fixtures and snapshot files for testing --- .lintstagedrc.cjs | 6 ++- .prettierignore | 3 ++ .stylelintignore | 2 + test/__snapshots__/fixtures.spec.ts.snap | 43 ++++++++++++++++++++ test/fixtures.spec.ts | 29 ++++++++++++++ test/fixtures/html/basic.html | 25 ++++++++++++ test/fixtures/svg/basic.svg | 4 ++ test/fixtures/svg/image-href.svg | 32 +++++++++++++++ test/fixtures/svg/xml.svg | 15 +++++++ test/html.spec.ts | 43 -------------------- test/svg.spec.ts | 51 ------------------------ 11 files changed, 158 insertions(+), 95 deletions(-) create mode 100644 test/__snapshots__/fixtures.spec.ts.snap create mode 100644 test/fixtures.spec.ts create mode 100644 test/fixtures/html/basic.html create mode 100644 test/fixtures/svg/basic.svg create mode 100644 test/fixtures/svg/image-href.svg create mode 100644 test/fixtures/svg/xml.svg delete mode 100644 test/html.spec.ts delete mode 100644 test/svg.spec.ts diff --git a/.lintstagedrc.cjs b/.lintstagedrc.cjs index 3f53038f..b58a6b2e 100644 --- a/.lintstagedrc.cjs +++ b/.lintstagedrc.cjs @@ -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', +} diff --git a/.prettierignore b/.prettierignore index d5b88cfa..aaa2ad02 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,3 +1,6 @@ +.type-coverage +.vercel coverage dist +lib /pnpm-lock.yaml diff --git a/.stylelintignore b/.stylelintignore index c7ea4910..59d6b214 100644 --- a/.stylelintignore +++ b/.stylelintignore @@ -5,5 +5,7 @@ LICENSE *.json *.log *.patch +*.snap +*.svg *.yaml *.yml diff --git a/test/__snapshots__/fixtures.spec.ts.snap b/test/__snapshots__/fixtures.spec.ts.snap new file mode 100644 index 00000000..1c9bf14c --- /dev/null +++ b/test/__snapshots__/fixtures.spec.ts.snap @@ -0,0 +1,43 @@ +// Vitest Snapshot v1 + +exports[`fixtures > html 1`] = ` +" + + + + Document + + + + + + +" +`; + +exports[`fixtures > svg 1`] = ` +" + Hello World! + +" +`; + +exports[`fixtures > svg 2`] = ` +" + + + + 11111 + + + + +" +`; + +exports[`fixtures > svg 3`] = ` +" + + +" +`; diff --git a/test/fixtures.spec.ts b/test/fixtures.spec.ts new file mode 100644 index 00000000..cce4a61f --- /dev/null +++ b/test/fixtures.spec.ts @@ -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() + } + }) +}) diff --git a/test/fixtures/html/basic.html b/test/fixtures/html/basic.html new file mode 100644 index 00000000..bace054f --- /dev/null +++ b/test/fixtures/html/basic.html @@ -0,0 +1,25 @@ + + + + + + + Document + + + + + + diff --git a/test/fixtures/svg/basic.svg b/test/fixtures/svg/basic.svg new file mode 100644 index 00000000..20ec2689 --- /dev/null +++ b/test/fixtures/svg/basic.svg @@ -0,0 +1,4 @@ + + Hello World! + + diff --git a/test/fixtures/svg/image-href.svg b/test/fixtures/svg/image-href.svg new file mode 100644 index 00000000..59c9b019 --- /dev/null +++ b/test/fixtures/svg/image-href.svg @@ -0,0 +1,32 @@ + + + + + 11111 + + + + + diff --git a/test/fixtures/svg/xml.svg b/test/fixtures/svg/xml.svg new file mode 100644 index 00000000..870863ef --- /dev/null +++ b/test/fixtures/svg/xml.svg @@ -0,0 +1,15 @@ + + + + diff --git a/test/html.spec.ts b/test/html.spec.ts deleted file mode 100644 index ee25f624..00000000 --- a/test/html.spec.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { sanitize } from 'domiso' - -test('Remove listeners and scripts', () => { - expect( - sanitize(/* HTML */ ` - - - - - - Document - - - - - - `), - ).toMatchInlineSnapshot(` - " - - - - Document - - - - - - " - `) -}) diff --git a/test/svg.spec.ts b/test/svg.spec.ts deleted file mode 100644 index 38f63cff..00000000 --- a/test/svg.spec.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { sanitizeSvg } from 'domiso' - -test('Remove listeners and scripts', () => { - expect( - sanitizeSvg(/* HTML */ ` - Hello World! - - `), - ).toMatchInlineSnapshot(` - " - Hello World! - - " - `) - - expect( - sanitizeSvg(/* HTML */ ` - - - - - `), - ).toMatchInlineSnapshot(` - " - - - - " - `) - - expect(sanitizeSvg('a')).toMatchInlineSnapshot('""') -})