Files
payload/test/helpers/assertToastErrors.ts
Said Akhrarov 70b9cab393 test: deflake indexed e2e (#11911)
### What?
This PR aims to deflake the indexed fields e2e test in
`test/fields/collections/Indexed/e2e.spec.ts`.

The issue is that this test is setup in a way where sometimes two toasts
will present themselves in the ui. The second toast assertion will fail
with a strict mode violation as the toast locator will resolve to two
elements.

### Why?
To prevent this test from flaking in ci.

### How?
Adding a new `dismissAfterAssertion` flag to the `assertToastErrors`
helper function which dismisses the toasts. This way, the toasts will
not raise the aforementioned error as they will be dismissed from the
ui.

The logic is handled in a separate loop through such that the assertions
occur first. This is done so that dismissing a toast does not surface
errors due to the order of toasts being shown changing.
2025-03-29 01:02:05 +00:00

39 lines
1.0 KiB
TypeScript

import type { Page } from '@playwright/test'
import { expect } from '@playwright/test'
export async function assertToastErrors({
page,
errors,
dismissAfterAssertion,
}: {
dismissAfterAssertion?: boolean
errors: string[]
page: Page
}): Promise<void> {
const message =
errors.length === 1
? 'The following field is invalid:'
: `The following fields are invalid (${errors.length}):`
await expect(
page.locator('.payload-toast-container li').filter({ hasText: message }),
).toBeVisible()
for (let i = 0; i < errors.length; i++) {
const error = errors[i]
if (error) {
await expect(
page.locator('.payload-toast-container [data-testid="field-errors"] li').nth(i),
).toHaveText(error)
}
}
if (dismissAfterAssertion) {
const closeButtons = page.locator('.payload-toast-container button.payload-toast-close-button')
const count = await closeButtons.count()
for (let i = 0; i < count; i++) {
await closeButtons.nth(i).click()
}
}
}