1
1
import _contentType , { type ParsedMediaType } from "content-type" ;
2
2
import { when } from "jest-when" ;
3
3
import type { DeepPartial } from "../types" ;
4
- import transform from "./transform" ;
4
+ import transform , { createTransform , transformDefaultJson } from "./transform" ;
5
5
6
6
jest . mock ( "content-type" ) ;
7
7
const contentType = _contentType as unknown as jest . Mocked < typeof _contentType > ;
8
8
9
+ const contentTypes = [
10
+ {
11
+ type : "application/json" ,
12
+ method : "json" ,
13
+ content : { some : "prop" } ,
14
+ hasExplicitSupport : true ,
15
+ } ,
16
+ {
17
+ type : "text/plain" ,
18
+ method : "text" ,
19
+ content : "Text" ,
20
+ hasExplicitSupport : true ,
21
+ } ,
22
+ {
23
+ type : "application/pdf" ,
24
+ method : "blob" ,
25
+ content : Symbol ( "Blob" ) ,
26
+ hasExplicitSupport : false ,
27
+ } ,
28
+ {
29
+ type : "application/zip" ,
30
+ method : "blob" ,
31
+ content : Symbol ( "Blob" ) ,
32
+ hasExplicitSupport : false ,
33
+ } ,
34
+ {
35
+ type : "application/octet-stream" ,
36
+ method : "blob" ,
37
+ content : Symbol ( "Blob" ) ,
38
+ hasExplicitSupport : false ,
39
+ } ,
40
+ ] as const satisfies {
41
+ type : string ;
42
+ method : string ;
43
+ content : unknown ;
44
+ hasExplicitSupport : boolean ;
45
+ } [ ] ;
46
+
9
47
describe ( "transform" , ( ) => {
10
48
it ( "should resolve a 204 status as null" , async ( ) => {
11
49
await expect (
@@ -28,33 +66,7 @@ describe("transform", () => {
28
66
) . resolves . toBe ( text ) ;
29
67
} ) ;
30
68
31
- [
32
- {
33
- type : "application/json" ,
34
- method : "json" ,
35
- content : { some : "prop" } ,
36
- } ,
37
- {
38
- type : "text/plain" ,
39
- method : "text" ,
40
- content : "Text" ,
41
- } ,
42
- {
43
- type : "application/pdf" ,
44
- method : "blob" ,
45
- content : Symbol ( "Blob" ) ,
46
- } ,
47
- {
48
- type : "application/zip" ,
49
- method : "blob" ,
50
- content : Symbol ( "Blob" ) ,
51
- } ,
52
- {
53
- type : "application/octet-stream" ,
54
- method : "blob" ,
55
- content : Symbol ( "Blob" ) ,
56
- } ,
57
- ] . forEach ( ( { type, method, content } ) => {
69
+ contentTypes . forEach ( ( { type, method, content } ) => {
58
70
it ( `should use ${ method } () for ${ type } ` , async ( ) => {
59
71
const header = `${ type } ; charset=utf-8` ;
60
72
when ( contentType . parse )
@@ -76,3 +88,111 @@ describe("transform", () => {
76
88
} ) ;
77
89
} ) ;
78
90
} ) ;
91
+
92
+ describe ( "createTransform" , ( ) => {
93
+ it ( "should resolve a 204 status as null" , async ( ) => {
94
+ const customTransform = createTransform ( ) ;
95
+ await expect (
96
+ customTransform ( { status : 204 } as Partial < Response > as Response ) ,
97
+ ) . resolves . toBe ( null ) ;
98
+ } ) ;
99
+
100
+ it ( "should resolve a response without a content type as the defaultTransform result" , async ( ) => {
101
+ const result = "transformedValue" ;
102
+ const customTransform = createTransform ( {
103
+ defaultTransform : ( ) => Promise . resolve ( result ) ,
104
+ } ) ;
105
+ await expect (
106
+ customTransform ( {
107
+ status : 200 ,
108
+ headers : {
109
+ get : jest . fn ( ) . mockReturnValue ( null ) ,
110
+ } ,
111
+ } as DeepPartial < Response > as Response ) ,
112
+ ) . resolves . toBe ( result ) ;
113
+ } ) ;
114
+
115
+ contentTypes . forEach ( ( { type, method, content, hasExplicitSupport } ) => {
116
+ const result = "transformedValue" ;
117
+ const customTransform = createTransform ( {
118
+ defaultTransform : ( ) => Promise . resolve ( result ) ,
119
+ } ) ;
120
+
121
+ it (
122
+ hasExplicitSupport
123
+ ? `should use ${ method } () for ${ type } `
124
+ : `should use the provided transform for type ${ type } ` ,
125
+ async ( ) => {
126
+ const header = `${ type } ; charset=utf-8` ;
127
+ when ( contentType . parse )
128
+ . calledWith ( header )
129
+ . mockReturnValue ( {
130
+ type,
131
+ } as Partial < ParsedMediaType > as ParsedMediaType ) ;
132
+ const getHeaders = jest . fn ( ) ;
133
+ when ( getHeaders ) . calledWith ( "Content-Type" ) . mockReturnValue ( header ) ;
134
+ await expect (
135
+ customTransform ( {
136
+ status : 200 ,
137
+ headers : {
138
+ get : getHeaders ,
139
+ } ,
140
+ [ method ] : async ( ) => content ,
141
+ } as DeepPartial < Response > as Response ) ,
142
+ ) . resolves . toEqual ( hasExplicitSupport ? content : result ) ;
143
+ } ,
144
+ ) ;
145
+ } ) ;
146
+ } ) ;
147
+
148
+ describe ( "transformDefaultJson" , ( ) => {
149
+ it ( "should resolve a 204 status as null" , async ( ) => {
150
+ await expect (
151
+ transformDefaultJson ( {
152
+ status : 204 ,
153
+ } as Partial < Response > as Response ) ,
154
+ ) . resolves . toBe ( null ) ;
155
+ } ) ;
156
+
157
+ it ( "should resolve a response without a content type as the json result" , async ( ) => {
158
+ const result = "transformedValue" ;
159
+ await expect (
160
+ transformDefaultJson ( {
161
+ status : 200 ,
162
+ headers : {
163
+ get : jest . fn ( ) . mockReturnValue ( null ) ,
164
+ } ,
165
+ json : ( ) => Promise . resolve ( result ) ,
166
+ } as DeepPartial < Response > as Response ) ,
167
+ ) . resolves . toBe ( result ) ;
168
+ } ) ;
169
+
170
+ contentTypes . forEach ( ( { type, method, content, hasExplicitSupport } ) => {
171
+ const result = { key : "value" } ;
172
+ it (
173
+ hasExplicitSupport
174
+ ? `should use ${ method } () for ${ type } `
175
+ : `should use the json transform for type ${ type } ` ,
176
+ async ( ) => {
177
+ const header = `${ type } ; charset=utf-8` ;
178
+ when ( contentType . parse )
179
+ . calledWith ( header )
180
+ . mockReturnValue ( {
181
+ type,
182
+ } as Partial < ParsedMediaType > as ParsedMediaType ) ;
183
+ const getHeaders = jest . fn ( ) ;
184
+ when ( getHeaders ) . calledWith ( "Content-Type" ) . mockReturnValue ( header ) ;
185
+ await expect (
186
+ transformDefaultJson ( {
187
+ status : 200 ,
188
+ headers : {
189
+ get : getHeaders ,
190
+ } ,
191
+ json : ( ) => Promise . resolve ( result ) ,
192
+ [ method ] : async ( ) => content ,
193
+ } as DeepPartial < Response > as Response ) ,
194
+ ) . resolves . toEqual ( hasExplicitSupport ? content : result ) ;
195
+ } ,
196
+ ) ;
197
+ } ) ;
198
+ } ) ;
0 commit comments