|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +import { |
| 3 | + type Page, |
| 4 | + type Locator, |
| 5 | + test as base, |
| 6 | + expect as baseExpect, |
| 7 | +} from '@playwright/test'; |
| 8 | +import { Interval } from '../../src/types'; |
| 9 | +import { uid } from 'uid'; |
| 10 | +import { IntervalType } from '../../src/types'; |
| 11 | + |
| 12 | +export const mockInterval = (type: IntervalType, duration: number) => ({ |
| 13 | + type, |
| 14 | + duration: duration * 60 * 1000, |
| 15 | + id: uid(), |
| 16 | +}); |
| 17 | + |
| 18 | +class Queries { |
| 19 | + protected readonly list: Locator; |
| 20 | + protected readonly controls: Locator; |
| 21 | + |
| 22 | + readonly timer: Locator; |
| 23 | + |
| 24 | + constructor(public readonly page: Page) { |
| 25 | + this.list = this.page.getByRole('list', { name: 'Intervals' }); |
| 26 | + this.timer = this.page.getByRole('timer'); |
| 27 | + this.controls = this.page.getByRole('group', { name: 'Timer controls' }); |
| 28 | + } |
| 29 | + |
| 30 | + getControl(name: string | RegExp, { context }: { context?: Locator } = {}) { |
| 31 | + return (context ?? this.controls).getByRole('button', { name }); |
| 32 | + } |
| 33 | + |
| 34 | + getButton(name: string | RegExp, { context }: { context?: Locator } = {}) { |
| 35 | + return (context ?? this.page).getByRole('button', { name }); |
| 36 | + } |
| 37 | + |
| 38 | + getIntervalList() { |
| 39 | + return this.list.getByRole('listitem'); |
| 40 | + } |
| 41 | + |
| 42 | + getIntervalByType(type: 'W' | 'LB' | 'SB') { |
| 43 | + return this.getIntervalList().filter({ hasText: type }); |
| 44 | + } |
| 45 | + |
| 46 | + getIntervalByDuration(duration: number) { |
| 47 | + return this.getIntervalList().filter({ |
| 48 | + hasText: `${String(duration).padStart(2, '0')}:00`, |
| 49 | + }); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +export class AppPage { |
| 54 | + constructor(public readonly page: Page) {} |
| 55 | + |
| 56 | + async setStorage(intervals: Interval[]) { |
| 57 | + // set two intervals |
| 58 | + await this.page.addInitScript((intervals) => { |
| 59 | + window.localStorage.setItem('intervals', JSON.stringify(intervals)); |
| 60 | + }, intervals); |
| 61 | + } |
| 62 | + |
| 63 | + async withinSettings(fn: (dialog: Locator) => Promise<void> | void) { |
| 64 | + await fn(this.page.getByRole('dialog', { name: 'Settings' })); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +export const test = base.extend<{ appPage: AppPage; queries: Queries }>({ |
| 69 | + appPage: async ({ page }, use) => { |
| 70 | + const todoPage = new AppPage(page); |
| 71 | + |
| 72 | + await use(todoPage); |
| 73 | + }, |
| 74 | + queries: async ({ page }, use) => { |
| 75 | + await use(new Queries(page)); |
| 76 | + }, |
| 77 | +}); |
| 78 | + |
| 79 | +export const expect = baseExpect.extend({ |
| 80 | + async toHaveOptions( |
| 81 | + locator: Locator, |
| 82 | + expectedOptions: string[], |
| 83 | + options?: { timeout?: number }, |
| 84 | + ) { |
| 85 | + const assertionName = 'toHaveOptions'; |
| 86 | + let pass = true; |
| 87 | + |
| 88 | + let matcherResult: any; |
| 89 | + try { |
| 90 | + const optionsLocator = locator.getByRole('option'); |
| 91 | + await baseExpect(optionsLocator).toHaveText(expectedOptions, options); |
| 92 | + } catch (e) { |
| 93 | + matcherResult = (e as any).matcherResult; |
| 94 | + pass = false; |
| 95 | + } |
| 96 | + |
| 97 | + const message = () => |
| 98 | + this.utils.matcherHint(assertionName, undefined, undefined, { |
| 99 | + isNot: this.isNot, |
| 100 | + }) + |
| 101 | + '\n\n' + |
| 102 | + `Locator: ${locator}\n` + |
| 103 | + `Expected options: ${this.utils.printExpected(expectedOptions)}\n` + |
| 104 | + `Received options: ${this.utils.printReceived(matcherResult?.actual)}`; |
| 105 | + |
| 106 | + return { |
| 107 | + message, |
| 108 | + pass, |
| 109 | + name: assertionName, |
| 110 | + expected: expectedOptions, |
| 111 | + actual: matcherResult?.actual, |
| 112 | + }; |
| 113 | + }, |
| 114 | + async toHaveSelected( |
| 115 | + locator: Locator, |
| 116 | + expected: string | RegExp, |
| 117 | + options?: { timeout?: number }, |
| 118 | + ) { |
| 119 | + const assertionName = 'toHaveOptions'; |
| 120 | + let pass = true; |
| 121 | + |
| 122 | + let matcherResult: any; |
| 123 | + try { |
| 124 | + const matchOptionValue = |
| 125 | + (await locator |
| 126 | + .getByRole('option', { name: expected }) |
| 127 | + .getAttribute('value')) ?? expected; |
| 128 | + await baseExpect(locator).toHaveValue(matchOptionValue, options); |
| 129 | + } catch (e) { |
| 130 | + matcherResult = (e as any).matcherResult; |
| 131 | + pass = false; |
| 132 | + } |
| 133 | + |
| 134 | + const message = () => |
| 135 | + this.utils.matcherHint(assertionName, undefined, undefined, { |
| 136 | + isNot: this.isNot, |
| 137 | + }) + |
| 138 | + '\n\n' + |
| 139 | + `Locator: ${locator}\n` + |
| 140 | + `Expected selected option: ${this.utils.printExpected(expected)}\n` + |
| 141 | + `Received selected option: ${this.utils.printReceived(matcherResult?.actual)}`; |
| 142 | + |
| 143 | + return { |
| 144 | + message, |
| 145 | + pass, |
| 146 | + name: assertionName, |
| 147 | + expected, |
| 148 | + actual: matcherResult?.actual, |
| 149 | + }; |
| 150 | + }, |
| 151 | +}); |
0 commit comments