Skip to content

Commit 6b0a632

Browse files
committed
chore(types): add event handler type tests
1 parent a1f7314 commit 6b0a632

File tree

2 files changed

+82
-64
lines changed

2 files changed

+82
-64
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import type { EventHandler, FunctionComponent, PropsOf } from '@qwik.dev/core';
2+
import { component$ } from '@qwik.dev/core';
3+
import { describe, expectTypeOf, test } from 'vitest';
4+
5+
// This is in a separate file because it makes TS very slow
6+
describe('polymorphism', () => {
7+
test('polymorphic component', () => () => {
8+
const Poly = component$(
9+
<C extends string | FunctionComponent = 'div'>({
10+
as,
11+
...props
12+
}: { as?: C } & PropsOf<string extends C ? 'div' : C>) => {
13+
const Cmp = as || 'div';
14+
return <Cmp {...props}>hi</Cmp>;
15+
}
16+
);
17+
expectTypeOf<Parameters<typeof Poly<'button'>>[0]['popovertarget']>().toEqualTypeOf<
18+
string | undefined
19+
>();
20+
expectTypeOf<Parameters<typeof Poly<'a'>>[0]['href']>().toEqualTypeOf<string | undefined>();
21+
expectTypeOf<Parameters<typeof Poly<'button'>>[0]>().not.toHaveProperty('href');
22+
expectTypeOf<Parameters<typeof Poly<'a'>>[0]>().not.toHaveProperty('popovertarget');
23+
expectTypeOf<
24+
Parameters<Extract<Parameters<typeof Poly>[0]['onClick$'], EventHandler>>[1]
25+
>().toEqualTypeOf<HTMLDivElement>();
26+
27+
const MyCmp = component$((p: { name: string }) => <span>Hi {p.name}</span>);
28+
29+
return (
30+
<>
31+
<Poly
32+
onClick$={(ev, el) => {
33+
expectTypeOf(ev).not.toBeAny();
34+
expectTypeOf(ev).toEqualTypeOf<PointerEvent>();
35+
expectTypeOf(el).toEqualTypeOf<HTMLDivElement>();
36+
}}
37+
// This should error
38+
// popovertarget
39+
>
40+
Foo
41+
</Poly>
42+
<Poly
43+
as="a"
44+
onClick$={(ev, el) => {
45+
expectTypeOf(ev).not.toBeAny();
46+
expectTypeOf(ev).toEqualTypeOf<PointerEvent>();
47+
expectTypeOf(el).toEqualTypeOf<HTMLAnchorElement>();
48+
}}
49+
href="hi"
50+
// This should error
51+
// popovertarget
52+
>
53+
Foo
54+
</Poly>
55+
<Poly
56+
as="button"
57+
onClick$={(ev, el) => {
58+
expectTypeOf(ev).not.toBeAny();
59+
expectTypeOf(ev).toEqualTypeOf<PointerEvent>();
60+
expectTypeOf(el).toEqualTypeOf<HTMLButtonElement>();
61+
}}
62+
popovertarget="foo"
63+
>
64+
Bar
65+
</Poly>
66+
<Poly as={MyCmp} name="meep" />
67+
</>
68+
);
69+
});
70+
});

packages/qwik/src/core/shared/jsx/types/jsx-types.unit.tsx

Lines changed: 12 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,18 @@ describe('types', () => {
148148
type: 'button';
149149
popovertarget?: string;
150150
}>().toMatchTypeOf<PropsOf<'input'>>();
151+
152+
$((_, element) => {
153+
element.select();
154+
expectTypeOf(element).toEqualTypeOf<HTMLInputElement>();
155+
}) as QRLEventHandlerMulti<FocusEvent, HTMLInputElement>;
156+
157+
const t = $<EventHandler<FocusEvent, HTMLInputElement>>((_, element) => {
158+
element.select();
159+
expectTypeOf(element).toEqualTypeOf<HTMLInputElement>();
160+
});
161+
expectTypeOf(t).toExtend<QRLEventHandlerMulti<FocusEvent, HTMLInputElement>>();
162+
151163
<>
152164
<button popovertarget="meep" />
153165
<input type="button" popovertarget="meep" />
@@ -225,70 +237,6 @@ describe('types', () => {
225237
</>;
226238
});
227239

228-
test('polymorphic component', () => () => {
229-
const Poly = component$(
230-
<C extends string | FunctionComponent = 'div'>({
231-
as,
232-
...props
233-
}: { as?: C } & PropsOf<string extends C ? 'div' : C>) => {
234-
const Cmp = as || 'div';
235-
return <Cmp {...props}>hi</Cmp>;
236-
}
237-
);
238-
expectTypeOf<Parameters<typeof Poly<'button'>>[0]['popovertarget']>().toEqualTypeOf<
239-
string | undefined
240-
>();
241-
expectTypeOf<Parameters<typeof Poly<'a'>>[0]['href']>().toEqualTypeOf<string | undefined>();
242-
expectTypeOf<Parameters<typeof Poly<'button'>>[0]>().not.toHaveProperty('href');
243-
expectTypeOf<Parameters<typeof Poly<'a'>>[0]>().not.toHaveProperty('popovertarget');
244-
expectTypeOf<
245-
Parameters<Extract<Parameters<typeof Poly>[0]['onClick$'], EventHandler>>[1]
246-
>().toEqualTypeOf<HTMLDivElement>();
247-
248-
const MyCmp = component$((p: { name: string }) => <span>Hi {p.name}</span>);
249-
250-
return (
251-
<>
252-
<Poly
253-
onClick$={(ev, el) => {
254-
expectTypeOf(ev).not.toBeAny();
255-
expectTypeOf(ev).toEqualTypeOf<PointerEvent>();
256-
expectTypeOf(el).toEqualTypeOf<HTMLDivElement>();
257-
}}
258-
// This should error
259-
// popovertarget
260-
>
261-
Foo
262-
</Poly>
263-
<Poly
264-
as="a"
265-
onClick$={(ev, el) => {
266-
expectTypeOf(ev).not.toBeAny();
267-
expectTypeOf(ev).toEqualTypeOf<PointerEvent>();
268-
expectTypeOf(el).toEqualTypeOf<HTMLAnchorElement>();
269-
}}
270-
href="hi"
271-
// This should error
272-
// popovertarget
273-
>
274-
Foo
275-
</Poly>
276-
<Poly
277-
as="button"
278-
onClick$={(ev, el) => {
279-
expectTypeOf(ev).not.toBeAny();
280-
expectTypeOf(ev).toEqualTypeOf<PointerEvent>();
281-
expectTypeOf(el).toEqualTypeOf<HTMLButtonElement>();
282-
}}
283-
popovertarget="foo"
284-
>
285-
Bar
286-
</Poly>
287-
<Poly as={MyCmp} name="meep" />
288-
</>
289-
);
290-
});
291-
292240
test('FunctionComponent', () => () => {
293241
const Cmp = component$((props: { foo: string }) => null);
294242
expectTypeOf(Cmp).toMatchTypeOf<FunctionComponent<{ foo: string }>>();

0 commit comments

Comments
 (0)