@@ -16,45 +16,45 @@ describe('body accessors', () => {
16
16
test ( 'works multiple times' , async ( ) => {
17
17
const mockResponse = new Response ( "Mock" , { status : 200 , headers : new Headers ( { 'Content-Type' : 'text/plain' } ) } )
18
18
const testResponse = new FetchResponse ( mockResponse )
19
-
19
+
20
+ expect ( await testResponse . text ) . toBe ( "Mock" )
20
21
expect ( await testResponse . text ) . toBe ( "Mock" )
21
- expect ( await testResponse . text ) . toBe ( "Mock" )
22
22
} )
23
23
test ( 'work regardless of content-type' , async ( ) => {
24
24
const mockResponse = new Response ( "Mock" , { status : 200 , headers : new Headers ( { 'Content-Type' : 'not/text' } ) } )
25
25
const testResponse = new FetchResponse ( mockResponse )
26
-
27
- expect ( await testResponse . text ) . toBe ( "Mock" )
26
+
27
+ expect ( await testResponse . text ) . toBe ( "Mock" )
28
28
} )
29
29
} )
30
30
describe ( 'html' , ( ) => {
31
31
test ( 'works multiple times' , async ( ) => {
32
32
const mockResponse = new Response ( "<h1>hi</h1>" , { status : 200 , headers : new Headers ( { 'Content-Type' : 'application/html' } ) } )
33
33
const testResponse = new FetchResponse ( mockResponse )
34
-
34
+
35
+ expect ( await testResponse . html ) . toBe ( "<h1>hi</h1>" )
35
36
expect ( await testResponse . html ) . toBe ( "<h1>hi</h1>" )
36
- expect ( await testResponse . html ) . toBe ( "<h1>hi</h1>" )
37
37
} )
38
38
test ( 'rejects on invalid content-type' , async ( ) => {
39
39
const mockResponse = new Response ( "<h1>hi</h1>" , { status : 200 , headers : new Headers ( { 'Content-Type' : 'text/plain' } ) } )
40
40
const testResponse = new FetchResponse ( mockResponse )
41
-
41
+
42
42
expect ( testResponse . html ) . rejects . toBeInstanceOf ( Error )
43
43
} )
44
44
} )
45
45
describe ( 'json' , ( ) => {
46
46
test ( 'works multiple times' , async ( ) => {
47
47
const mockResponse = new Response ( JSON . stringify ( { json : 'body' } ) , { status : 200 , headers : new Headers ( { 'Content-Type' : 'application/json' } ) } )
48
48
const testResponse = new FetchResponse ( mockResponse )
49
-
49
+
50
50
// works mutliple times
51
51
expect ( { json : 'body' } ) . toStrictEqual ( await testResponse . json )
52
52
expect ( { json : 'body' } ) . toStrictEqual ( await testResponse . json )
53
53
} )
54
54
test ( 'rejects on invalid content-type' , async ( ) => {
55
55
const mockResponse = new Response ( "<h1>hi</h1>" , { status : 200 , headers : new Headers ( { 'Content-Type' : 'text/json' } ) } )
56
56
const testResponse = new FetchResponse ( mockResponse )
57
-
57
+
58
58
expect ( testResponse . json ) . rejects . toBeInstanceOf ( Error )
59
59
} )
60
60
} )
@@ -85,7 +85,7 @@ describe('body accessors', () => {
85
85
const warningSpy = jest . spyOn ( console , 'warn' ) . mockImplementation ( )
86
86
87
87
await testResponse . renderTurboStream ( )
88
-
88
+
89
89
expect ( warningSpy ) . toBeCalled ( )
90
90
} )
91
91
test ( 'calls turbo' , async ( ) => {
@@ -99,10 +99,18 @@ describe('body accessors', () => {
99
99
test ( 'rejects on invalid content-type' , async ( ) => {
100
100
const mockResponse = new Response ( "<h1>hi</h1>" , { status : 200 , headers : new Headers ( { 'Content-Type' : 'text/plain' } ) } )
101
101
const testResponse = new FetchResponse ( mockResponse )
102
-
102
+
103
103
expect ( testResponse . renderTurboStream ( ) ) . rejects . toBeInstanceOf ( Error )
104
104
} )
105
105
} )
106
+ describe ( 'script' , ( ) => {
107
+ test ( 'rejects on invalid content-type' , async ( ) => {
108
+ const mockResponse = new Response ( "" , { status : 200 , headers : new Headers ( { 'Content-Type' : 'text/plain' } ) } )
109
+ const testResponse = new FetchResponse ( mockResponse )
110
+
111
+ expect ( testResponse . activeScript ( ) ) . rejects . toBeInstanceOf ( Error )
112
+ } )
113
+ } )
106
114
} )
107
115
108
116
describe ( 'fetch response helpers' , ( ) => {
@@ -135,46 +143,46 @@ describe('fetch response helpers', () => {
135
143
} )
136
144
} )
137
145
describe ( 'http-status helpers' , ( ) => {
138
-
146
+
139
147
test ( '200' , ( ) => {
140
148
const mockResponse = new Response ( null , { status : 200 } )
141
149
const testResponse = new FetchResponse ( mockResponse )
142
-
150
+
143
151
expect ( testResponse . statusCode ) . toBe ( 200 )
144
152
expect ( testResponse . ok ) . toBeTruthy ( )
145
- expect ( testResponse . redirected ) . toBeFalsy ( )
153
+ expect ( testResponse . redirected ) . toBeFalsy ( )
146
154
expect ( testResponse . unauthenticated ) . toBeFalsy ( )
147
155
expect ( testResponse . unprocessableEntity ) . toBeFalsy ( )
148
156
} )
149
-
157
+
150
158
test ( '401' , ( ) => {
151
159
const mockResponse = new Response ( null , { status : 401 } )
152
160
const testResponse = new FetchResponse ( mockResponse )
153
-
161
+
154
162
expect ( testResponse . statusCode ) . toBe ( 401 )
155
163
expect ( testResponse . ok ) . toBeFalsy ( )
156
- expect ( testResponse . redirected ) . toBeFalsy ( )
164
+ expect ( testResponse . redirected ) . toBeFalsy ( )
157
165
expect ( testResponse . unauthenticated ) . toBeTruthy ( )
158
166
expect ( testResponse . unprocessableEntity ) . toBeFalsy ( )
159
167
} )
160
-
168
+
161
169
test ( '422' , ( ) => {
162
170
const mockResponse = new Response ( null , { status : 422 } )
163
171
const testResponse = new FetchResponse ( mockResponse )
164
-
172
+
165
173
expect ( testResponse . statusCode ) . toBe ( 422 )
166
174
expect ( testResponse . ok ) . toBeFalsy ( )
167
- expect ( testResponse . redirected ) . toBeFalsy ( )
175
+ expect ( testResponse . redirected ) . toBeFalsy ( )
168
176
expect ( testResponse . unauthenticated ) . toBeFalsy ( )
169
177
expect ( testResponse . unprocessableEntity ) . toBeTruthy ( )
170
178
} )
171
-
179
+
172
180
test ( '302' , ( ) => {
173
181
const mockHeaders = new Headers ( { 'Location' : 'https://localhost/login' } )
174
182
const mockResponse = new Response ( null , { status : 302 , url : 'https://localhost/login' , headers : mockHeaders } )
175
183
jest . spyOn ( mockResponse , 'redirected' , 'get' ) . mockReturnValue ( true )
176
184
const testResponse = new FetchResponse ( mockResponse )
177
-
185
+
178
186
expect ( testResponse . statusCode ) . toBe ( 302 )
179
187
expect ( testResponse . ok ) . toBeFalsy ( )
180
188
expect ( testResponse . redirected ) . toBeTruthy ( )
0 commit comments