1
1
import { module , test } from 'qunit' ;
2
2
3
+ import { http , HttpResponse } from 'msw' ;
4
+
3
5
import { setupTest } from 'crates-io/tests/helpers' ;
6
+ import setupMsw from 'crates-io/tests/helpers/setup-msw' ;
4
7
import ajax , { AjaxError , HttpError } from 'crates-io/utils/ajax' ;
5
8
6
- import setupMirage from '../helpers/setup-mirage' ;
7
-
8
9
module ( 'ajax()' , function ( hooks ) {
9
10
setupTest ( hooks ) ;
10
- setupMirage ( hooks ) ;
11
+ setupMsw ( hooks ) ;
11
12
setupFetchRestore ( hooks ) ;
12
13
13
- test ( 'fetches a JSON document from the server ' , async function ( assert ) {
14
- this . server . get ( '/foo' , { foo : 42 } ) ;
14
+ test ( 'fetches a JSON document from the worker ' , async function ( assert ) {
15
+ this . worker . use ( http . get ( '/foo' , ( ) => HttpResponse . json ( { foo : 42 } ) ) ) ;
15
16
16
17
let response = await ajax ( '/foo' ) ;
17
18
assert . deepEqual ( response , { foo : 42 } ) ;
18
19
} ) ;
19
20
20
21
test ( 'passes additional options to `fetch()`' , async function ( assert ) {
21
- this . server . get ( '/foo' , { foo : 42 } ) ;
22
- this . server . put ( '/foo' , { foo : 'bar' } ) ;
22
+ this . worker . use (
23
+ http . get ( '/foo' , ( ) => HttpResponse . json ( { foo : 42 } ) ) ,
24
+ http . put ( '/foo' , ( ) => HttpResponse . json ( { foo : 'bar' } ) ) ,
25
+ ) ;
23
26
24
27
let response = await ajax ( '/foo' , { method : 'PUT' } ) ;
25
28
assert . deepEqual ( response , { foo : 'bar' } ) ;
26
29
} ) ;
27
30
28
31
test ( 'throws an `HttpError` for 5xx responses' , async function ( assert ) {
29
- this . server . get ( '/foo' , { foo : 42 } , 500 ) ;
32
+ this . worker . use ( http . get ( '/foo' , ( ) => HttpResponse . json ( { foo : 42 } , { status : 500 } ) ) ) ;
30
33
31
34
await assert . rejects ( ajax ( '/foo' ) , function ( error ) {
32
35
let expectedMessage = 'GET /foo failed\n\ncaused by: HttpError: GET /foo failed with: 500 Internal Server Error' ;
@@ -50,13 +53,13 @@ module('ajax()', function (hooks) {
50
53
assert . strictEqual ( cause . method , 'GET' ) ;
51
54
assert . strictEqual ( cause . url , '/foo' ) ;
52
55
assert . ok ( cause . response ) ;
53
- assert . strictEqual ( cause . response . url , '/foo' ) ;
56
+ assert . ok ( cause . response . url . endsWith ( '/foo' ) ) ;
54
57
return true ;
55
58
} ) ;
56
59
} ) ;
57
60
58
61
test ( 'throws an `HttpError` for 4xx responses' , async function ( assert ) {
59
- this . server . get ( '/foo' , { foo : 42 } , 404 ) ;
62
+ this . worker . use ( http . get ( '/foo' , ( ) => HttpResponse . json ( { foo : 42 } , { status : 404 } ) ) ) ;
60
63
61
64
await assert . rejects ( ajax ( '/foo' ) , function ( error ) {
62
65
let expectedMessage = 'GET /foo failed\n\ncaused by: HttpError: GET /foo failed with: 404 Not Found' ;
@@ -80,13 +83,13 @@ module('ajax()', function (hooks) {
80
83
assert . strictEqual ( cause . method , 'GET' ) ;
81
84
assert . strictEqual ( cause . url , '/foo' ) ;
82
85
assert . ok ( cause . response ) ;
83
- assert . strictEqual ( cause . response . url , '/foo' ) ;
86
+ assert . ok ( cause . response . url . endsWith ( '/foo' ) ) ;
84
87
return true ;
85
88
} ) ;
86
89
} ) ;
87
90
88
91
test ( 'throws an error for invalid JSON responses' , async function ( assert ) {
89
- this . server . get ( '/foo' , ( ) => '{ foo: 42' ) ;
92
+ this . worker . use ( http . get ( '/foo' , ( ) => HttpResponse . text ( '{ foo: 42' ) ) ) ;
90
93
91
94
await assert . rejects ( ajax ( '/foo' ) , function ( error ) {
92
95
let expectedMessage = 'GET /foo failed\n\ncaused by: SyntaxError' ;
@@ -150,7 +153,7 @@ module('ajax()', function (hooks) {
150
153
151
154
module ( 'json()' , function ( ) {
152
155
test ( 'resolves with the JSON payload' , async function ( assert ) {
153
- this . server . get ( '/foo' , { foo : 42 } , 500 ) ;
156
+ this . worker . use ( http . get ( '/foo' , ( ) => HttpResponse . json ( { foo : 42 } , { status : 500 } ) ) ) ;
154
157
155
158
let error ;
156
159
await assert . rejects ( ajax ( '/foo' ) , function ( _error ) {
@@ -163,7 +166,7 @@ module('ajax()', function (hooks) {
163
166
} ) ;
164
167
165
168
test ( 'resolves with `undefined` if there is no JSON payload' , async function ( assert ) {
166
- this . server . get ( '/foo' , ( ) => '{ foo: 42' , 500 ) ;
169
+ this . worker . use ( http . get ( '/foo' , ( ) => HttpResponse . text ( '{ foo: 42' , { status : 500 } ) ) ) ;
167
170
168
171
let error ;
169
172
await assert . rejects ( ajax ( '/foo' ) , function ( _error ) {
0 commit comments