11import { AuthUtility } from "@auth/auth.utility" ;
2- import { Page } from "@playwright/test" ;
2+ import { Page , TestInfo } from "@playwright/test" ;
33import { API_URLS , mockApiUrl } from "@shared/utilities/url-api.utility" ;
4+ import { Article , CreateArticleRequest } from "./models/article.model" ;
5+ import { CompositeIdFactory } from "@shared/factories/composite-id.factory" ;
6+ import { ArticleFactory } from "./article.factory" ;
7+ import { FRONT_URLS } from "@shared/utilities/url-front.utility" ;
48
59export class ArticleUtility {
10+ static async createArticleViaApi ( token : string , articlePayload : CreateArticleRequest ) : Promise < Article > {
11+ const apiContext = await AuthUtility . createAuthenticatedApiContext ( token ) ;
12+
13+ try {
14+ const response = await apiContext . post ( API_URLS . ARTICLES . CREATION , {
15+ data : { article : articlePayload } ,
16+ } ) ;
17+
18+ if ( ! response . ok ( ) ) {
19+ const body = await response . text ( ) ;
20+ throw new Error (
21+ `Create article failed: ${ response . status ( ) } ${ response . statusText ( ) } - ${ body } `
22+ ) ;
23+ }
24+ const responseBody = await response . json ( ) ;
25+ return responseBody . article ;
26+ } finally {
27+ await apiContext . dispose ( ) ;
28+ }
29+ }
30+
631 static async deleteArticle ( token : string , slug : string ) : Promise < void > {
732 const apiContext = await AuthUtility . createAuthenticatedApiContext ( token ) ;
833
@@ -42,4 +67,33 @@ export class ArticleUtility {
4267 getCallCount : ( ) => callCount ,
4368 }
4469 }
70+
71+ static async mockGetArticleList ( page : Page , { delay = 0 , status = 200 , body = { } } = { } ) : Promise < void > {
72+ await page . route ( mockApiUrl ( API_URLS . ARTICLES . GET_LIST ( ) ) , async route => {
73+ if ( delay ) await new Promise ( r => setTimeout ( r , delay ) ) ;
74+ await route . fulfill ( {
75+ status,
76+ contentType : 'application/json' ,
77+ body : JSON . stringify ( body ) ,
78+ } ) ;
79+ } ) ;
80+ }
81+
82+ static async mockGetTagList ( page : Page , { delay = 0 , status = 200 , body = { } } = { } ) : Promise < void > {
83+ await page . route ( mockApiUrl ( API_URLS . TAG . GET_LIST ) , async route => {
84+ if ( delay ) await new Promise ( r => setTimeout ( r , delay ) ) ;
85+ await route . fulfill ( {
86+ status,
87+ contentType : 'application/json' ,
88+ body : JSON . stringify ( body ) ,
89+ } ) ;
90+ } ) ;
91+ }
92+
93+ static generateTestArticle ( workerInfo : TestInfo , title : string ) : CreateArticleRequest {
94+ const shardIndex = workerInfo . config . shard ?. current ?? 1 ;
95+ const id = CompositeIdFactory . create ( title , 'shard' , shardIndex , 'worker' , workerInfo . parallelIndex ) ;
96+ return ArticleFactory . buildArticlePayload ( id ) ;
97+ }
98+
4599}
0 commit comments