@@ -2,36 +2,45 @@ import fs from 'node:fs/promises'
22import path from 'node:path'
33import url from 'node:url'
44import type { Page , Response } from '@playwright/test'
5- import type { User } from '../ src/services/api.ts'
6- import { Route } from './constant'
7- import { expect } from './extends.ts'
8- import { boxedStep } from './utils/test-decorators.ts'
5+ import type { User } from 'src/services/api.ts'
6+ import { Route } from '.. /constant'
7+ import { expect } from '.. /extends.ts'
8+ import { boxedStep } from '.. /utils/test-decorators.ts'
99
1010const __dirname = url . fileURLToPath ( new URL ( '.' , import . meta. url ) )
11- const fixtureDir = path . join ( __dirname , '../cypress/fixtures' )
11+ const fixtureDir = path . join ( __dirname , '../../ cypress/fixtures' )
1212
13- export class ConductPageObject {
13+ export class ConduitPageObject {
1414 constructor (
1515 public readonly page : Page ,
1616 ) { }
1717
1818 async intercept ( method : 'POST' | 'GET' | 'PATCH' | 'DELETE' | 'PUT' , url : string | RegExp , options : {
1919 fixture ?: string
20+ postFixture ?: ( fixture : any ) => void | unknown
2021 statusCode ?: number
2122 body ?: unknown
22- } = { } ) : Promise < ( timeout ?: number ) => Promise < Response > > {
23+ timeout ?: number
24+ } = { } ) : Promise < ( ) => Promise < Response > > {
2325 await this . page . route ( url , async route => {
2426 if ( route . request ( ) . method ( ) !== method )
2527 return await route . continue ( )
2628
29+ if ( options . postFixture && options . fixture ) {
30+ const body = await this . getFixture ( options . fixture )
31+ const returnValue = await options . postFixture ( body )
32+ options . body = returnValue === undefined ? body : returnValue
33+ options . fixture = undefined
34+ }
35+
2736 return await route . fulfill ( {
2837 status : options . statusCode || undefined ,
2938 json : options . body ?? undefined ,
3039 path : options . fixture ? path . join ( fixtureDir , options . fixture ) : undefined ,
3140 } )
3241 } )
3342
34- return ( timeout : number = 1000 ) => this . page . waitForResponse ( response => {
43+ return ( ) => this . page . waitForResponse ( response => {
3544 const request = response . request ( )
3645 if ( request . method ( ) !== method )
3746 return false
@@ -40,7 +49,7 @@ export class ConductPageObject {
4049 return request . url ( ) . includes ( url )
4150
4251 return url . test ( request . url ( ) )
43- } , { timeout } )
52+ } , { timeout : options . timeout ?? 4000 } )
4453 }
4554
4655 async getFixture < T = unknown > ( fixture : string ) : Promise < T > {
@@ -49,21 +58,29 @@ export class ConductPageObject {
4958 }
5059
5160 async goto ( route : Route ) {
52- await this . page . goto ( route )
61+ await this . page . goto ( route , { waitUntil : 'domcontentloaded' } )
5362 }
5463
5564 @boxedStep
5665 async login ( username = 'plumrx' ) {
5766 const userFixture = await this . getFixture < { user : User } > ( 'user.json' )
5867 userFixture . user . username = username
59- await this . intercept ( 'POST' , / u s e r s \/ l o g i n $ / , { statusCode : 200 , body : userFixture } )
6068
6169 await this . goto ( Route . Login )
6270
6371 await this . page . getByPlaceholder ( 'Email' ) . fill ( '[email protected] ' ) 6472 await this . page . getByPlaceholder ( 'Password' ) . fill ( '12345678' )
65- await this . page . getByRole ( 'button' , { name : 'Sign in' } ) . click ( )
73+
74+ const waitForLogin = await this . intercept ( 'POST' , / u s e r s \/ l o g i n $ / , { statusCode : 200 , body : userFixture } )
75+ await Promise . all ( [
76+ waitForLogin ( ) ,
77+ this . page . getByRole ( 'button' , { name : 'Sign in' } ) . click ( ) ,
78+ ] )
6679
6780 await expect ( this . page ) . toHaveURL ( Route . Home )
6881 }
82+
83+ async toContainText ( text : string ) {
84+ await expect ( this . page . locator ( 'body' ) ) . toContainText ( text )
85+ }
6986}
0 commit comments