Skip to content

Commit d4ce89d

Browse files
committed
test(getComponentSlotsFromTypeDefineTypes): add basic tests
1 parent f58b4d9 commit d4ce89d

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* Test for getComponentSlotsFromTypeDefineTypes
3+
*/
4+
'use strict'
5+
6+
const path = require('path')
7+
const fs = require('fs')
8+
const Linter = require('../../../../eslint-compat').Linter
9+
const parser = require('vue-eslint-parser')
10+
const tsParser = require('@typescript-eslint/parser')
11+
const utils = require('../../../../../lib/utils/index')
12+
const assert = require('assert')
13+
14+
const FIXTURES_ROOT = path.resolve(
15+
__dirname,
16+
'../../../../fixtures/utils/ts-utils'
17+
)
18+
const TSCONFIG_PATH = path.resolve(FIXTURES_ROOT, './tsconfig.json')
19+
const SRC_TS_TEST_PATH = path.join(FIXTURES_ROOT, './src/test.ts')
20+
21+
function extractComponentSlots(code, tsFileCode) {
22+
const linter = new Linter()
23+
const result = []
24+
const config = {
25+
files: ['**/*.vue'],
26+
languageOptions: {
27+
parser,
28+
ecmaVersion: 2020,
29+
parserOptions: {
30+
parser: tsParser,
31+
project: [TSCONFIG_PATH],
32+
extraFileExtensions: ['.vue']
33+
}
34+
},
35+
plugins: {
36+
test: {
37+
rules: {
38+
test: {
39+
create(context) {
40+
return utils.defineScriptSetupVisitor(context, {
41+
onDefineSlotsEnter(_node, slots) {
42+
result.push(
43+
...slots.map((prop) => ({
44+
type: prop.type,
45+
name: prop.slotName
46+
}))
47+
)
48+
}
49+
})
50+
}
51+
}
52+
}
53+
}
54+
},
55+
rules: {
56+
'test/test': 'error'
57+
}
58+
}
59+
fs.writeFileSync(SRC_TS_TEST_PATH, tsFileCode || '', 'utf8')
60+
// clean './src/test.ts' cache
61+
tsParser.clearCaches()
62+
assert.deepStrictEqual(
63+
linter.verify(code, config, path.join(FIXTURES_ROOT, './src/test.vue')),
64+
[]
65+
)
66+
// reset
67+
fs.writeFileSync(SRC_TS_TEST_PATH, '', 'utf8')
68+
return result
69+
}
70+
71+
describe('getComponentSlotsFromTypeDefineTypes', () => {
72+
for (const { scriptCode, tsFileCode, slots: expected } of [
73+
{
74+
scriptCode: `
75+
defineSlots<{
76+
default(props: { msg: string }): any
77+
}>()
78+
`,
79+
slots: [{ type: 'type', name: 'default' }]
80+
},
81+
{
82+
scriptCode: `
83+
interface Slots {
84+
default(props: { msg: string }): any
85+
}
86+
defineSlots<Slots>()
87+
`,
88+
slots: [{ type: 'type', name: 'default' }]
89+
},
90+
{
91+
scriptCode: `
92+
type Slots = {
93+
default(props: { msg: string }): any
94+
}
95+
defineSlots<Slots>()
96+
`,
97+
slots: [{ type: 'type', name: 'default' }]
98+
}
99+
]) {
100+
const code = `
101+
<script setup lang="ts">
102+
${scriptCode}
103+
</script>
104+
`
105+
it(`should return expected slots with :${code}`, () => {
106+
const slots = extractComponentSlots(code, tsFileCode)
107+
108+
assert.deepStrictEqual(
109+
slots,
110+
expected,
111+
`\n${JSON.stringify(slots)}\n === \n${JSON.stringify(expected)}`
112+
)
113+
})
114+
}
115+
})

0 commit comments

Comments
 (0)