1
- import { test , expect } from '@playwright/test' ;
1
+ import { test , expect , Page } from '@playwright/test' ;
2
2
3
3
test . describe ( 'Produkter' , ( ) => {
4
4
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
+
5
46
await page . goto ( 'http://localhost:3000' ) ;
6
47
} ) ;
7
48
@@ -15,51 +56,35 @@ test.describe('Produkter', () => {
15
56
16
57
await expect ( page ) . toHaveURL ( / .* s i m p l e / ) ;
17
58
59
+ // Verify buy button is visible
18
60
await expect ( page . getByRole ( 'button' , { name : 'KJØP' } ) ) . toBeVisible ( ) ;
19
61
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
+ ) ;
21
67
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 ;
25
69
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 ( ) ;
33
72
73
+ // Navigate to cart
34
74
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
39
75
await expect (
40
- page . locator ( 'section' ) . filter ( { hasText : 'Handlekurv' } ) ,
76
+ page . locator ( 'section' ) . filter ( { hasText : 'Handlekurv' } )
41
77
) . toBeVisible ( ) ;
42
78
43
- // Check that we can go to Kasse
44
-
79
+ // Navigate to checkout
45
80
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' ) ;
51
82
await expect (
52
- page . locator ( 'section' ) . filter ( { hasText : 'Kasse' } ) ,
83
+ page . locator ( 'section' ) . filter ( { hasText : 'Kasse' } )
53
84
) . toBeVisible ( ) ;
54
85
55
- // Check that we can type something in Billing fields
56
-
86
+ // Test billing form
57
87
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' ) ;
64
89
} ) ;
65
90
} ) ;
0 commit comments