1
+ import test , { expect } from '@playwright/test'
2
+ import { shouldBeDumpPage , setupCustomAxiosInstance } from './support'
3
+
4
+ // Extend Window interface for TypeScript
5
+ declare global {
6
+ interface Window {
7
+ Inertia : any
8
+ createInertiaApp : any
9
+ }
10
+ }
11
+
12
+ test . describe ( 'Axios Instance' , ( ) => {
13
+ test . beforeEach ( async ( { page } ) => {
14
+ await page . goto ( '/' )
15
+ } )
16
+
17
+ test ( 'uses default axios instance when no custom instance is provided' , async ( { page } ) => {
18
+ await page . goto ( '/axios-instance/test' )
19
+
20
+ // Make a request using the default axios instance
21
+ await page . evaluate ( ( ) => {
22
+ window . Inertia . visit ( '/axios-instance/test' , { method : 'post' , data : { test : 'data' } } )
23
+ } )
24
+
25
+ await page . waitForURL ( '/axios-instance/test' )
26
+
27
+ const dump = await shouldBeDumpPage ( page , 'post' )
28
+
29
+ // Should not have custom axios header
30
+ await expect ( dump . headers [ 'x-custom-axios' ] ) . toBeUndefined ( )
31
+ await expect ( dump . axiosInstanceUsed ) . toBe ( 'default' )
32
+ } )
33
+
34
+ test ( 'uses custom axios instance when provided' , async ( { page } ) => {
35
+ // Set up a custom axios instance with a custom header
36
+ await setupCustomAxiosInstance ( page )
37
+
38
+ await page . goto ( '/axios-instance/test' )
39
+
40
+ // Make a request using the custom axios instance
41
+ await page . evaluate ( ( ) => {
42
+ window . Inertia . visit ( '/axios-instance/test' , { method : 'post' , data : { test : 'data' } } )
43
+ } )
44
+
45
+ await page . waitForURL ( '/axios-instance/test' )
46
+
47
+ const dump = await shouldBeDumpPage ( page , 'post' )
48
+
49
+ // Should have custom axios header
50
+ await expect ( dump . headers [ 'x-custom-axios' ] ) . toBe ( 'custom-instance' )
51
+ await expect ( dump . axiosInstanceUsed ) . toBe ( 'custom-instance' )
52
+ } )
53
+
54
+ test ( 'custom axios instance works with different HTTP methods' , async ( { page } ) => {
55
+ // Set up a custom axios instance
56
+ await setupCustomAxiosInstance ( page )
57
+
58
+ await page . goto ( '/axios-instance/test' )
59
+
60
+ // Test GET request
61
+ await page . evaluate ( ( ) => {
62
+ window . Inertia . visit ( '/axios-instance/test?method=get' , { method : 'get' } )
63
+ } )
64
+
65
+ await page . waitForURL ( '/axios-instance/test' )
66
+
67
+ let dump = await shouldBeDumpPage ( page , 'get' )
68
+ await expect ( dump . headers [ 'x-custom-axios' ] ) . toBe ( 'custom-instance' )
69
+ await expect ( dump . axiosInstanceUsed ) . toBe ( 'custom-instance' )
70
+
71
+ // Test POST request
72
+ await page . evaluate ( ( ) => {
73
+ window . Inertia . visit ( '/axios-instance/test' , { method : 'post' , data : { test : 'data' } } )
74
+ } )
75
+
76
+ await page . waitForURL ( '/axios-instance/test' )
77
+
78
+ dump = await shouldBeDumpPage ( page , 'post' )
79
+ await expect ( dump . headers [ 'x-custom-axios' ] ) . toBe ( 'custom-instance' )
80
+ await expect ( dump . axiosInstanceUsed ) . toBe ( 'custom-instance' )
81
+ } )
82
+
83
+ test ( 'custom axios instance configuration is preserved' , async ( { page } ) => {
84
+ // Set up a custom axios instance with specific configuration
85
+ await setupCustomAxiosInstance ( page , { 'x-test-config' : 'preserved' } )
86
+
87
+ await page . goto ( '/axios-instance/test' )
88
+
89
+ await page . evaluate ( ( ) => {
90
+ window . Inertia . visit ( '/axios-instance/test' , { method : 'post' , data : { test : 'data' } } )
91
+ } )
92
+
93
+ await page . waitForURL ( '/axios-instance/test' )
94
+
95
+ const dump = await shouldBeDumpPage ( page , 'post' )
96
+
97
+ // Should have both custom headers
98
+ await expect ( dump . headers [ 'x-custom-axios' ] ) . toBe ( 'custom-instance' )
99
+ await expect ( dump . headers [ 'x-test-config' ] ) . toBe ( 'preserved' )
100
+ await expect ( dump . axiosInstanceUsed ) . toBe ( 'custom-instance' )
101
+ } )
102
+
103
+ test ( 'axios instance option is optional and backward compatible' , async ( { page } ) => {
104
+ // Test that the app works without the axiosInstance option
105
+ await page . goto ( '/axios-instance/test' )
106
+
107
+ await page . evaluate ( ( ) => {
108
+ window . Inertia . visit ( '/axios-instance/test' , { method : 'get' } )
109
+ } )
110
+
111
+ await page . waitForURL ( '/axios-instance/test' )
112
+
113
+ const dump = await shouldBeDumpPage ( page , 'get' )
114
+
115
+ // Should work normally without custom instance
116
+ await expect ( dump . method ) . toBe ( 'get' )
117
+ await expect ( dump . axiosInstanceUsed ) . toBe ( 'default' )
118
+ } )
119
+
120
+ test ( 'custom axios instance works with form submissions' , async ( { page } ) => {
121
+ // Set up a custom axios instance
122
+ await setupCustomAxiosInstance ( page )
123
+
124
+ await page . goto ( '/axios-instance/test' )
125
+
126
+ // Test form submission
127
+ await page . evaluate ( ( ) => {
128
+ const form = document . createElement ( 'form' )
129
+ form . method = 'post'
130
+ form . action = '/axios-instance/test'
131
+
132
+ const input = document . createElement ( 'input' )
133
+ input . name = 'form_field'
134
+ input . value = 'test_value'
135
+ form . appendChild ( input )
136
+
137
+ document . body . appendChild ( form )
138
+
139
+ window . Inertia . post ( '/axios-instance/test' , { form_field : 'test_value' } )
140
+ } )
141
+
142
+ await page . waitForURL ( '/axios-instance/test' )
143
+
144
+ const dump = await shouldBeDumpPage ( page , 'post' )
145
+
146
+ await expect ( dump . headers [ 'x-custom-axios' ] ) . toBe ( 'custom-instance' )
147
+ await expect ( dump . axiosInstanceUsed ) . toBe ( 'custom-instance' )
148
+ await expect ( dump . form . form_field ) . toBe ( 'test_value' )
149
+ } )
150
+ } )
0 commit comments