|
1 | | -import { type Ref, nextTick, onUpdated, ref } from '@vue/runtime-dom' |
| 1 | +import { |
| 2 | + type Ref, |
| 3 | + nextTick, |
| 4 | + onUpdated, |
| 5 | + ref, |
| 6 | + withModifiers, |
| 7 | +} from '@vue/runtime-dom' |
2 | 8 | import { |
3 | 9 | VaporTeleport, |
4 | 10 | createComponent, |
@@ -356,133 +362,128 @@ describe('attribute fallthrough', () => { |
356 | 362 | expect(`Extraneous non-props attributes (class)`).toHaveBeenWarned() |
357 | 363 | }) |
358 | 364 |
|
359 | | - // it('should dedupe same listeners when $attrs is used during render', () => { |
360 | | - // const click = vi.fn() |
361 | | - // const count = ref(0) |
362 | | - |
363 | | - // function inc() { |
364 | | - // count.value++ |
365 | | - // click() |
366 | | - // } |
367 | | - |
368 | | - // const Parent = { |
369 | | - // render() { |
370 | | - // return h(Child, { onClick: inc }) |
371 | | - // }, |
372 | | - // } |
373 | | - |
374 | | - // const Child = defineVaporComponent({ |
375 | | - // render() { |
376 | | - // return h( |
377 | | - // 'div', |
378 | | - // mergeProps( |
379 | | - // { |
380 | | - // onClick: withModifiers(() => {}, ['prevent', 'stop']), |
381 | | - // }, |
382 | | - // this.$attrs, |
383 | | - // ), |
384 | | - // ) |
385 | | - // }, |
386 | | - // }) |
387 | | - |
388 | | - // const root = document.createElement('div') |
389 | | - // document.body.appendChild(root) |
390 | | - // render(h(Parent), root) |
391 | | - |
392 | | - // const node = root.children[0] as HTMLElement |
393 | | - // node.dispatchEvent(new CustomEvent('click')) |
394 | | - // expect(click).toHaveBeenCalledTimes(1) |
395 | | - // expect(count.value).toBe(1) |
396 | | - // }) |
397 | | - |
398 | | - // it('should not warn when $attrs is used during render', () => { |
399 | | - // const Parent = { |
400 | | - // render() { |
401 | | - // return h(Child, { foo: 1, class: 'parent', onBar: () => {} }) |
402 | | - // }, |
403 | | - // } |
| 365 | + it('should dedupe same listeners when $attrs is used during render', () => { |
| 366 | + const click = vi.fn() |
| 367 | + const count = ref(0) |
404 | 368 |
|
405 | | - // const Child = defineVaporComponent({ |
406 | | - // props: ['foo'], |
407 | | - // render() { |
408 | | - // return [h('div'), h('div', this.$attrs)] |
409 | | - // }, |
410 | | - // }) |
| 369 | + function inc() { |
| 370 | + count.value++ |
| 371 | + click() |
| 372 | + } |
411 | 373 |
|
412 | | - // const root = document.createElement('div') |
413 | | - // document.body.appendChild(root) |
414 | | - // render(h(Parent), root) |
| 374 | + const Parent = { |
| 375 | + render() { |
| 376 | + return createComponent(Child, { onClick: () => inc }) |
| 377 | + }, |
| 378 | + } |
415 | 379 |
|
416 | | - // expect(`Extraneous non-props attributes`).not.toHaveBeenWarned() |
417 | | - // expect(`Extraneous non-emits event listeners`).not.toHaveBeenWarned() |
| 380 | + const Child = defineVaporComponent({ |
| 381 | + setup(_, { attrs }) { |
| 382 | + const n0 = template('<div></div>', true)() as any |
| 383 | + n0.$evtclick = withModifiers(() => {}, ['prevent', 'stop']) |
| 384 | + renderEffect(() => setDynamicProps(n0, [attrs])) |
| 385 | + return n0 |
| 386 | + }, |
| 387 | + }) |
418 | 388 |
|
419 | | - // expect(root.innerHTML).toBe(`<div></div><div class="parent"></div>`) |
420 | | - // }) |
| 389 | + const { host } = define(Parent).render() |
| 390 | + const node = host.children[0] as HTMLElement |
| 391 | + node.dispatchEvent(new CustomEvent('click')) |
| 392 | + expect(click).toHaveBeenCalledTimes(1) |
| 393 | + expect(count.value).toBe(1) |
| 394 | + }) |
421 | 395 |
|
422 | | - // it('should not warn when context.attrs is used during render', () => { |
423 | | - // const Parent = { |
424 | | - // render() { |
425 | | - // return h(Child, { foo: 1, class: 'parent', onBar: () => {} }) |
426 | | - // }, |
427 | | - // } |
| 396 | + it('should not warn when context.attrs is used during render', () => { |
| 397 | + const Parent = { |
| 398 | + render() { |
| 399 | + return createComponent(Child, { |
| 400 | + foo: () => 1, |
| 401 | + class: () => 'parent', |
| 402 | + onBar: () => () => {}, |
| 403 | + }) |
| 404 | + }, |
| 405 | + } |
428 | 406 |
|
429 | | - // const Child = defineVaporComponent({ |
430 | | - // props: ['foo'], |
431 | | - // setup(_props, { attrs }) { |
432 | | - // return () => [h('div'), h('div', attrs)] |
433 | | - // }, |
434 | | - // }) |
| 407 | + const Child = defineVaporComponent({ |
| 408 | + props: ['foo'], |
| 409 | + render(_ctx, $props, $emit, $attrs, $slots) { |
| 410 | + const n0 = template('<div></div>')() as Element |
| 411 | + const n1 = template('<div></div>')() as Element |
| 412 | + renderEffect(() => { |
| 413 | + setDynamicProps(n1, [$attrs]) |
| 414 | + }) |
| 415 | + return [n0, n1] |
| 416 | + }, |
| 417 | + }) |
435 | 418 |
|
436 | | - // const root = document.createElement('div') |
437 | | - // document.body.appendChild(root) |
438 | | - // render(h(Parent), root) |
| 419 | + const { html } = define(Parent).render() |
439 | 420 |
|
440 | | - // expect(`Extraneous non-props attributes`).not.toHaveBeenWarned() |
441 | | - // expect(`Extraneous non-emits event listeners`).not.toHaveBeenWarned() |
| 421 | + expect(`Extraneous non-props attributes`).not.toHaveBeenWarned() |
| 422 | + expect(`Extraneous non-emits event listeners`).not.toHaveBeenWarned() |
442 | 423 |
|
443 | | - // expect(root.innerHTML).toBe(`<div></div><div class="parent"></div>`) |
444 | | - // }) |
| 424 | + expect(html()).toBe(`<div></div><div class="parent"></div>`) |
| 425 | + }) |
445 | 426 |
|
446 | | - // it('should not warn when context.attrs is used during render (functional)', () => { |
447 | | - // const Parent = { |
448 | | - // render() { |
449 | | - // return h(Child, { foo: 1, class: 'parent', onBar: () => {} }) |
450 | | - // }, |
451 | | - // } |
| 427 | + it('should not warn when context.attrs is used during render (functional)', () => { |
| 428 | + const Parent = { |
| 429 | + render() { |
| 430 | + return createComponent(Child, { |
| 431 | + foo: () => 1, |
| 432 | + class: () => 'parent', |
| 433 | + onBar: () => () => {}, |
| 434 | + }) |
| 435 | + }, |
| 436 | + } |
452 | 437 |
|
453 | | - // const Child: FunctionalComponent = (_, { attrs }) => [ |
454 | | - // h('div'), |
455 | | - // h('div', attrs), |
456 | | - // ] |
| 438 | + const Child = defineVaporComponent((_, { attrs }) => { |
| 439 | + const n0 = template('<div></div>')() as Element |
| 440 | + const n1 = template('<div></div>')() as Element |
| 441 | + renderEffect(() => { |
| 442 | + setDynamicProps(n1, [attrs]) |
| 443 | + }) |
| 444 | + return [n0, n1] |
| 445 | + }) |
457 | 446 |
|
458 | | - // Child.props = ['foo'] |
| 447 | + Child.props = ['foo'] |
459 | 448 |
|
460 | | - // const root = document.createElement('div') |
461 | | - // document.body.appendChild(root) |
462 | | - // render(h(Parent), root) |
| 449 | + const { html } = define(Parent).render() |
463 | 450 |
|
464 | | - // expect(`Extraneous non-props attributes`).not.toHaveBeenWarned() |
465 | | - // expect(`Extraneous non-emits event listeners`).not.toHaveBeenWarned() |
466 | | - // expect(root.innerHTML).toBe(`<div></div><div class="parent"></div>`) |
467 | | - // }) |
| 451 | + expect(`Extraneous non-props attributes`).not.toHaveBeenWarned() |
| 452 | + expect(`Extraneous non-emits event listeners`).not.toHaveBeenWarned() |
| 453 | + expect(html()).toBe(`<div></div><div class="parent"></div>`) |
| 454 | + }) |
468 | 455 |
|
469 | | - // it('should not warn when functional component has optional props', () => { |
470 | | - // const Parent = { |
471 | | - // render() { |
472 | | - // return h(Child, { foo: 1, class: 'parent', onBar: () => {} }) |
473 | | - // }, |
474 | | - // } |
| 456 | + it.todo( |
| 457 | + 'should not warn when functional component has optional props', |
| 458 | + () => { |
| 459 | + const Parent = { |
| 460 | + render() { |
| 461 | + return createComponent(Child, { |
| 462 | + foo: () => 1, |
| 463 | + class: () => 'parent', |
| 464 | + onBar: () => () => {}, |
| 465 | + }) |
| 466 | + }, |
| 467 | + } |
475 | 468 |
|
476 | | - // const Child = (props: any) => [h('div'), h('div', { class: props.class })] |
| 469 | + const Child = defineVaporComponent((props: any) => { |
| 470 | + const n0 = template('<div></div>')() as Element |
| 471 | + const n1 = template('<div></div>')() as Element |
| 472 | + renderEffect(() => { |
| 473 | + setClass(n1, 'class', props.class) |
| 474 | + }) |
| 475 | + return [n0, n1] |
| 476 | + }) |
477 | 477 |
|
478 | | - // const root = document.createElement('div') |
479 | | - // document.body.appendChild(root) |
480 | | - // render(h(Parent), root) |
| 478 | + const root = document.createElement('div') |
| 479 | + document.body.appendChild(root) |
| 480 | + const { html } = define(Parent).render() |
481 | 481 |
|
482 | | - // expect(`Extraneous non-props attributes`).not.toHaveBeenWarned() |
483 | | - // expect(`Extraneous non-emits event listeners`).not.toHaveBeenWarned() |
484 | | - // expect(root.innerHTML).toBe(`<div></div><div class="parent"></div>`) |
485 | | - // }) |
| 482 | + expect(`Extraneous non-props attributes`).not.toHaveBeenWarned() |
| 483 | + expect(`Extraneous non-emits event listeners`).not.toHaveBeenWarned() |
| 484 | + expect(html()).toBe(`<div></div><div class="parent"></div>`) |
| 485 | + }, |
| 486 | + ) |
486 | 487 |
|
487 | 488 | // it('should warn when functional component has props and does not use attrs', () => { |
488 | 489 | // const Parent = { |
|
0 commit comments