Skip to content

Commit b78f99b

Browse files
Mihai PlamadealaLwveMike
Mihai Plamadeala
authored andcommitted
test: add tests
1 parent 48f9bf3 commit b78f99b

File tree

5 files changed

+175
-46
lines changed

5 files changed

+175
-46
lines changed

package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"bumpp": "^9.2.0",
5555
"eslint": "^8.51.0",
5656
"esno": "^0.17.0",
57+
"happy-dom": "^12.9.1",
5758
"lint-staged": "^14.0.1",
5859
"pnpm": "^8.8.0",
5960
"rimraf": "^5.0.5",
@@ -64,8 +65,11 @@
6465
"vitest": "^0.34.6"
6566
},
6667
"peerDependencies": {
67-
"vue": "3.x",
68-
"@vue/compiler-core": "3.x"
68+
"@vue/compiler-core": "3.x",
69+
"vue": "3.x"
70+
},
71+
"optionalDependencies": {
72+
"@vue/compiler-sfc": "^3.3.6"
6973
},
7074
"simple-git-hooks": {
7175
"pre-commit": "pnpm lint-staged"

pnpm-lock.yaml

+64-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

+3-26
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,21 @@ const NODE_TYPES = {
1010
SIMPLE_EXPRESSION: 4,
1111
} as const
1212

13-
export type AttributeToBeRemoved =
14-
| string
15-
| { type: 'static' | 'dynamic'; value: string }
16-
17-
export default function createAttributesRemover(attributes: AttributeToBeRemoved[]): NodeTransform {
13+
export default function createAttributesRemover(attributes: string[]): NodeTransform {
1814
if (!attributes.length)
1915
return () => undefined
2016

21-
const [staticAttributes, dynamicAttributes]: [string[], string[]] = [[], []]
22-
23-
attributes
24-
.forEach((attribute) => {
25-
if (typeof attribute === 'string') {
26-
staticAttributes.push(attribute)
27-
dynamicAttributes.push(attribute)
28-
29-
return
30-
}
31-
32-
if (attribute.type === 'static') {
33-
staticAttributes.push(attribute.value)
34-
return
35-
}
36-
37-
dynamicAttributes.push(attribute.value)
38-
})
39-
4017
return (node): void => {
4118
if (node.type !== NODE_TYPES.ELEMENT)
4219
return
4320

4421
node.props = node.props
4522
.filter((prop) => {
4623
if (prop.type === NODE_TYPES.ATTRIBUTE)
47-
return !staticAttributes.includes(prop.name)
24+
return !attributes.includes(prop.name)
4825

4926
if (prop.type === NODE_TYPES.DIRECTIVE && prop.arg?.type === NODE_TYPES.SIMPLE_EXPRESSION)
50-
return !dynamicAttributes.includes(prop.arg.content)
27+
return !attributes.includes(prop.arg.content)
5128

5229
return true
5330
})

test/__snapshots__/index.test.ts.snap

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`createAttributesRemover > empty attributes > should not change anything 1`] = `
4+
"import { createElementVNode as _createElementVNode, openBlock as _openBlock, createElementBlock as _createElementBlock } from \\"vue\\"
5+
6+
const _hoisted_1 = /*#__PURE__*/_createElementVNode(\\"div\\", null, [
7+
/*#__PURE__*/_createElementVNode(\\"header\\", { \\"data-test\\": \\"header-test\\" }, \\" Header \\"),
8+
/*#__PURE__*/_createElementVNode(\\"section\\", { \\"data-test\\": 'section-content' }, [
9+
/*#__PURE__*/_createElementVNode(\\"ul\\", null, [
10+
/*#__PURE__*/_createElementVNode(\\"li\\", {
11+
\\"data-test\\": 'item-text',
12+
textContent: 'text'
13+
})
14+
]),
15+
/*#__PURE__*/_createElementVNode(\\"div\\", { \\"data-meta\\": \\"not-secure-meta\\" }, \\" Meta \\")
16+
]),
17+
/*#__PURE__*/_createElementVNode(\\"footer\\", { dataTest: \\"footer-test\\" }, \\" Footer \\")
18+
], -1 /* HOISTED */)
19+
const _hoisted_2 = [
20+
_hoisted_1
21+
]
22+
23+
export function render(_ctx, _cache) {
24+
return (_openBlock(), _createElementBlock(\\"template\\", null, _hoisted_2))
25+
}"
26+
`;
27+
28+
exports[`createAttributesRemover > with attributes > should remove all data-test and data-meta 1`] = `
29+
"import { createElementVNode as _createElementVNode, openBlock as _openBlock, createElementBlock as _createElementBlock } from \\"vue\\"
30+
31+
const _hoisted_1 = /*#__PURE__*/_createElementVNode(\\"div\\", null, [
32+
/*#__PURE__*/_createElementVNode(\\"header\\", null, \\" Header \\"),
33+
/*#__PURE__*/_createElementVNode(\\"section\\", null, [
34+
/*#__PURE__*/_createElementVNode(\\"ul\\", null, [
35+
/*#__PURE__*/_createElementVNode(\\"li\\", { textContent: 'text' })
36+
]),
37+
/*#__PURE__*/_createElementVNode(\\"div\\", null, \\" Meta \\")
38+
]),
39+
/*#__PURE__*/_createElementVNode(\\"footer\\", { dataTest: \\"footer-test\\" }, \\" Footer \\")
40+
], -1 /* HOISTED */)
41+
const _hoisted_2 = [
42+
_hoisted_1
43+
]
44+
45+
export function render(_ctx, _cache) {
46+
return (_openBlock(), _createElementBlock(\\"template\\", null, _hoisted_2))
47+
}"
48+
`;

test/index.test.ts

+54-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,55 @@
1-
import { describe } from 'vitest'
1+
import { describe, expect, it } from 'vitest'
2+
import { compileTemplate } from '@vue/compiler-sfc'
3+
import type { NodeTransform } from '@vue/compiler-core'
4+
import createAttributesRemover from '../src'
25

3-
describe.skip('vue-plugin-remove-attributes')
6+
describe('createAttributesRemover', () => {
7+
const template
8+
= `<template>
9+
<div>
10+
<header data-test="header-test">
11+
Header
12+
</header>
13+
<section :data-test="'section-content'">
14+
<ul>
15+
<li
16+
:data-test="'item-text'"
17+
v-text="'text'"
18+
/>
19+
</ul>
20+
<div data-meta="not-secure-meta">
21+
Meta
22+
</div>
23+
</section>
24+
<footer dataTest="footer-test">
25+
Footer
26+
</footer>
27+
</div>
28+
</template>`
29+
30+
const parse = (removeParameters: NodeTransform) => {
31+
return compileTemplate({
32+
id: 'TestComponent.vue',
33+
filename: 'TestComponent.vue',
34+
source: template,
35+
compilerOptions: {
36+
nodeTransforms: [removeParameters],
37+
},
38+
}).code
39+
}
40+
41+
describe('empty attributes', () => {
42+
it('should not change anything', () => {
43+
const code = parse(createAttributesRemover([]))
44+
45+
expect(code).toMatchSnapshot()
46+
})
47+
})
48+
describe('with attributes', () => {
49+
it('should remove all data-test and data-meta', () => {
50+
const code = parse(createAttributesRemover(['data-test', 'data-meta']))
51+
52+
expect(code).toMatchSnapshot()
53+
})
54+
})
55+
})

0 commit comments

Comments
 (0)