Skip to content

Commit 9b1ef67

Browse files
committed
Port some conveniences from @types/ember__test-helpers package
e.g. convenient overloads for `find`
1 parent 7f01af2 commit 9b1ef67

7 files changed

Lines changed: 53 additions & 13 deletions

File tree

addon-test-support/@ember/test-helpers/dom/-get-element.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import getRootElement from './get-root-element';
22
import Target, { isDocument, isElement } from './-target';
33

4-
function getElement(target: string): Element | null;
4+
function getElement<K extends keyof HTMLElementTagNameMap>(
5+
target: K
6+
): HTMLElementTagNameMap[K] | null;
7+
function getElement<K extends keyof SVGElementTagNameMap>(
8+
target: K
9+
): SVGElementTagNameMap[K] | null;
10+
function getElement<E extends Element = Element>(target: string): E | null;
511
function getElement(target: Element): Element;
612
function getElement(target: Document): Document;
713
function getElement(target: Window): Document;

addon-test-support/@ember/test-helpers/dom/find-all.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
import getElements from './-get-elements';
22
import { toArray } from '../ie-11-polyfills';
33

4+
// Derived, with modification, from the types for `querySelectorAll`. These
5+
// would simply be defined as a tweaked re-export as `querySelector` is, but it
6+
// is non-trivial (to say the least!) to preserve overloads like this while also
7+
// changing the return type (from `NodeListOf` to `Array`).
8+
export default function findAll<K extends keyof HTMLElementTagNameMap>(
9+
selector: K
10+
): Array<HTMLElementTagNameMap[K]>;
11+
export default function findAll<K extends keyof SVGElementTagNameMap>(
12+
selector: K
13+
): Array<SVGElementTagNameMap[K]>;
14+
export default function findAll(selector: string): Element[];
415
/**
516
Find all elements matched by the given selector. Similar to calling
617
`querySelectorAll()` on the test root element, but returns an array instead

addon-test-support/@ember/test-helpers/dom/find.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
import getElement from './-get-element';
22

3+
// Derived from `querySelector` types.
4+
export default function find<K extends keyof HTMLElementTagNameMap>(
5+
selector: K
6+
): HTMLElementTagNameMap[K] | null;
7+
export default function find<K extends keyof SVGElementTagNameMap>(
8+
selector: K
9+
): SVGElementTagNameMap[K] | null;
10+
export default function find<E extends Element = Element>(
11+
selector: string
12+
): E | null;
313
/**
414
Find the first element matched by the given selector. Equivalent to calling
515
`querySelector()` on the test root element.
616
717
@public
818
@param {string} selector the selector to search for
9-
@return {Element} matched element or null
19+
@return {Element | null} matched element or null
1020
*/
1121
export default function find(selector: string): Element | null {
1222
if (!selector) {

addon-test-support/@ember/test-helpers/dom/trigger-event.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ registerHook('triggerEvent', 'start', (target: Target, eventType: string) => {
5757
export default function triggerEvent(
5858
target: Target,
5959
eventType: string,
60-
options?: object
60+
options?: Record<string, unknown>
6161
): Promise<void> {
6262
return Promise.resolve()
6363
.then(() => {

addon-test-support/@ember/test-helpers/settled.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ export interface SettledState {
193193
hasPendingTransitions: boolean | null;
194194
isRenderPending: boolean;
195195
pendingRequestCount: number;
196-
debugInfo?: DebugInfo;
196+
debugInfo: DebugInfo;
197197
}
198198

199199
/**

addon-test-support/@ember/test-helpers/setup-context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export interface TestContext extends BaseContext {
8080
getProperties(...args: string[]): Pick<BaseContext, string>;
8181

8282
pauseTest(): Promise<void>;
83-
resumeTest(): Promise<void>;
83+
resumeTest(): void;
8484
}
8585

8686
// eslint-disable-next-line require-jsdoc

type-tests/api.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
currentURL,
2424
// Rendering Helpers
2525
render,
26+
rerender,
2627
clearRender,
2728
// Wait Helpers
2829
waitFor,
@@ -45,6 +46,11 @@ import {
4546
unsetContext,
4647
teardownContext,
4748
setupRenderingContext,
49+
BaseContext,
50+
TestContext,
51+
RenderingTestContext,
52+
TestMetadata,
53+
DebugInfo as InternalDebugInfo,
4854
getApplication,
4955
setApplication,
5056
setupApplicationContext,
@@ -57,10 +63,6 @@ import {
5763
getDeprecationsDuringCallback,
5864
getWarnings,
5965
getWarningsDuringCallback,
60-
BaseContext,
61-
TestContext,
62-
TestMetadata,
63-
DebugInfo as InternalDebugInfo,
6466
DeprecationFailure,
6567
Warning,
6668
} from '@ember/test-helpers';
@@ -99,7 +101,11 @@ expectTypeOf(tap).toEqualTypeOf<
99101
(target: Target, options?: TouchEventInit) => Promise<void>
100102
>();
101103
expectTypeOf(triggerEvent).toEqualTypeOf<
102-
(target: Target, eventType: string, options?: object) => Promise<void>
104+
(
105+
target: Target,
106+
eventType: string,
107+
options?: Record<string, unknown>
108+
) => Promise<void>
103109
>();
104110
expectTypeOf(triggerKeyEvent).toEqualTypeOf<
105111
(
@@ -125,8 +131,14 @@ expectTypeOf(typeIn).toEqualTypeOf<
125131
>();
126132

127133
// DOM Query Helpers
128-
expectTypeOf(find).toEqualTypeOf<(selector: string) => Element | null>();
134+
expectTypeOf(find).toEqualTypeOf<Document['querySelector']>();
135+
expectTypeOf(find('a')).toEqualTypeOf<HTMLAnchorElement | null>();
136+
expectTypeOf(find('circle')).toEqualTypeOf<SVGCircleElement | null>();
137+
expectTypeOf(find('.corkscrew')).toEqualTypeOf<Element | null>();
129138
expectTypeOf(findAll).toEqualTypeOf<(selector: string) => Array<Element>>();
139+
expectTypeOf(findAll('a')).toEqualTypeOf<HTMLAnchorElement[]>();
140+
expectTypeOf(findAll('circle')).toEqualTypeOf<SVGCircleElement[]>();
141+
expectTypeOf(findAll('.corkscrew')).toEqualTypeOf<Element[]>();
130142
expectTypeOf(getRootElement).toEqualTypeOf<() => Element | Document>();
131143

132144
// Routing Helpers
@@ -143,6 +155,7 @@ expectTypeOf(render).toMatchTypeOf<
143155
options?: { owner?: Owner }
144156
) => Promise<void>
145157
>();
158+
expectTypeOf(rerender).toMatchTypeOf<() => Promise<void>>();
146159
expectTypeOf(clearRender).toEqualTypeOf<() => Promise<void>>();
147160

148161
// Wait Helpers
@@ -176,7 +189,7 @@ expectTypeOf(getSettledState).toEqualTypeOf<
176189
hasPendingTransitions: boolean | null;
177190
isRenderPending: boolean;
178191
pendingRequestCount: number;
179-
debugInfo?: InternalDebugInfo;
192+
debugInfo: InternalDebugInfo;
180193
}
181194
>();
182195

@@ -209,7 +222,7 @@ expectTypeOf(teardownContext).toEqualTypeOf<
209222
) => Promise<void>
210223
>();
211224
expectTypeOf(setupRenderingContext).toEqualTypeOf<
212-
(context: TestContext) => Promise<void>
225+
(context: TestContext) => Promise<RenderingTestContext>
213226
>();
214227
expectTypeOf(getApplication).toEqualTypeOf<() => Application | undefined>();
215228
expectTypeOf(setApplication).toEqualTypeOf<

0 commit comments

Comments
 (0)