@@ -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 } from 'h3'
7
7
8
8
import FetchComponent from '~/components/FetchComponent.vue'
9
9
@@ -71,4 +71,98 @@ 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
+
98
+ expect ( await fetch ( new Request ( '/native/1' , { method : 'GET' } ) ) . then ( res => res . json ( ) ) ) . toMatchObject ( { path : '/native/1' , method : 'GET' } )
99
+ expect ( await fetch ( new Request ( '/native/1' , { method : 'POST' } ) ) . then ( res => res . json ( ) ) ) . toMatchObject ( { path : '/native/1' , method : 'POST' } )
100
+ expect ( await fetch ( new Request ( 'https://jsonplaceholder.typicode.com/native/1' , { method : 'POST' } ) ) . then ( res => res . json ( ) ) )
101
+ . toMatchObject ( { path : 'https://jsonplaceholder.typicode.com/native/1' , method : 'POST' } )
102
+
103
+ expect ( await fetch ( new Request ( '/native/1' ) , { method : 'GET' } ) . then ( res => res . json ( ) ) ) . toMatchObject ( { path : '/native/1' , method : 'GET' } )
104
+ expect ( await fetch ( new Request ( '/native/1' ) , { method : 'POST' } ) . then ( res => res . json ( ) ) ) . toMatchObject ( { path : '/native/1' , method : 'POST' } )
105
+ expect ( await fetch ( new Request ( 'https://jsonplaceholder.typicode.com/native/1' ) , { method : 'POST' } ) . then ( res => res . json ( ) ) )
106
+ . toMatchObject ( { path : 'https://jsonplaceholder.typicode.com/native/1' , method : 'POST' } )
107
+ } )
108
+
109
+ it ( 'can mock fetch requests with data' , async ( ) => {
110
+ registerEndpoint ( '/with-data' , {
111
+ method : 'POST' ,
112
+ handler : async ( event ) => {
113
+ return {
114
+ body : await readBody ( event ) ,
115
+ headers : getHeaders ( event ) ,
116
+ }
117
+ } ,
118
+ } )
119
+
120
+ expect ( await $fetch < unknown > ( '/with-data' , {
121
+ method : 'POST' ,
122
+ body : { data : 'data' } ,
123
+ headers : { 'x-test' : 'test' } ,
124
+ } ) ) . toMatchObject ( {
125
+ body : { data : 'data' } ,
126
+ headers : { 'x-test' : 'test' } ,
127
+ } )
128
+ } )
129
+
130
+ it ( 'can mock native fetch requests with data' , async ( ) => {
131
+ registerEndpoint ( '/with-data-native' , {
132
+ method : 'POST' ,
133
+ handler : async ( event ) => {
134
+ return {
135
+ body : await readBody ( event ) ,
136
+ headers : getHeaders ( event ) ,
137
+ }
138
+ } ,
139
+ } )
140
+
141
+ expect ( await fetch ( '/with-data-native' , {
142
+ method : 'POST' ,
143
+ body : JSON . stringify ( { data : 'data' } ) ,
144
+ headers : { 'x-test' : 'test' , 'content-type' : 'application/json' } ,
145
+ } ) . then ( res => res . json ( ) ) ) . toMatchObject ( {
146
+ body : { data : 'data' } ,
147
+ headers : { 'x-test' : 'test' } ,
148
+ } )
149
+
150
+ const request = new Request ( '/with-data-native' , {
151
+ method : 'POST' ,
152
+ body : JSON . stringify ( { data : 'data' } ) ,
153
+ headers : { 'x-test' : 'test' , 'content-type' : 'application/json' } ,
154
+ } )
155
+ expect ( await fetch ( request ) . then ( res => res . json ( ) ) ) . toMatchObject ( {
156
+ body : { data : 'data' } ,
157
+ headers : { 'x-test' : 'test' , 'content-type' : 'application/json' } ,
158
+ } )
159
+
160
+ expect ( await fetch ( request , {
161
+ body : JSON . stringify ( { data : 'data2' } ) ,
162
+ headers : { 'x-test2' : 'test2' , 'content-type' : 'application/json' } ,
163
+ } ) . then ( res => res . json ( ) ) ) . toMatchObject ( {
164
+ body : { data : 'data2' } ,
165
+ headers : { 'x-test2' : 'test2' } ,
166
+ } )
167
+ } )
74
168
} )
0 commit comments