-
Notifications
You must be signed in to change notification settings - Fork 159
/
test.js
192 lines (163 loc) · 5.96 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { createApp, h, nextTick } from 'vue'
import { resolve } from 'path'
import { readFileSync } from 'fs'
import BasicSrc from './components/BasicSrc.vue'
import TsSrc from './components/TsSrc.vue'
import Pug from './components/Pug.vue'
import Coffee from './components/Coffee.vue'
import Basic from './components/Basic.vue'
import ClassComponent from './components/ClassComponent.vue'
import ClassComponentWithMixin from './components/ClassComponentWithMixin.vue'
import ClassComponentProperty from './components/ClassComponentProperty.vue'
import TypeScript from './components/TypeScript.vue'
import jestVue from '../../../'
import RenderFunction from './components/RenderFunction.vue'
import FunctionalSFC from './components/FunctionalSFC.vue'
import CoffeeScript from './components/CoffeeScript.vue'
import FunctionalSFCParent from './components/FunctionalSFCParent.vue'
import NoScript from './components/NoScript.vue'
import PugRelative from './components/PugRelativeExtends.vue'
import { randomExport } from './components/NamedExport.vue'
import ScriptSetup from './components/ScriptSetup.vue'
import FunctionalRenderFn from './components/FunctionalRenderFn.vue'
// TODO: JSX for Vue 3? TSX?
import Jsx from './components/Jsx.vue'
function mount(Component, props, slots) {
document.getElementsByTagName('html')[0].innerHTML = ''
const el = document.createElement('div')
el.id = 'app'
document.body.appendChild(el)
const Parent = {
render() {
return h(Component, props, slots)
}
}
createApp(Parent).mount(el)
}
test('supports <script setup>', () => {
mount(ScriptSetup)
expect(document.body.outerHTML).toContain('Count: 5')
expect(document.body.outerHTML).toContain('Welcome to Your Vue.js App')
})
test('processes .vue files', () => {
mount(Basic)
expect(document.querySelector('h1').textContent).toBe(
'Welcome to Your Vue.js App'
)
})
test('processes .vue files with js src attributes', () => {
mount(BasicSrc)
expect(document.querySelector('h1').textContent).toBe(
'Welcome to Your Vue.js App'
)
})
test('processes .vue files with ts src attributes', () => {
mount(TsSrc)
expect(document.querySelector('h1').textContent).toBe(
'Welcome to Your Vue.js App'
)
})
test('handles named exports', () => {
expect(randomExport).toEqual(42)
})
test('generates source maps for .vue files', () => {
const filePath = resolve(__dirname, './components/Basic.vue')
const fileString = readFileSync(filePath, { encoding: 'utf8' })
const config = {
moduleFileExtensions: ['js', 'vue']
}
const { code } = jestVue.process(fileString, filePath, { config })
expect(code).toMatchSnapshot()
})
test('generates source maps using src attributes', () => {
const filePath = resolve(__dirname, './components/SourceMapsSrc.vue')
const fileString = readFileSync(filePath, { encoding: 'utf8' })
const config = {
moduleFileExtensions: ['js', 'vue']
}
const { code } = jestVue.process(fileString, filePath, { config })
expect(code).toMatchSnapshot()
})
test('processes .vue file with lang set to coffee', () => {
mount(Coffee)
expect(document.querySelector('h1').textContent).toBe('Coffee')
})
test('processes .vue file with lang set to coffeescript', () => {
mount(CoffeeScript)
expect(document.querySelector('h1').textContent).toBe('CoffeeScript')
})
test('processes SFC with no template', () => {
mount(RenderFunction, {}, { default: () => h('div', { id: 'slot' }) })
expect(document.querySelector('#slot')).toBeTruthy()
})
test('processes .vue files with lang set to typescript', () => {
mount(TypeScript)
expect(document.querySelector('#parent').textContent).toBe('Parent')
expect(document.querySelector('#child').textContent).toBe('Child')
})
test('handles missing script block', () => {
mount(NoScript)
expect(document.querySelector('.footer').textContent).toBe("I'm footer!")
})
test('processes pug templates', () => {
mount(Pug)
expect(document.querySelector('.pug-base')).toBeTruthy()
expect(document.querySelector('.pug-extended')).toBeTruthy()
})
test('supports relative paths when extending templates from .pug files', () => {
mount(PugRelative)
expect(document.querySelector('.pug-relative-base')).toBeTruthy()
})
test('supports class component .vue files', () => {
expect.assertions(3)
mount(ClassComponent, { msg: 'Props Message' })
expect(document.querySelector('[data-computed]').textContent).toBe(
'Message: Props Message'
)
expect(document.querySelector('[data-props]').textContent).toBe(
'Props Message'
)
const event = new window.Event('click')
document.querySelector('button').dispatchEvent(event)
nextTick().then(() => {
expect(document.querySelector('[data-methods]').textContent).toBe('Updated')
})
})
test('supports class component .vue files with mixins', () => {
expect.assertions(1)
mount(ClassComponentWithMixin)
expect(document.querySelector('[data-mixin]').textContent).toBe(
'Hello world!'
)
})
test('supports class component .vue files using vue-property-decorator', () => {
expect.assertions(2)
mount(ClassComponentProperty, { msg: 'Props Message' })
expect(document.querySelector('[data-computed]').textContent).toBe(
'Message: Hello'
)
expect(document.querySelector('[data-props]').textContent).toBe(
'Props Message'
)
})
// TODO: How do functional components work in Vue 3?
xtest('processes functional components', () => {
// const clickSpy = jest.fn()
mount(FunctionalSFC)
})
// TODO: How do functional components work in Vue 3?
xtest('processes SFC with functional template from parent', () => {
mount(FunctionalSFCParent)
expect(document.querySelector('div').textContent).toBe('foo')
})
// TODO: JSX in Vue 3?
xtest('processes .vue file using jsx', () => {
mount(Jsx)
expect(document.querySelector('#jsx')).toBeTruthy()
})
test('processes functional component exported as function', () => {
mount(FunctionalRenderFn)
const elem = document.querySelector('#functional-render-fn')
expect(elem).toBeTruthy()
expect(elem.innerHTML).toBe('Nyan')
})