@@ -3,7 +3,7 @@ import { describe, expect, it } from 'vitest'
3
3
import { mountSuspended , registerEndpoint } from '@nuxt/test-utils/runtime'
4
4
5
5
import { listen } from 'listhen'
6
- import { createApp , eventHandler , toNodeListener } from 'h3'
6
+ import { createApp , eventHandler , toNodeListener , readBody , getHeaders , getQuery } from 'h3'
7
7
8
8
import FetchComponent from '~/components/FetchComponent.vue'
9
9
@@ -71,4 +71,109 @@ describe('server mocks and data fetching', () => {
71
71
} )
72
72
expect ( await $fetch < unknown > ( '/method' ) ) . toMatchObject ( { method : 'GET' } )
73
73
} )
74
+
75
+ it ( 'can mock native fetch requests' , async ( ) => {
76
+ registerEndpoint ( '/native/1' , {
77
+ method : 'POST' ,
78
+ handler : ( ) => ( { path : '/native/1' , method : 'POST' } ) ,
79
+ } )
80
+ registerEndpoint ( '/native/1' , {
81
+ method : 'GET' ,
82
+ handler : ( ) => ( { path : '/native/1' , method : 'GET' } ) ,
83
+ } )
84
+ registerEndpoint ( 'https://jsonplaceholder.typicode.com/native/1' , {
85
+ method : 'GET' ,
86
+ handler : ( ) => ( { path : 'https://jsonplaceholder.typicode.com/native/1' , method : 'GET' } ) ,
87
+ } )
88
+ registerEndpoint ( 'https://jsonplaceholder.typicode.com/native/1' , {
89
+ method : 'POST' ,
90
+ handler : ( ) => ( { path : 'https://jsonplaceholder.typicode.com/native/1' , method : 'POST' } ) ,
91
+ } )
92
+
93
+ expect ( await fetch ( '/native/1' ) . then ( res => res . json ( ) ) ) . toMatchObject ( { path : '/native/1' , method : 'GET' } )
94
+ expect ( await fetch ( '/native/1' , { method : 'POST' } ) . then ( res => res . json ( ) ) ) . toMatchObject ( { path : '/native/1' , method : 'POST' } )
95
+ expect ( await fetch ( 'https://jsonplaceholder.typicode.com/native/1' ) . then ( res => res . json ( ) ) )
96
+ . toMatchObject ( { path : 'https://jsonplaceholder.typicode.com/native/1' , method : 'GET' } )
97
+ expect ( await fetch ( 'https://jsonplaceholder.typicode.com/native/1' , { method : 'POST' } ) . then ( res => res . json ( ) ) )
98
+ . toMatchObject ( { path : 'https://jsonplaceholder.typicode.com/native/1' , method : 'POST' } )
99
+ } )
100
+
101
+ it ( 'can mock fetch requests with data' , async ( ) => {
102
+ registerEndpoint ( '/with-data' , {
103
+ method : 'POST' ,
104
+ handler : async ( event ) => {
105
+ return {
106
+ body : await readBody ( event ) ,
107
+ headers : getHeaders ( event ) ,
108
+ }
109
+ } ,
110
+ } )
111
+
112
+ expect ( await $fetch < unknown > ( '/with-data' , {
113
+ method : 'POST' ,
114
+ body : { data : 'data' } ,
115
+ headers : { 'x-test' : 'test' } ,
116
+ } ) ) . toMatchObject ( {
117
+ body : { data : 'data' } ,
118
+ headers : { 'x-test' : 'test' } ,
119
+ } )
120
+
121
+ expect ( await fetch ( '/with-data' , {
122
+ method : 'POST' ,
123
+ body : JSON . stringify ( { data : 'data' } ) ,
124
+ headers : { 'x-test' : 'test' , 'content-type' : 'application/json' } ,
125
+ } ) . then ( res => res . json ( ) ) ) . toMatchObject ( {
126
+ body : { data : 'data' } ,
127
+ headers : { 'x-test' : 'test' } ,
128
+ } )
129
+ } )
130
+
131
+ it ( 'can mock fetch requests with query' , async ( ) => {
132
+ registerEndpoint ( '/with-data' , {
133
+ method : 'GET' ,
134
+ handler : async ( event ) => {
135
+ return {
136
+ query : getQuery ( event ) ,
137
+ }
138
+ } ,
139
+ } )
140
+
141
+ expect ( await $fetch < unknown > ( '/with-data' , { query : { q : 1 } } ) ) . toMatchObject ( { query : { q : '1' } } )
142
+ expect ( await fetch ( '/with-data?q=1' ) . then ( res => res . json ( ) ) ) . toMatchObject ( { query : { q : '1' } } )
143
+ } )
144
+
145
+ it ( 'can mock fetch requests with Request' , async ( ) => {
146
+ registerEndpoint ( 'http://localhost:3000/with-request' , {
147
+ method : 'GET' ,
148
+ handler : ( event ) => {
149
+ return { title : 'with-request' , data : getQuery ( event ) }
150
+ } ,
151
+ } )
152
+ registerEndpoint ( 'http://localhost:3000/with-request' , {
153
+ method : 'POST' ,
154
+ handler : async ( event ) => {
155
+ return { title : 'with-request' , data : await readBody ( event ) }
156
+ } ,
157
+ } )
158
+
159
+ const request = new Request ( '/with-request?q=1' , { headers : { 'content-type' : 'application/json' } } )
160
+
161
+ expect ( await $fetch < unknown > ( request ) ) . toMatchObject ( { title : 'with-request' , data : { q : '1' } } )
162
+ expect ( await fetch ( request ) . then ( res => res . json ( ) ) ) . toMatchObject ( { title : 'with-request' , data : { q : '1' } } )
163
+
164
+ expect ( await $fetch < unknown > ( request , { method : 'POST' , body : [ 1 ] } ) ) . toMatchObject ( { title : 'with-request' , data : [ 1 ] } )
165
+ expect ( await fetch ( request , { method : 'POST' , body : '[1]' } ) . then ( res => res . json ( ) ) ) . toMatchObject ( { title : 'with-request' , data : [ 1 ] } )
166
+ } )
167
+
168
+ it ( 'can mock fetch requests with URL' , async ( ) => {
169
+ registerEndpoint ( 'http://localhost:3000/with-url' , {
170
+ method : 'GET' ,
171
+ handler : ( event ) => {
172
+ return { title : 'with-url' , data : getQuery ( event ) }
173
+ } ,
174
+ } )
175
+
176
+ expect ( await fetch ( new URL ( 'http://localhost:3000/with-url' ) ) . then ( res => res . json ( ) ) ) . toMatchObject ( { title : 'with-url' , data : { } } )
177
+ expect ( await fetch ( new URL ( 'http://localhost:3000/with-url?q=1' ) ) . then ( res => res . json ( ) ) ) . toMatchObject ( { title : 'with-url' , data : { q : '1' } } )
178
+ } )
74
179
} )
0 commit comments