Files
payload/test/helpers/e2e/trackNetworkRequests.ts
Jacob Fletcher 9e85be0006 fix(next): autosave document rendering (#9364)
Closes #9242 and #9365. Autosave-enabled documents rendered within a
drawer were not being properly handled. This was causing multiple draft
documents to be created upon opening the drawer, as well as an empty
document returned from the server function, etc.
2024-11-19 19:01:54 -05:00

53 lines
1.5 KiB
TypeScript

import type { Page, Request } from '@playwright/test'
import { expect } from '@playwright/test'
// Allows you to test the number of network requests triggered by an action
// This can be used to ensure various actions do not trigger unnecessary requests
// For example, an effect within a component might fetch data multiple times unnecessarily
export const trackNetworkRequests = async (
page: Page,
url: string,
action: () => Promise<any>,
options?: {
allowedNumberOfRequests?: number
beforePoll?: () => Promise<any> | void
interval?: number
timeout?: number
},
): Promise<Array<Request>> => {
const { beforePoll, allowedNumberOfRequests = 1, timeout = 5000, interval = 1000 } = options || {}
const matchedRequests = []
// begin tracking network requests
page.on('request', (request) => {
if (request.url().includes(url)) {
matchedRequests.push(request)
}
})
await action()
if (typeof beforePoll === 'function') {
await beforePoll()
}
const startTime = Date.now()
// continuously poll even after a request has been matched
// this will ensure no subsequent requests are made
// such as a result of a `useEffect` within a component
while (Date.now() - startTime < timeout) {
if (matchedRequests.length > 0) {
expect(matchedRequests.length).toBeLessThanOrEqual(allowedNumberOfRequests)
}
await new Promise((resolve) => setTimeout(resolve, interval))
}
expect(matchedRequests.length).toBe(allowedNumberOfRequests)
return matchedRequests
}