-
-
Notifications
You must be signed in to change notification settings - Fork 260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Port some conveniences from @types/ember__test-helpers package #1287
Changes from 1 commit
9b1ef67
05aa399
10c9f68
5024ed0
c7eb91a
f77cb62
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,7 +1,13 @@ | ||||||
import getRootElement from './get-root-element'; | ||||||
import Target, { isDocument, isElement } from './-target'; | ||||||
|
||||||
function getElement(target: string): Element | 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<E extends Element = Element>(target: string): E | null; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I recognize these came from the DT types, but… this is terrible and we should not perpetuate its terribleness.
Suggested change
This change prevents people from “casting” it implicitly by writing |
||||||
function getElement(target: Element): Element; | ||||||
function getElement(target: Document): Document; | ||||||
function getElement(target: Window): Document; | ||||||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,12 +1,22 @@ | ||||||||||
import getElement from './-get-element'; | ||||||||||
|
||||||||||
// Derived from `querySelector` types. | ||||||||||
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<E extends Element = Element>( | ||||||||||
selector: string | ||||||||||
): E | null; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here!
Suggested change
|
||||||||||
/** | ||||||||||
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 | ||||||||||
*/ | ||||||||||
export default function find(selector: string): Element | null { | ||||||||||
if (!selector) { | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I did a deep dive on what the types should be, and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could do something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Going to open a separate PR w/ the deep dive since I couldn't help myself. 😂 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hahaha I love it. Feels very https://xkcd.com/356/ 😂 "A Project™" sounds exactly right, though. |
||
): Promise<void> { | ||
return Promise.resolve() | ||
.then(() => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ import { | |
currentURL, | ||
// Rendering Helpers | ||
render, | ||
rerender, | ||
clearRender, | ||
// Wait Helpers | ||
waitFor, | ||
|
@@ -45,6 +46,11 @@ import { | |
unsetContext, | ||
teardownContext, | ||
setupRenderingContext, | ||
BaseContext, | ||
TestContext, | ||
RenderingTestContext, | ||
TestMetadata, | ||
DebugInfo as InternalDebugInfo, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No harm done here, but for future reference: I try to keep things like these ordering changes separate from any meaningful changes—I just pull them into a separate commit, using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll just un-sort for now. Re There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per my note on #1286, I think exporting it is great. 👍🏼 |
||
getApplication, | ||
setApplication, | ||
setupApplicationContext, | ||
|
@@ -57,10 +63,6 @@ import { | |
getDeprecationsDuringCallback, | ||
getWarnings, | ||
getWarningsDuringCallback, | ||
BaseContext, | ||
TestContext, | ||
TestMetadata, | ||
DebugInfo as InternalDebugInfo, | ||
DeprecationFailure, | ||
Warning, | ||
} from '@ember/test-helpers'; | ||
|
@@ -99,7 +101,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< | ||
( | ||
|
@@ -125,8 +131,14 @@ expectTypeOf(typeIn).toEqualTypeOf< | |
>(); | ||
|
||
// DOM Query Helpers | ||
expectTypeOf(find).toEqualTypeOf<(selector: string) => Element | null>(); | ||
expectTypeOf(find).toEqualTypeOf<Document['querySelector']>(); | ||
expectTypeOf(find('a')).toEqualTypeOf<HTMLAnchorElement | 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[]>(); | ||
expectTypeOf(findAll('circle')).toEqualTypeOf<SVGCircleElement[]>(); | ||
expectTypeOf(findAll('.corkscrew')).toEqualTypeOf<Element[]>(); | ||
expectTypeOf(getRootElement).toEqualTypeOf<() => Element | Document>(); | ||
|
||
// Routing Helpers | ||
|
@@ -143,6 +155,7 @@ expectTypeOf(render).toMatchTypeOf< | |
options?: { owner?: Owner } | ||
) => Promise<void> | ||
>(); | ||
expectTypeOf(rerender).toMatchTypeOf<() => Promise<void>>(); | ||
expectTypeOf(clearRender).toEqualTypeOf<() => Promise<void>>(); | ||
|
||
// Wait Helpers | ||
|
@@ -176,7 +189,7 @@ expectTypeOf(getSettledState).toEqualTypeOf< | |
hasPendingTransitions: boolean | null; | ||
isRenderPending: boolean; | ||
pendingRequestCount: number; | ||
debugInfo?: InternalDebugInfo; | ||
debugInfo: InternalDebugInfo; | ||
} | ||
>(); | ||
|
||
|
@@ -209,7 +222,7 @@ expectTypeOf(teardownContext).toEqualTypeOf< | |
) => Promise<void> | ||
>(); | ||
expectTypeOf(setupRenderingContext).toEqualTypeOf< | ||
(context: TestContext) => Promise<void> | ||
(context: TestContext) => Promise<RenderingTestContext> | ||
>(); | ||
expectTypeOf(getApplication).toEqualTypeOf<() => Application | undefined>(); | ||
expectTypeOf(setApplication).toEqualTypeOf< | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These types started as copy-pasta from the built-in
querySelector
types, which look like this:...but as @chriskrycho pointed out here, those types aren't that great.
Since we're diverging from them a bit by removing the generic from the
target: string
overload, I decided to also fix a type bug from those types, which is this:HTMLElementTagNameMap
andSVGElementTagNameMap
have some overlap in keys, e.g.a
is a valid tag in both. In the built-inquerySelector
types, the return type fortarget: 'a'
would always be anHTMLAnchorElement
, when the actual value might actually be aSVGAElement
. So I added a third overload to account for that overlap. You can see the results in the type-tests.