11import { test , expect } from "playwright/test" ;
2+ import { randomUUID } from "crypto" ;
23
34test . describe ( "Unauthenticated Articles Page" , ( ) => {
45 test . beforeEach ( async ( { page } ) => {
@@ -16,7 +17,7 @@ test.describe("Unauthenticated Articles Page", () => {
1617 ) ;
1718 } ) ;
1819
19- test ( "Should not show bookmark article icon" , async ( { page } ) => {
20+ test ( "Should show bookmark article icon" , async ( { page } ) => {
2021 await page . goto ( "http://localhost:3000/articles" ) ;
2122
2223 await expect (
@@ -25,7 +26,7 @@ test.describe("Unauthenticated Articles Page", () => {
2526
2627 await expect (
2728 page . locator ( "article" ) . first ( ) . getByLabel ( "Bookmark this post" ) ,
28- ) . toBeHidden ( ) ;
29+ ) . toBeVisible ( ) ;
2930 } ) ;
3031 test ( "Should load more articles when scrolling to the end of the page" , async ( {
3132 page,
@@ -57,6 +58,24 @@ test.describe("Unauthenticated Articles Page", () => {
5758 await expect ( page . getByText ( "Sponsorship" ) ) . toBeVisible ( ) ;
5859 await expect ( page . getByText ( "Code Of Conduct" ) ) . toBeVisible ( ) ;
5960 } ) ;
61+
62+ test ( "Should not be able to post a comment on an article" , async ( {
63+ page,
64+ } ) => {
65+ await page . goto ( "http://localhost:3000" ) ;
66+ // Waits for articles to be loaded
67+ await expect ( page . getByText ( "Read Full Article" ) . first ( ) ) . toBeVisible ( ) ;
68+ await page . getByText ( "Read Full Article" ) . first ( ) . click ( ) ;
69+ await page . waitForURL ( / ^ h t t p : \/ \/ l o c a l h o s t : 3 0 0 0 \/ a r t i c l e s \/ .* $ / ) ;
70+
71+ await expect ( page . getByPlaceholder ( "What do you think?" ) ) . toBeHidden ( ) ;
72+
73+ await expect ( page . getByText ( "Hey! 👋" ) ) . toBeVisible ( ) ;
74+ await expect ( page . getByText ( "Got something to say?" ) ) . toBeVisible ( ) ;
75+ await expect (
76+ page . getByText ( "Sign in or sign up to leave a comment" ) ,
77+ ) . toBeVisible ( ) ;
78+ } ) ;
6079} ) ;
6180
6281test . describe ( "Authenticated Articles Page" , ( ) => {
@@ -102,27 +121,36 @@ test.describe("Authenticated Articles Page", () => {
102121 // Waits for articles to be loaded
103122 await page . waitForSelector ( "article" ) ;
104123
105- const initialArticleCount = await page . $$eval (
106- "article" ,
107- ( articles ) => articles . length ,
108- ) ;
124+ // This delays the requests by 100ms.
125+ // This is needed as the load more article request was resolving too fast
126+ await page . route ( "**/*" , async ( route ) => {
127+ await new Promise ( ( f ) => setTimeout ( f , 100 ) ) ;
128+ await route . continue ( ) ;
129+ } ) ;
109130
110131 if ( ! isMobile ) {
111- await page . getByText ( "Code Of Conduct" ) . scrollIntoViewIfNeeded ( ) ;
112- await page . waitForTimeout ( 5000 ) ;
113- const finalArticleCount = await page . $$eval (
114- "article" ,
115- ( articles ) => articles . length ,
116- ) ;
117- expect ( finalArticleCount ) . toBeGreaterThan ( initialArticleCount ) ;
132+ const articleLocator = page . locator ( "article" ) ;
133+ const initialArticleCount = await articleLocator . count ( ) ;
134+
135+ await page
136+ . getByRole ( "link" , { name : "Code Of Conduct" } )
137+ . scrollIntoViewIfNeeded ( ) ;
138+
139+ // We expect to see the loading indicator become visible why loading and then hidden when more articles are loaded
140+ await expect ( page . getByTestId ( "article-loading-indicator" ) ) . toBeVisible ( ) ;
141+ await expect ( page . getByTestId ( "article-loading-indicator" ) ) . toBeHidden ( ) ;
142+
143+ expect ( await articleLocator . count ( ) ) . toBeGreaterThan ( initialArticleCount ) ;
118144 }
119145
120- await expect ( page . getByText ( " Home") ) . toBeVisible ( ) ;
146+ await expect ( page . getByRole ( "link" , { name : " Home" } ) ) . toBeVisible ( ) ;
121147 await expect (
122148 page . getByLabel ( "Footer" ) . getByRole ( "link" , { name : "Events" } ) ,
123149 ) . toBeVisible ( ) ;
124- await expect ( page . getByText ( "Sponsorship" ) ) . toBeVisible ( ) ;
125- await expect ( page . getByText ( "Code Of Conduct" ) ) . toBeVisible ( ) ;
150+ await expect ( page . getByRole ( "link" , { name : "Sponsorship" } ) ) . toBeVisible ( ) ;
151+ await expect (
152+ page . getByRole ( "link" , { name : "Code Of Conduct" } ) ,
153+ ) . toBeVisible ( ) ;
126154 } ) ;
127155
128156 test ( "Should write and publish an article" , async ( { page, isMobile } ) => {
@@ -155,6 +183,7 @@ test.describe("Authenticated Articles Page", () => {
155183
156184 await expect ( page . getByRole ( "button" , { name : "Next" } ) ) . toBeVisible ( ) ;
157185 await page . getByRole ( "button" , { name : "Next" } ) . click ( ) ;
186+
158187 await expect (
159188 page . getByRole ( "button" , { name : "Publish now" } ) ,
160189 ) . toBeVisible ( ) ;
@@ -176,4 +205,19 @@ test.describe("Authenticated Articles Page", () => {
176205 await expect ( page . getByLabel ( "like-trigger" ) ) . toBeVisible ( ) ;
177206 await expect ( page . getByLabel ( "bookmark-trigger" ) ) . toBeVisible ( ) ;
178207 } ) ;
208+
209+ test ( "Should post a comment on an article" , async ( { page } , workerInfo ) => {
210+ const commentContent = `This is a great read. Thanks for posting! Sent from ${ workerInfo . project . name } + ${ randomUUID ( ) } ` ;
211+ await page . goto ( "http://localhost:3000" ) ;
212+ // Waits for articles to be loaded
213+ await expect ( page . getByText ( "Read Full Article" ) . first ( ) ) . toBeVisible ( ) ;
214+ await page . getByText ( "Read Full Article" ) . first ( ) . click ( ) ;
215+ await page . waitForURL ( / ^ h t t p : \/ \/ l o c a l h o s t : 3 0 0 0 \/ a r t i c l e s \/ .* $ / ) ;
216+
217+ await expect ( page . getByPlaceholder ( "What do you think?" ) ) . toBeVisible ( ) ;
218+ await page . getByPlaceholder ( "What do you think?" ) . fill ( commentContent ) ;
219+ await page . getByRole ( "button" , { name : "Submit" } ) . click ( ) ;
220+
221+ await expect ( page . getByText ( commentContent ) ) . toBeVisible ( ) ;
222+ } ) ;
179223} ) ;
0 commit comments