Allow any number of onFetch handlers for a single test (#54846)

Recent usability testing indicated that it's convenient to register multiple `onFetch` handlers and iterate them until one of them response a result.
This commit is contained in:
Dima Voytenko 2023-08-31 08:58:53 -07:00 committed by GitHub
parent 7b54e954c2
commit 826ef8c715
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -9,7 +9,7 @@ export interface NextFixture {
}
class NextFixtureImpl implements NextFixture {
private fetchHandler: FetchHandler | null = null
private fetchHandlers: FetchHandler[] = []
constructor(
public testId: string,
@ -27,12 +27,11 @@ class NextFixtureImpl implements NextFixture {
}
onFetch(handler: FetchHandler): void {
this.fetchHandler = handler
this.fetchHandlers.push(handler)
}
private async handleFetch(request: Request): Promise<FetchHandlerResult> {
const handler = this.fetchHandler
if (handler) {
for (const handler of this.fetchHandlers.slice().reverse()) {
const result = handler(request)
if (result) {
return result