-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
/
Copy pathvue-test.ts
285 lines (263 loc) · 5.46 KB
/
vue-test.ts
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import Vue, { VNode, defineComponent } from '../index'
import { ComponentOptions } from '../options'
class Test extends Vue {
a: number = 0
testProperties() {
this.$data
this.$el
this.$options
this.$parent
this.$root
this.$children
this.$refs
this.$slots
this.$isServer
this.$ssrContext
this.$vnode
this.$root.$children[0].$children[0]
}
// test property reification
$el!: HTMLElement | SVGElement
$refs!: {
vue: Vue
element: HTMLInputElement
vues: Vue[]
elements: HTMLInputElement[]
}
testReification() {
this.$refs.vue.$data
this.$refs.element.value
this.$refs.vues[0].$data
this.$refs.elements[0].value
}
testMethods() {
this.$mount('#app', false)
this.$forceUpdate()
this.$destroy()
this.$set({}, 'key', 'value')
this.$delete({}, 'key')
this.$watch('a', (val: number, oldVal: number) => {}, {
immediate: true,
deep: false
})()
this.$watch(
() => this.a,
(val: number) => {}
)
this.$on('', () => {})
this.$once('', () => {})
this.$off('', () => {})
this.$emit('', 1, 2, 3)
this.$nextTick(function () {
this.$nextTick
})
this.$nextTick().then(() => {})
this.$createElement('div', {}, 'message')
}
static testConfig() {
const { config } = this
config.silent
config.optionMergeStrategies
config.devtools
config.errorHandler = (err, vm) => {
if (vm instanceof Test) {
vm.testProperties()
vm.testMethods()
}
}
config.warnHandler = (msg, vm) => {
if (vm instanceof Test) {
vm.testProperties()
vm.testMethods()
}
}
config.keyCodes = { esc: 27 }
config.ignoredElements = ['foo', /^ion-/]
config.async = false
}
static testMethods() {
this.extend({
data() {
return {
msg: ''
}
}
})
this.nextTick(() => {})
this.nextTick(
function () {
console.log(this.text === 'test')
},
{ text: 'test' }
)
this.nextTick().then(() => {})
this.set({}, '', '')
this.set({}, 1, '')
this.set([true, false, true], 1, true)
this.delete({}, '')
this.delete({}, 1)
this.delete([true, false], 0)
this.directive('', { bind() {} })
this.filter('', (value: number) => value)
this.component('', { data: () => ({}) })
this.component('', {
functional: true,
render(h) {
return h('div', 'hello!')
}
})
this.use
this.mixin(Test)
this.compile('<div>{{ message }}</div>')
this.use(() => {})
.use(() => {})
.mixin({})
.mixin({})
}
}
const HelloWorldComponent = Vue.extend({
props: ['name'],
data() {
return {
message: 'Hello ' + this.name
}
},
computed: {
shouted(): string {
return this.message.toUpperCase()
}
},
methods: {
getMoreExcited() {
this.message += '!'
}
},
watch: {
message(a: string) {
console.log(`Message ${this.message} was changed!`)
}
}
})
const FunctionalHelloWorldComponent = Vue.extend({
functional: true,
props: ['name'],
render(createElement, ctxt) {
return createElement('div', 'Hello ' + ctxt.props.name)
}
})
const FunctionalScopedSlotsComponent = Vue.extend({
functional: true,
render(h, ctx) {
return (
(ctx.scopedSlots.default && ctx.scopedSlots.default({})) ||
h('div', 'functional scoped slots')
)
}
})
const Parent = Vue.extend({
data() {
return { greeting: 'Hello' }
}
})
const Child = Parent.extend({
methods: {
foo() {
console.log(this.greeting.toLowerCase())
}
}
})
const GrandChild = Child.extend({
computed: {
lower(): string {
return this.greeting.toLowerCase()
}
}
})
new GrandChild().lower.toUpperCase()
for (let _ in new Test().$options) {
}
declare const options: ComponentOptions<Vue>
Vue.extend(options)
Vue.component('test-comp', options)
new Vue(options)
// cyclic example
Vue.extend({
props: {
bar: {
type: String
}
},
methods: {
foo() {}
},
mounted() {
this.foo()
},
// manual annotation
render(h): VNode {
const a = this.bar
return h('canvas', {}, [a])
}
})
declare function decorate<VC extends typeof Vue>(v: VC): VC
@decorate
class Decorated extends Vue {
a = 123
}
const obj = Vue.observable({ a: 1 })
obj.a++
// VNodeData style tests.
const ComponentWithStyleInVNodeData = Vue.extend({
render(h) {
const elementWithStyleAsString = h('div', {
style: '--theme-color: black;'
})
const elementWithStyleCSSProperties = h('div', {
style: { ['--theme-color' as any]: 'black' }
})
const elementWithStyleAsArrayOfStyleValues = h('div', {
style: [{ ['--theme-color' as any]: 'black' }]
})
return h('div', undefined, [
elementWithStyleAsString,
elementWithStyleCSSProperties,
elementWithStyleAsArrayOfStyleValues
])
}
})
// infer mixin type with new Vue() #12730
new Vue({
mixins: [
defineComponent({
props: {
p1: String,
p2: {
type: Number,
default: 0
}
},
data() {
return {
foo: 123
}
},
computed: {
bar() {
return 123
}
}
}),
{
methods: {
hello(n: number) {}
}
}
],
created() {
this.hello(this.foo)
this.hello(this.bar)
// @ts-expect-error
this.hello(this.p1)
this.hello(this.p2)
}
})