1- import { test , expect } from '@playwright/test' ;
1+ import { test , expect , Page } from '@playwright/test' ;
22
33test . describe ( 'Produkter' , ( ) => {
44 test . beforeEach ( async ( { page } ) => {
5+ // Mock the cart API response
6+ await page . route ( '**/graphql' , async ( route ) => {
7+ const json = await route . request ( ) . postData ( ) ;
8+ if ( json ?. includes ( 'AddToCart' ) ) {
9+ await route . fulfill ( {
10+ status : 200 ,
11+ contentType : 'application/json' ,
12+ body : JSON . stringify ( {
13+ data : {
14+ addToCart : {
15+ added : true ,
16+ cartKey : 'test-cart-key' ,
17+ cart : {
18+ contents : {
19+ nodes : [
20+ {
21+ key : 'test-cart-key' ,
22+ product : {
23+ node : {
24+ name : 'Test simple' ,
25+ id : '29'
26+ }
27+ } ,
28+ quantity : 1 ,
29+ total : '100'
30+ }
31+ ]
32+ } ,
33+ total : '100' ,
34+ subtotal : '100' ,
35+ totalProductsCount : 1
36+ }
37+ }
38+ }
39+ } )
40+ } ) ;
41+ } else {
42+ await route . continue ( ) ;
43+ }
44+ } ) ;
45+
546 await page . goto ( 'http://localhost:3000' ) ;
647 } ) ;
748
@@ -15,51 +56,35 @@ test.describe('Produkter', () => {
1556
1657 await expect ( page ) . toHaveURL ( / .* s i m p l e / ) ;
1758
59+ // Verify buy button is visible
1860 await expect ( page . getByRole ( 'button' , { name : 'KJØP' } ) ) . toBeVisible ( ) ;
1961
20- // Click the buy button and wait for it to complete
62+ // Click buy button and wait for the mocked API response
63+ const responsePromise = page . waitForResponse ( response =>
64+ response . url ( ) . includes ( 'graphql' ) &&
65+ response . request ( ) . postData ( ) ?. includes ( 'AddToCart' )
66+ ) ;
2167 await page . getByRole ( 'button' , { name : 'KJØP' } ) . click ( ) ;
22-
23- // Wait for network idle to ensure any API calls complete
24- await page . waitForLoadState ( 'networkidle' ) ;
68+ await responsePromise ;
2569
26- // More specific selector for the cart count and consistent timeout
27- const cartCountSelector = '#header' ;
28-
29- // Wait for cart count to be visible and equal to "1"
30- await expect ( page . locator ( cartCountSelector ) . getByText ( '1' ) ) . toBeVisible ( {
31- timeout : 30000
32- } ) ;
70+ // Verify cart count updates in header
71+ await expect ( page . locator ( '#header' ) . getByText ( '1' ) ) . toBeVisible ( ) ;
3372
73+ // Navigate to cart
3474 await page . getByRole ( 'link' , { name : 'Handlekurv' } ) . click ( ) ;
35-
36- await page . locator ( 'section' ) . filter ( { hasText : 'Handlekurv' } ) . waitFor ( ) ;
37-
38- // Check that that Handlekurv is visible
3975 await expect (
40- page . locator ( 'section' ) . filter ( { hasText : 'Handlekurv' } ) ,
76+ page . locator ( 'section' ) . filter ( { hasText : 'Handlekurv' } )
4177 ) . toBeVisible ( ) ;
4278
43- // Check that we can go to Kasse
44-
79+ // Navigate to checkout
4580 await page . getByRole ( 'button' , { name : 'GÅ TIL KASSE' } ) . click ( ) ;
46-
47- await page . waitForURL ( 'http://localhost:3000/kasse' , {
48- waitUntil : 'networkidle' ,
49- } ) ;
50-
81+ await page . waitForURL ( 'http://localhost:3000/kasse' ) ;
5182 await expect (
52- page . locator ( 'section' ) . filter ( { hasText : 'Kasse' } ) ,
83+ page . locator ( 'section' ) . filter ( { hasText : 'Kasse' } )
5384 ) . toBeVisible ( ) ;
5485
55- // Check that we can type something in Billing fields
56-
86+ // Test billing form
5787 await page . getByPlaceholder ( 'Etternavn' ) . fill ( 'testetternavn' ) ;
58-
59- await page . getByPlaceholder ( 'Etternavn' ) . waitFor ( ) ;
60-
61- await expect ( page . getByPlaceholder ( 'Etternavn' ) ) . toHaveValue (
62- 'testetternavn' ,
63- ) ;
88+ await expect ( page . getByPlaceholder ( 'Etternavn' ) ) . toHaveValue ( 'testetternavn' ) ;
6489 } ) ;
6590} ) ;
0 commit comments