|
1 | | -import { component$, componentQrl, inlinedQrl, useId } from '@qwik.dev/core'; |
| 1 | +import { component$, componentQrl, inlinedQrl, useId, useSignal } from '@qwik.dev/core'; |
2 | 2 | import { describe, expect, it } from 'vitest'; |
3 | | -import { domRender, ssrRenderToDom } from '@qwik.dev/core/testing'; |
| 3 | +import { domRender, ssrRenderToDom, trigger } from '@qwik.dev/core/testing'; |
4 | 4 |
|
5 | 5 | const debug = false; //true; |
6 | 6 | Error.stackTraceLimit = 100; |
@@ -34,11 +34,47 @@ describe.each([ |
34 | 34 |
|
35 | 35 | const { document } = await render(<Parent />, { debug }); |
36 | 36 | if (render === ssrRenderToDom) { |
37 | | - expect(document.querySelector('#cmp1')?.textContent).toMatch(/^\w{3}cmp0$/); |
38 | | - expect(document.querySelector('#cmp2')?.textContent).toMatch(/^\w{3}2cm1$/); |
| 37 | + expect(document.querySelector('#cmp1')?.textContent).toMatch(/^\w{3}cmp255s$/); |
| 38 | + expect(document.querySelector('#cmp2')?.textContent).toMatch(/^\w{3}2cm255t$/); |
39 | 39 | } else { |
40 | 40 | expect(document.querySelector('#cmp1')?.textContent).toMatch(/^cmp0$/); |
41 | 41 | expect(document.querySelector('#cmp2')?.textContent).toMatch(/^Ccm1$/); |
42 | 42 | } |
43 | 43 | }); |
| 44 | + |
| 45 | + it('should generate different ids for csr and ssr', async () => { |
| 46 | + const Checkbox = component$((props: { label: string }) => { |
| 47 | + const id = useId(); |
| 48 | + return ( |
| 49 | + <div> |
| 50 | + <input type="checkbox" id={id} /> |
| 51 | + <label for={id}>{props.label}</label> |
| 52 | + </div> |
| 53 | + ); |
| 54 | + }); |
| 55 | + |
| 56 | + const Cmp = component$(() => { |
| 57 | + const enabled = useSignal(false); |
| 58 | + |
| 59 | + return ( |
| 60 | + <div> |
| 61 | + <h1>useId Example</h1> |
| 62 | + <Checkbox label="Item 1" /> |
| 63 | + <button |
| 64 | + onClick$={() => { |
| 65 | + enabled.value = !enabled.value; |
| 66 | + }} |
| 67 | + ></button> |
| 68 | + |
| 69 | + {enabled.value && <Checkbox label="Subitem 1" />} |
| 70 | + </div> |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + const { document } = await render(<Cmp />, { debug }); |
| 75 | + await trigger(document.body, 'button', 'click'); |
| 76 | + const inputs = document.querySelectorAll('input'); |
| 77 | + expect(inputs.length).toBe(2); |
| 78 | + expect(inputs[0].id).not.toBe(inputs[1].id); |
| 79 | + }); |
44 | 80 | }); |
0 commit comments