-
-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathcomponentProps.spec.ts
600 lines (543 loc) · 13.2 KB
/
componentProps.spec.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
// NOTE: This test is implemented based on the case of `runtime-core/__test__/componentProps.spec.ts`.
// NOTE: not supported
// mixins
// caching
import { type FunctionalComponent, setCurrentInstance } from '../src/component'
import {
children,
defineComponent,
getCurrentInstance,
nextTick,
ref,
render,
setText,
template,
watchEffect,
} from '../src'
let host: HTMLElement
const initHost = () => {
host = document.createElement('div')
host.setAttribute('id', 'host')
document.body.appendChild(host)
}
beforeEach(() => initHost())
afterEach(() => host.remove())
describe('component props (vapor)', () => {
test('stateful', () => {
let props: any
// TODO: attrs
const Comp = defineComponent({
props: ['fooBar', 'barBaz'],
render() {
const instance = getCurrentInstance()!
props = instance.props
},
})
render(
Comp,
{
get fooBar() {
return 1
},
},
host,
)
expect(props.fooBar).toEqual(1)
// test passing kebab-case and resolving to camelCase
render(
Comp,
{
get ['foo-bar']() {
return 2
},
},
host,
)
expect(props.fooBar).toEqual(2)
// test updating kebab-case should not delete it (#955)
render(
Comp,
{
get ['foo-bar']() {
return 3
},
get barBaz() {
return 5
},
},
host,
)
expect(props.fooBar).toEqual(3)
expect(props.barBaz).toEqual(5)
render(Comp, {}, host)
expect(props.fooBar).toBeUndefined()
expect(props.barBaz).toBeUndefined()
// expect(props.qux).toEqual(5) // TODO: attrs
})
test.todo('stateful with setup', () => {
// TODO:
})
test('functional with declaration', () => {
let props: any
// TODO: attrs
const Comp: FunctionalComponent = defineComponent((_props: any) => {
const instance = getCurrentInstance()!
props = instance.props
return {}
})
Comp.props = ['foo']
Comp.render = (() => {}) as any
render(
Comp,
{
get foo() {
return 1
},
},
host,
)
expect(props.foo).toEqual(1)
render(
Comp,
{
get foo() {
return 2
},
},
host,
)
expect(props.foo).toEqual(2)
render(Comp, {}, host)
expect(props.foo).toBeUndefined()
})
test('functional without declaration', () => {
let props: any
// TODO: attrs
const Comp: FunctionalComponent = defineComponent((_props: any) => {
const instance = getCurrentInstance()!
props = instance.props
return {}
})
Comp.props = undefined as any
Comp.render = (() => {}) as any
render(
Comp,
{
get foo() {
return 1
},
},
host,
)
expect(props.foo).toBeUndefined()
render(
Comp,
{
get foo() {
return 2
},
},
host,
)
expect(props.foo).toBeUndefined()
})
test('boolean casting', () => {
let props: any
const Comp = defineComponent({
props: {
foo: Boolean,
bar: Boolean,
baz: Boolean,
qux: Boolean,
},
render() {
const instance = getCurrentInstance()!
props = instance.props
},
})
render(
Comp,
{
// absent should cast to false
bar: '', // empty string should cast to true
baz: 'baz', // same string should cast to true
qux: 'ok', // other values should be left in-tact (but raise warning)
},
host,
)
expect(props.foo).toBe(false)
expect(props.bar).toBe(true)
expect(props.baz).toBe(true)
expect(props.qux).toBe('ok')
})
test('default value', () => {
let props: any
const defaultFn = vi.fn(() => ({ a: 1 }))
const defaultBaz = vi.fn(() => ({ b: 1 }))
const Comp = defineComponent({
props: {
foo: {
default: 1,
},
bar: {
default: defaultFn,
},
baz: {
type: Function,
default: defaultBaz,
},
},
render() {
const instance = getCurrentInstance()!
props = instance.props
},
})
render(
Comp,
{
get foo() {
return 2
},
},
host,
)
expect(props.foo).toBe(2)
// const prevBar = props.bar
props.bar
expect(props.bar).toEqual({ a: 1 })
expect(props.baz).toEqual(defaultBaz)
// expect(defaultFn).toHaveBeenCalledTimes(1) // failed: (caching is not supported)
expect(defaultFn).toHaveBeenCalledTimes(3)
expect(defaultBaz).toHaveBeenCalledTimes(0)
// #999: updates should not cause default factory of unchanged prop to be
// called again
render(
Comp,
{
get foo() {
return 3
},
},
host,
)
expect(props.foo).toBe(3)
expect(props.bar).toEqual({ a: 1 })
// expect(props.bar).toBe(prevBar) // failed: (caching is not supported)
// expect(defaultFn).toHaveBeenCalledTimes(1) // failed: caching is not supported (called 3 times)
render(
Comp,
{
get bar() {
return { b: 2 }
},
},
host,
)
expect(props.foo).toBe(1)
expect(props.bar).toEqual({ b: 2 })
// expect(defaultFn).toHaveBeenCalledTimes(1) // failed: caching is not supported (called 3 times)
render(
Comp,
{
get foo() {
return 3
},
get bar() {
return { b: 3 }
},
},
host,
)
expect(props.foo).toBe(3)
expect(props.bar).toEqual({ b: 3 })
// expect(defaultFn).toHaveBeenCalledTimes(1) // failed: caching is not supported (called 3 times)
render(
Comp,
{
get bar() {
return { b: 4 }
},
},
host,
)
expect(props.foo).toBe(1)
expect(props.bar).toEqual({ b: 4 })
// expect(defaultFn).toHaveBeenCalledTimes(1) // failed: caching is not supported (called 3 times)
})
test.todo('using inject in default value factory', () => {
// TODO: impl inject
})
// NOTE: maybe it's unnecessary
// https://github.com/vuejs/core-vapor/pull/99#discussion_r1472647377
test('optimized props updates', async () => {
const Child = defineComponent({
props: ['foo'],
render() {
const instance = getCurrentInstance()!
const t0 = template('<div><!></div>')
const n0 = t0()
const {
0: [n1],
} = children(n0)
watchEffect(() => {
setText(n1, instance.props.foo)
})
return n0
},
})
const foo = ref(1)
const id = ref('a')
const Comp = defineComponent({
setup() {
return { foo, id }
},
render(_ctx: Record<string, any>) {
const t0 = template('')
const n0 = t0()
render(
Child,
{
get foo() {
return _ctx.foo
},
get id() {
return _ctx.id
},
},
n0 as any, // TODO: type
)
return n0
},
})
const instace = render(Comp, {}, host)
const reset = setCurrentInstance(instace)
// expect(host.innerHTML).toBe('<div id="a">1</div>') // TODO: Fallthrough Attributes
expect(host.innerHTML).toBe('<div>1</div>')
foo.value++
await nextTick()
// expect(host.innerHTML).toBe('<div id="a">2</div>') // TODO: Fallthrough Attributes
expect(host.innerHTML).toBe('<div>2</div>')
// id.value = 'b'
// await nextTick()
// expect(host.innerHTML).toBe('<div id="b">2</div>') // TODO: Fallthrough Attributes
reset()
})
describe('validator', () => {
test('validator should be called with two arguments', () => {
let args: any
const mockFn = vi.fn((..._args: any[]) => {
args = _args
return true
})
const Comp = defineComponent({
props: {
foo: {
type: Number,
validator: (value: any, props: any) => mockFn(value, props),
},
bar: {
type: Number,
},
},
render() {
const t0 = template('<div/>')
const n0 = t0()
return n0
},
})
const props = {
get foo() {
return 1
},
get bar() {
return 2
},
}
render(Comp, props, host)
expect(mockFn).toHaveBeenCalled()
// NOTE: Vapor Component props defined by getter. So, `props` not Equal to `{ foo: 1, bar: 2 }`
// expect(mockFn).toHaveBeenCalledWith(1, { foo: 1, bar: 2 })
expect(args.length).toBe(2)
expect(args[0]).toBe(1)
expect(args[1].foo).toEqual(1)
expect(args[1].bar).toEqual(2)
})
// TODO: impl setter and warnner
test.todo(
'validator should not be able to mutate other props',
async () => {
const mockFn = vi.fn((...args: any[]) => true)
const Comp = defineComponent({
props: {
foo: {
type: Number,
validator: (value: any, props: any) => !!(props.bar = 1),
},
bar: {
type: Number,
validator: (value: any) => mockFn(value),
},
},
render() {
const t0 = template('<div/>')
const n0 = t0()
return n0
},
})
render(
Comp,
{
get foo() {
return 1
},
get bar() {
return 2
},
},
host,
)
expect(
`Set operation on key "bar" failed: target is readonly.`,
).toHaveBeenWarnedLast()
expect(mockFn).toHaveBeenCalledWith(2)
},
)
})
test.todo('warn props mutation', () => {
// TODO: impl warn
})
test('warn absent required props', () => {
const Comp = defineComponent({
props: {
bool: { type: Boolean, required: true },
str: { type: String, required: true },
num: { type: Number, required: true },
},
setup() {
return () => null
},
})
render(Comp, {}, host)
expect(`Missing required prop: "bool"`).toHaveBeenWarned()
expect(`Missing required prop: "str"`).toHaveBeenWarned()
expect(`Missing required prop: "num"`).toHaveBeenWarned()
})
// NOTE: type check is not supported in vapor
// test('warn on type mismatch', () => {})
// #3495
test('should not warn required props using kebab-case', async () => {
const Comp = defineComponent({
props: {
fooBar: { type: String, required: true },
},
setup() {
return () => null
},
})
render(
Comp,
{
get ['foo-bar']() {
return 'hello'
},
},
host,
)
expect(`Missing required prop: "fooBar"`).not.toHaveBeenWarned()
})
test('props type support BigInt', () => {
const Comp = defineComponent({
props: {
foo: BigInt,
},
render() {
const instance = getCurrentInstance()!
const t0 = template('<div></div>')
const n0 = t0()
const {
0: [n1],
} = children(n0)
watchEffect(() => {
setText(n1, instance.props.foo)
})
return n0
},
})
render(
Comp,
{
get foo() {
return (
BigInt(BigInt(100000111)) + BigInt(2000000000) * BigInt(30000000)
)
},
},
'#host',
)
expect(host.innerHTML).toBe('<div>60000000100000111</div>')
})
// #3288
test.todo(
'declared prop key should be present even if not passed',
async () => {
// let initialKeys: string[] = []
// const changeSpy = vi.fn()
// const passFoo = ref(false)
// const Comp = {
// props: ['foo'],
// setup() {
// const instance = getCurrentInstance()!
// initialKeys = Object.keys(instance.props)
// watchEffect(changeSpy)
// return {}
// },
// render() {
// return {}
// },
// }
// const Parent = createIf(
// () => passFoo.value,
// () => {
// return render(Comp , { foo: 1 }, host) // TODO: createComponent fn
// },
// )
// // expect(changeSpy).toHaveBeenCalledTimes(1)
},
)
// #3371
test.todo(`avoid double-setting props when casting`, async () => {
// TODO: proide, slots
})
test('support null in required + multiple-type declarations', () => {
const Comp = defineComponent({
props: {
foo: { type: [Function, null], required: true },
},
render() {},
})
expect(() => {
render(Comp, { foo: () => {} }, host)
}).not.toThrow()
expect(() => {
render(Comp, { foo: null }, host)
}).not.toThrow()
})
// #5016
test.todo('handling attr with undefined value', () => {
// TODO: attrs
})
// #6915
test('should not mutate original props long-form definition object', () => {
const props = {
msg: {
type: String,
},
}
const Comp = defineComponent({
props,
render() {},
})
render(Comp, { msg: 'test' }, host)
expect(Object.keys(props.msg).length).toBe(1)
})
})