@@ -2,39 +2,34 @@ jest.setTimeout(60000)
2
2
3
3
const { Nuxt, Builder } = require ( 'nuxt-edge' )
4
4
const axios = require ( 'axios' )
5
+
5
6
const config = require ( './fixture/nuxt.config' )
6
7
7
- const url = path => `http://localhost:3000 ${ path } `
8
+ let nuxt , addTemplate
8
9
9
- describe ( 'axios module' , ( ) => {
10
- let nuxt
11
- let addTemplate
10
+ const url = path => `http://localhost:3000${ path } `
12
11
13
- beforeAll ( async ( ) => {
14
- nuxt = new Nuxt ( config )
12
+ const setupNuxt = async ( config ) => {
13
+ nuxt = new Nuxt ( config )
15
14
16
- // Spy addTemplate
17
- addTemplate = nuxt . moduleContainer . addTemplate = jest . fn (
18
- nuxt . moduleContainer . addTemplate
19
- )
15
+ // Spy addTemplate
16
+ addTemplate = nuxt . moduleContainer . addTemplate = jest . fn (
17
+ nuxt . moduleContainer . addTemplate
18
+ )
20
19
21
- await new Builder ( nuxt ) . build ( )
22
- await nuxt . listen ( 3000 )
23
- } )
20
+ const build = new Builder ( nuxt )
24
21
25
- afterAll ( async ( ) => {
26
- await nuxt . close ( )
27
- } )
22
+ await build . validatePages ( )
23
+ await build . generateRoutesAndFiles ( )
24
+ await nuxt . listen ( 3000 )
25
+ }
28
26
27
+ const testSuite = ( ) => {
29
28
test ( 'baseURL' , ( ) => {
30
29
expect ( addTemplate ) . toBeDefined ( )
31
- const call = addTemplate . mock . calls . find ( args =>
32
- args [ 0 ] . src . includes ( 'plugin.js' )
33
- )
30
+ const call = addTemplate . mock . calls . find ( args => args [ 0 ] . src . includes ( 'plugin.js' ) )
34
31
const options = call [ 0 ] . options
35
- expect ( options . baseURL . toString ( ) ) . toBe (
36
- `http://localhost:3000/test_api`
37
- )
32
+ expect ( options . baseURL . toString ( ) ) . toBe ( 'http://localhost:3000/test_api' )
38
33
expect ( options . browserBaseURL . toString ( ) ) . toBe ( '/test_api' )
39
34
} )
40
35
@@ -60,12 +55,11 @@ describe('axios module', () => {
60
55
} )
61
56
62
57
test ( 'ssr' , async ( ) => {
63
- const makeReq = login =>
64
- axios
65
- . get ( url ( '/ssr' + ( login ? '?login' : '' ) ) )
66
- . then ( r => r . data )
67
- . then ( h => / s e s s i o n - [ 0 - 9 ] + / . exec ( h ) )
68
- . then ( m => ( m && m [ 0 ] ? m [ 0 ] : null ) )
58
+ const makeReq = login => axios
59
+ . get ( url ( '/ssr' + ( login ? '?login' : '' ) ) )
60
+ . then ( r => r . data )
61
+ . then ( h => / s e s s i o n - [ 0 - 9 ] + / . exec ( h ) )
62
+ . then ( m => ( m && m [ 0 ] ? m [ 0 ] : null ) )
69
63
70
64
const a = await makeReq ( )
71
65
const b = await makeReq ( true )
@@ -80,15 +74,96 @@ describe('axios module', () => {
80
74
} )
81
75
82
76
test ( 'ssr no brotli' , async ( ) => {
83
- const makeReq = login =>
84
- axios
85
- . get ( url ( '/ssr' + ( login ? '?login' : '' ) ) )
86
- . then ( r => r . data )
87
- . then ( h => / e n c o d i n g - \$ ( .* ) \$ / . exec ( h ) )
88
- . then ( m => ( m && m [ 1 ] ? m [ 1 ] : null ) )
77
+ const makeReq = login => axios
78
+ . get ( url ( '/ssr' + ( login ? '?login' : '' ) ) )
79
+ . then ( r => r . data )
80
+ . then ( h => / e n c o d i n g - \$ ( .* ) \$ / . exec ( h ) )
81
+ . then ( m => ( m && m [ 1 ] ? m [ 1 ] : null ) )
89
82
90
83
const result = await makeReq ( )
91
84
92
85
expect ( result ) . toBe ( 'gzip, deflate' )
93
86
} )
87
+ }
88
+
89
+ describe ( 'module' , ( ) => {
90
+ beforeAll ( async ( ) => {
91
+ nuxt = new Nuxt ( config )
92
+
93
+ // Spy addTemplate
94
+ addTemplate = nuxt . moduleContainer . addTemplate = jest . fn (
95
+ nuxt . moduleContainer . addTemplate
96
+ )
97
+
98
+ await new Builder ( nuxt ) . build ( )
99
+ await nuxt . listen ( 3000 )
100
+ } )
101
+
102
+ afterAll ( async ( ) => {
103
+ await nuxt . close ( )
104
+ } )
105
+
106
+ testSuite ( )
107
+ } )
108
+
109
+ describe ( 'other options' , ( ) => {
110
+ beforeAll ( async ( ) => {
111
+ config . axios = {
112
+ prefix : '/test_api' ,
113
+ proxy : { } ,
114
+ credentials : true ,
115
+ https : true ,
116
+ retry : false
117
+ }
118
+
119
+ await setupNuxt ( config )
120
+ } )
121
+
122
+ afterAll ( async ( ) => {
123
+ await nuxt . close ( )
124
+ } )
125
+
126
+ testSuite ( )
127
+ } )
128
+
129
+ describe ( 'browserBaseURL' , ( ) => {
130
+ beforeAll ( async ( ) => {
131
+ config . axios = {
132
+ browserBaseURL : '/test_api'
133
+ }
134
+
135
+ await setupNuxt ( config )
136
+ } )
137
+
138
+ afterAll ( async ( ) => {
139
+ await nuxt . close ( )
140
+ } )
141
+
142
+ test ( 'custom' , ( ) => {
143
+ expect ( addTemplate ) . toBeDefined ( )
144
+ const call = addTemplate . mock . calls . find ( args => args [ 0 ] . src . includes ( 'plugin.js' ) )
145
+ const options = call [ 0 ] . options
146
+ expect ( options . baseURL . toString ( ) ) . toBe ( 'http://localhost:3000/' )
147
+ expect ( options . browserBaseURL . toString ( ) ) . toBe ( '/test_api' )
148
+ } )
149
+ } )
150
+
151
+ describe ( 'empty config' , ( ) => {
152
+ beforeAll ( async ( ) => {
153
+ config . axios = { }
154
+
155
+ await setupNuxt ( config )
156
+ } )
157
+
158
+ afterAll ( async ( ) => {
159
+ await nuxt . close ( )
160
+ } )
161
+
162
+ test ( 'preset baseURL and browserBaseURL' , ( ) => {
163
+ expect ( addTemplate ) . toBeDefined ( )
164
+ const call = addTemplate . mock . calls . find ( args => args [ 0 ] . src . includes ( 'plugin.js' ) )
165
+ const options = call [ 0 ] . options
166
+ expect ( options . baseURL . toString ( ) ) . toBe ( 'http://localhost:3000/' )
167
+ expect ( options . browserBaseURL . toString ( ) ) . toBe ( 'http://localhost:3000/' )
168
+ } )
94
169
} )
0 commit comments