Skip to content

Commit 331b2d5

Browse files
committed
tests/utils/ajax: Migrate from mirage to @crates-io/msw
1 parent 9b528c5 commit 331b2d5

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

tests/utils/ajax-test.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
11
import { module, test } from 'qunit';
22

3+
import { http, HttpResponse } from 'msw';
4+
35
import { setupTest } from 'crates-io/tests/helpers';
6+
import setupMsw from 'crates-io/tests/helpers/setup-msw';
47
import ajax, { AjaxError, HttpError } from 'crates-io/utils/ajax';
58

6-
import setupMirage from '../helpers/setup-mirage';
7-
89
module('ajax()', function (hooks) {
910
setupTest(hooks);
10-
setupMirage(hooks);
11+
setupMsw(hooks);
1112
setupFetchRestore(hooks);
1213

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 })));
1516

1617
let response = await ajax('/foo');
1718
assert.deepEqual(response, { foo: 42 });
1819
});
1920

2021
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+
);
2326

2427
let response = await ajax('/foo', { method: 'PUT' });
2528
assert.deepEqual(response, { foo: 'bar' });
2629
});
2730

2831
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 })));
3033

3134
await assert.rejects(ajax('/foo'), function (error) {
3235
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) {
5053
assert.strictEqual(cause.method, 'GET');
5154
assert.strictEqual(cause.url, '/foo');
5255
assert.ok(cause.response);
53-
assert.strictEqual(cause.response.url, '/foo');
56+
assert.ok(cause.response.url.endsWith('/foo'));
5457
return true;
5558
});
5659
});
5760

5861
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 })));
6063

6164
await assert.rejects(ajax('/foo'), function (error) {
6265
let expectedMessage = 'GET /foo failed\n\ncaused by: HttpError: GET /foo failed with: 404 Not Found';
@@ -80,13 +83,13 @@ module('ajax()', function (hooks) {
8083
assert.strictEqual(cause.method, 'GET');
8184
assert.strictEqual(cause.url, '/foo');
8285
assert.ok(cause.response);
83-
assert.strictEqual(cause.response.url, '/foo');
86+
assert.ok(cause.response.url.endsWith('/foo'));
8487
return true;
8588
});
8689
});
8790

8891
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')));
9093

9194
await assert.rejects(ajax('/foo'), function (error) {
9295
let expectedMessage = 'GET /foo failed\n\ncaused by: SyntaxError';
@@ -150,7 +153,7 @@ module('ajax()', function (hooks) {
150153

151154
module('json()', function () {
152155
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 })));
154157

155158
let error;
156159
await assert.rejects(ajax('/foo'), function (_error) {
@@ -163,7 +166,7 @@ module('ajax()', function (hooks) {
163166
});
164167

165168
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 })));
167170

168171
let error;
169172
await assert.rejects(ajax('/foo'), function (_error) {

0 commit comments

Comments
 (0)