|
| 1 | +import { expect as baseExpect } from "@playwright/test"; |
| 2 | +import type { FileUpload } from "../pages/FileUpload"; |
| 3 | +import type { MatcherResult } from "./types"; |
| 4 | + |
| 5 | +export interface FileUploadMatchers { |
| 6 | + toHaveSummaryUploadStatus(expectedStatus: { |
| 7 | + successfulFiles: number; |
| 8 | + totalFiles: number; |
| 9 | + }): Promise<MatcherResult>; |
| 10 | + toHaveItemUploadStatus(expectedStatus: { |
| 11 | + fileName: string; |
| 12 | + status: "success" | "danger"; |
| 13 | + }): Promise<MatcherResult>; |
| 14 | +} |
| 15 | + |
| 16 | +type FileUploadMatcherDefinitions = { |
| 17 | + readonly [K in keyof FileUploadMatchers]: ( |
| 18 | + receiver: FileUpload, |
| 19 | + ...args: Parameters<FileUploadMatchers[K]> |
| 20 | + ) => Promise<MatcherResult>; |
| 21 | +}; |
| 22 | + |
| 23 | +export const fileUploadAssertions = |
| 24 | + baseExpect.extend<FileUploadMatcherDefinitions>({ |
| 25 | + toHaveSummaryUploadStatus: async ( |
| 26 | + fileUpload: FileUpload, |
| 27 | + expectedStatus: { |
| 28 | + successfulFiles: number; |
| 29 | + totalFiles: number; |
| 30 | + }, |
| 31 | + ): Promise<MatcherResult> => { |
| 32 | + try { |
| 33 | + await baseExpect( |
| 34 | + fileUpload._uploader.locator( |
| 35 | + ".pf-v6-c-multiple-file-upload__status .pf-v6-c-expandable-section__toggle", |
| 36 | + ), |
| 37 | + ).toContainText( |
| 38 | + `${expectedStatus.successfulFiles} of ${expectedStatus.totalFiles} files uploaded`, |
| 39 | + ); |
| 40 | + |
| 41 | + return { |
| 42 | + pass: true, |
| 43 | + message: () => "Uploader has expected summary status", |
| 44 | + }; |
| 45 | + } catch (error) { |
| 46 | + return { |
| 47 | + pass: false, |
| 48 | + message: () => |
| 49 | + error instanceof Error ? error.message : String(error), |
| 50 | + }; |
| 51 | + } |
| 52 | + }, |
| 53 | + toHaveItemUploadStatus: async ( |
| 54 | + fileUpload: FileUpload, |
| 55 | + expectedStatus: { |
| 56 | + fileName: string; |
| 57 | + status: "success" | "danger"; |
| 58 | + }, |
| 59 | + ): Promise<MatcherResult> => { |
| 60 | + try { |
| 61 | + const statusItem = await fileUpload.getUploadStatusItem( |
| 62 | + expectedStatus.fileName, |
| 63 | + ); |
| 64 | + await baseExpect( |
| 65 | + statusItem.locator(`.pf-v6-c-progress.pf-m-${expectedStatus.status}`), |
| 66 | + ).toBeVisible(); |
| 67 | + await baseExpect( |
| 68 | + statusItem.locator(".pf-v6-c-progress__status", { hasText: "100%" }), |
| 69 | + ).toBeVisible(); |
| 70 | + |
| 71 | + return { |
| 72 | + pass: true, |
| 73 | + message: () => "Uploader has item with expected status", |
| 74 | + }; |
| 75 | + } catch (error) { |
| 76 | + return { |
| 77 | + pass: false, |
| 78 | + message: () => |
| 79 | + error instanceof Error ? error.message : String(error), |
| 80 | + }; |
| 81 | + } |
| 82 | + }, |
| 83 | + }); |
0 commit comments