|
1 | | -import { Fragment as Component, Fragment, component$, useSignal } from '@qwik.dev/core'; |
| 1 | +import { Fragment as Component, Fragment, Slot, component$, useSignal } from '@qwik.dev/core'; |
2 | 2 | import { domRender, ssrRenderToDom } from '@qwik.dev/core/testing'; |
3 | 3 | import { describe, expect, it } from 'vitest'; |
4 | 4 |
|
@@ -43,4 +43,73 @@ describe.each([ |
43 | 43 | const { document } = await render(<Cmp />, { debug }); |
44 | 44 | expect(document.querySelector('div')).toHaveProperty('textContent', 'child'); |
45 | 45 | }); |
| 46 | + |
| 47 | + it('should not duplicate projected element children when component throws a promise on first render', async () => { |
| 48 | + const Wrapper = component$(() => ( |
| 49 | + <form> |
| 50 | + <Slot /> |
| 51 | + </form> |
| 52 | + )); |
| 53 | + const Title = component$(() => <header>title</header>); |
| 54 | + const Footer = component$(() => <footer>footer</footer>); |
| 55 | + const Cmp = component$(() => { |
| 56 | + // The thrown promise writes a tracked signal, scheduling a second diff |
| 57 | + // pass before <Wrapper> has rendered its <Slot>. The trailing <Footer /> |
| 58 | + // is required: matching it moves the cursor past the stale <div>, so |
| 59 | + // expectNoMore() would not clean it up. |
| 60 | + const store = useSignal<string>(); |
| 61 | + if (!store.value) { |
| 62 | + throw Promise.resolve().then(() => { |
| 63 | + store.value = 'resolved'; |
| 64 | + }); |
| 65 | + } |
| 66 | + return ( |
| 67 | + <Wrapper> |
| 68 | + <Title /> |
| 69 | + <div class="fields">{store.value}</div> |
| 70 | + <Footer /> |
| 71 | + </Wrapper> |
| 72 | + ); |
| 73 | + }); |
| 74 | + const { document } = await render(<Cmp />, { debug }); |
| 75 | + expect(document.querySelectorAll('div.fields')).toHaveLength(1); |
| 76 | + expect(document.querySelectorAll('header')).toHaveLength(1); |
| 77 | + expect(document.querySelectorAll('footer')).toHaveLength(1); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should not duplicate named slot content when component throws a promise on first render', async () => { |
| 81 | + const Wrapper = component$(() => ( |
| 82 | + <form> |
| 83 | + <section> |
| 84 | + <Slot name="top" /> |
| 85 | + </section> |
| 86 | + <Slot /> |
| 87 | + </form> |
| 88 | + )); |
| 89 | + const Title = component$(() => <header>title</header>); |
| 90 | + const Footer = component$(() => <footer>footer</footer>); |
| 91 | + const Cmp = component$(() => { |
| 92 | + const store = useSignal<string>(); |
| 93 | + if (!store.value) { |
| 94 | + throw Promise.resolve().then(() => { |
| 95 | + store.value = 'resolved'; |
| 96 | + }); |
| 97 | + } |
| 98 | + return ( |
| 99 | + <Wrapper> |
| 100 | + <div q:slot="top" class="banner"> |
| 101 | + banner |
| 102 | + </div> |
| 103 | + <Title /> |
| 104 | + <div class="fields">{store.value}</div> |
| 105 | + <Footer /> |
| 106 | + </Wrapper> |
| 107 | + ); |
| 108 | + }); |
| 109 | + const { document } = await render(<Cmp />, { debug }); |
| 110 | + expect(document.querySelectorAll('div.banner')).toHaveLength(1); |
| 111 | + expect(document.querySelectorAll('div.fields')).toHaveLength(1); |
| 112 | + expect(document.querySelectorAll('header')).toHaveLength(1); |
| 113 | + expect(document.querySelectorAll('footer')).toHaveLength(1); |
| 114 | + }); |
46 | 115 | }); |
0 commit comments