Skip to content

Commit

Permalink
Merge pull request #1287 from gitKrystan/audit-types-package
Browse files Browse the repository at this point in the history
Port some conveniences from @types/ember__test-helpers package
  • Loading branch information
chriskrycho authored Dec 15, 2022
2 parents 5652374 + f77cb62 commit db0df64
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 7 deletions.
9 changes: 9 additions & 0 deletions addon-test-support/@ember/test-helpers/dom/-get-element.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import getRootElement from './get-root-element';
import Target, { isDocument, isElement } from './-target';

function getElement<
K extends keyof (HTMLElementTagNameMap | SVGElementTagNameMap)
>(target: K): (HTMLElementTagNameMap[K] | SVGElementTagNameMap[K]) | null;
function getElement<K extends keyof HTMLElementTagNameMap>(
target: K
): HTMLElementTagNameMap[K] | null;
function getElement<K extends keyof SVGElementTagNameMap>(
target: K
): SVGElementTagNameMap[K] | null;
function getElement(target: string): Element | null;
function getElement(target: Element): Element;
function getElement(target: Document): Document;
Expand Down
14 changes: 14 additions & 0 deletions addon-test-support/@ember/test-helpers/dom/find-all.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import getElements from './-get-elements';
import { toArray } from '../ie-11-polyfills';

// Derived, with modification, from the types for `querySelectorAll`. These
// would simply be defined as a tweaked re-export as `querySelector` is, but it
// is non-trivial (to say the least!) to preserve overloads like this while also
// changing the return type (from `NodeListOf` to `Array`).
export default function findAll<
K extends keyof (HTMLElementTagNameMap | SVGElementTagNameMap)
>(selector: K): Array<HTMLElementTagNameMap[K] | SVGElementTagNameMap[K]>;
export default function findAll<K extends keyof HTMLElementTagNameMap>(
selector: K
): Array<HTMLElementTagNameMap[K]>;
export default function findAll<K extends keyof SVGElementTagNameMap>(
selector: K
): Array<SVGElementTagNameMap[K]>;
export default function findAll(selector: string): Element[];
/**
Find all elements matched by the given selector. Similar to calling
`querySelectorAll()` on the test root element, but returns an array instead
Expand Down
13 changes: 12 additions & 1 deletion addon-test-support/@ember/test-helpers/dom/find.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import getElement from './-get-element';

// Derived from `querySelector` types.
export default function find<
K extends keyof (HTMLElementTagNameMap | SVGElementTagNameMap)
>(selector: K): HTMLElementTagNameMap[K] | SVGElementTagNameMap[K] | null;
export default function find<K extends keyof HTMLElementTagNameMap>(
selector: K
): HTMLElementTagNameMap[K] | null;
export default function find<K extends keyof SVGElementTagNameMap>(
selector: K
): SVGElementTagNameMap[K] | null;
export default function find(selector: string): Element | null;
/**
Find the first element matched by the given selector. Equivalent to calling
`querySelector()` on the test root element.
@public
@param {string} selector the selector to search for
@return {Element} matched element or null
@return {Element | null} matched element or null
@example
<caption>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ registerHook('triggerEvent', 'start', (target: Target, eventType: string) => {
export default function triggerEvent(
target: Target,
eventType: string,
options?: object
options?: Record<string, unknown>
): Promise<void> {
return Promise.resolve()
.then(() => {
Expand Down
2 changes: 1 addition & 1 deletion addon-test-support/@ember/test-helpers/settled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export interface SettledState {
hasPendingTransitions: boolean | null;
isRenderPending: boolean;
pendingRequestCount: number;
debugInfo?: DebugInfo;
debugInfo: DebugInfo;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion addon-test-support/@ember/test-helpers/setup-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export interface TestContext extends BaseContext {
getProperties(...args: string[]): Pick<BaseContext, string>;

pauseTest(): Promise<void>;
resumeTest(): Promise<void>;
resumeTest(): void;
}

// eslint-disable-next-line require-jsdoc
Expand Down
20 changes: 17 additions & 3 deletions type-tests/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
currentURL,
// Rendering Helpers
render,
rerender,
clearRender,
// Wait Helpers
waitFor,
Expand Down Expand Up @@ -109,7 +110,11 @@ expectTypeOf(tap).toEqualTypeOf<
(target: Target, options?: TouchEventInit) => Promise<void>
>();
expectTypeOf(triggerEvent).toEqualTypeOf<
(target: Target, eventType: string, options?: object) => Promise<void>
(
target: Target,
eventType: string,
options?: Record<string, unknown>
) => Promise<void>
>();
expectTypeOf(triggerKeyEvent).toEqualTypeOf<
(
Expand All @@ -135,8 +140,16 @@ expectTypeOf(typeIn).toEqualTypeOf<
>();

// DOM Query Helpers
expectTypeOf(find).toEqualTypeOf<(selector: string) => Element | null>();
expectTypeOf(find).toEqualTypeOf<Document['querySelector']>();
expectTypeOf(find('a')).toEqualTypeOf<HTMLAnchorElement | SVGAElement | null>();
expectTypeOf(find('div')).toEqualTypeOf<HTMLDivElement | null>();
expectTypeOf(find('circle')).toEqualTypeOf<SVGCircleElement | null>();
expectTypeOf(find('.corkscrew')).toEqualTypeOf<Element | null>();
expectTypeOf(findAll).toEqualTypeOf<(selector: string) => Array<Element>>();
expectTypeOf(findAll('a')).toEqualTypeOf<(HTMLAnchorElement | SVGAElement)[]>();
expectTypeOf(findAll('div')).toEqualTypeOf<HTMLDivElement[]>();
expectTypeOf(findAll('circle')).toEqualTypeOf<SVGCircleElement[]>();
expectTypeOf(findAll('.corkscrew')).toEqualTypeOf<Element[]>();
expectTypeOf(getRootElement).toEqualTypeOf<() => Element | Document>();

// Routing Helpers
Expand All @@ -153,6 +166,7 @@ expectTypeOf(render).toMatchTypeOf<
options?: { owner?: Owner }
) => Promise<void>
>();
expectTypeOf(rerender).toMatchTypeOf<() => Promise<void>>();
expectTypeOf(clearRender).toEqualTypeOf<() => Promise<void>>();

// Wait Helpers
Expand Down Expand Up @@ -186,7 +200,7 @@ expectTypeOf(getSettledState).toEqualTypeOf<
hasPendingTransitions: boolean | null;
isRenderPending: boolean;
pendingRequestCount: number;
debugInfo?: InternalDebugInfo;
debugInfo: InternalDebugInfo;
}
>();

Expand Down

0 comments on commit db0df64

Please sign in to comment.