-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: export our own async version of fireEvent (#3)
* feat: export our own async version of fireEvent The promise returned from fireEvent resolves once any pending updates caused by the event have completed. BREAKING CHANGE: fireEvent no longer returns a boolean and instead returns a promise.
- Loading branch information
1 parent
f277590
commit 7ff8fbe
Showing
6 changed files
with
145 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { | ||
fireEvent as originalFireEvent, | ||
wait, | ||
EventType, | ||
FireFunction as originalFireFunction, | ||
FireObject as originalFireObject | ||
} from "@testing-library/dom"; | ||
|
||
export interface EventRecord { | ||
"*"?: Array<{ type: string; args: unknown[] }>; | ||
[x: string]: unknown[] | undefined; | ||
} | ||
|
||
export interface RenderOptions { | ||
container?: Element | DocumentFragment; | ||
} | ||
|
||
export interface Template { | ||
renderToString( | ||
input: unknown, | ||
cb: (err: Error | null, result: any) => void | ||
): any; | ||
render(input: unknown, cb: (err: Error | null, result: any) => void): any; | ||
} | ||
|
||
export const INTERNAL_EVENTS = [ | ||
"create", | ||
"input", | ||
"render", | ||
"mount", | ||
"update", | ||
"destroy" | ||
] as const; | ||
|
||
export type InternalEventNames = typeof INTERNAL_EVENTS[number]; | ||
|
||
export type FireFunction = ( | ||
...params: Parameters<originalFireFunction> | ||
) => Promise<void>; | ||
|
||
export type FireObject = { | ||
[K in EventType]: ( | ||
...params: Parameters<originalFireObject[keyof originalFireObject]> | ||
) => Promise<void>; | ||
}; | ||
|
||
export const fireEvent = (async (...params) => { | ||
originalFireEvent(...params); | ||
await wait(); | ||
}) as FireFunction & FireObject; | ||
|
||
Object.keys(originalFireEvent).forEach((eventName: EventType) => { | ||
const fire = originalFireEvent[eventName]; | ||
fireEvent[eventName] = async (...params) => { | ||
fire(...params); | ||
|
||
// TODO: this waits for a possible update using setTimeout(0) which should | ||
// be sufficient, but ideally we would hook into the Marko lifecycle to | ||
// determine when all pending updates are complete. | ||
await wait(); | ||
}; | ||
}); | ||
|
||
export type AsyncReturnValue< | ||
AsyncFunction extends (...args: any) => Promise<any> | ||
> = Parameters< | ||
NonNullable<Parameters<ReturnType<AsyncFunction>["then"]>[0]> | ||
>[0]; |
This file was deleted.
Oops, something went wrong.