Skip to content

Commit 76096a3

Browse files
committed
tests/acceptance/search: Migrate from mirage to @crates-io/msw
1 parent 6b11ddb commit 76096a3

File tree

1 file changed

+69
-60
lines changed

1 file changed

+69
-60
lines changed

tests/acceptance/search-test.js

Lines changed: 69 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@ import { module, test } from 'qunit';
33

44
import { defer } from 'rsvp';
55

6+
import { loadFixtures } from '@crates-io/msw/fixtures.js';
67
import percySnapshot from '@percy/ember';
78
import a11yAudit from 'ember-a11y-testing/test-support/audit';
89
import { keyDown } from 'ember-keyboard/test-support/test-helpers';
910
import { getPageTitle } from 'ember-page-title/test-support';
11+
import { http, HttpResponse } from 'msw';
1012

1113
import { setupApplicationTest } from 'crates-io/tests/helpers';
1214

1315
import { list as listCrates } from '../../mirage/route-handlers/crates';
1416
import axeConfig from '../axe-config';
1517

1618
module('Acceptance | search', function (hooks) {
17-
setupApplicationTest(hooks);
19+
setupApplicationTest(hooks, { msw: true });
1820

1921
test('searching for "rust"', async function (assert) {
20-
this.server.loadFixtures();
22+
loadFixtures();
2123

2224
await visit('/');
2325
await fillIn('[data-test-search-input]', 'rust');
@@ -45,7 +47,7 @@ module('Acceptance | search', function (hooks) {
4547
});
4648

4749
test('searching for "rust" from query', async function (assert) {
48-
this.server.loadFixtures();
50+
loadFixtures();
4951

5052
await visit('/search?q=rust');
5153

@@ -58,7 +60,7 @@ module('Acceptance | search', function (hooks) {
5860
});
5961

6062
test('clearing search results', async function (assert) {
61-
this.server.loadFixtures();
63+
loadFixtures();
6264

6365
await visit('/search?q=rust');
6466

@@ -72,7 +74,7 @@ module('Acceptance | search', function (hooks) {
7274
});
7375

7476
test('pressing S key to focus the search bar', async function (assert) {
75-
this.server.loadFixtures();
77+
loadFixtures();
7678

7779
await visit('/');
7880

@@ -98,7 +100,7 @@ module('Acceptance | search', function (hooks) {
98100
});
99101

100102
test('check search results are by default displayed by relevance', async function (assert) {
101-
this.server.loadFixtures();
103+
loadFixtures();
102104

103105
await visit('/');
104106
await fillIn('[data-test-search-input]', 'rust');
@@ -108,10 +110,11 @@ module('Acceptance | search', function (hooks) {
108110
});
109111

110112
test('error handling when searching from the frontpage', async function (assert) {
111-
let crate = this.server.create('crate', { name: 'rust' });
112-
this.server.create('version', { crate, num: '1.0.0' });
113+
let crate = this.db.crate.create({ name: 'rust' });
114+
this.db.version.create({ crate, num: '1.0.0' });
113115

114-
this.server.get('/api/v1/crates', {}, 500);
116+
let error = HttpResponse.json({}, { status: 500 });
117+
this.worker.use(http.get('/api/v1/crates', () => error));
115118

116119
await visit('/');
117120
await fillIn('[data-test-search-input]', 'rust');
@@ -121,10 +124,8 @@ module('Acceptance | search', function (hooks) {
121124
assert.dom('[data-test-try-again-button]').isEnabled();
122125

123126
let deferred = defer();
124-
this.server.get('/api/v1/crates', async function (schema, request) {
125-
await deferred.promise;
126-
return listCrates.call(this, schema, request);
127-
});
127+
this.worker.resetHandlers();
128+
this.worker.use(http.get('/api/v1/crates', () => deferred.promise));
128129

129130
click('[data-test-try-again-button]');
130131
await waitFor('[data-test-page-header] [data-test-spinner]');
@@ -140,15 +141,16 @@ module('Acceptance | search', function (hooks) {
140141
});
141142

142143
test('error handling when searching from the search page', async function (assert) {
143-
let crate = this.server.create('crate', { name: 'rust' });
144-
this.server.create('version', { crate, num: '1.0.0' });
144+
let crate = this.db.crate.create({ name: 'rust' });
145+
this.db.version.create({ crate, num: '1.0.0' });
145146

146147
await visit('/search?q=rust');
147148
assert.dom('[data-test-crate-row]').exists({ count: 1 });
148149
assert.dom('[data-test-error-message]').doesNotExist();
149150
assert.dom('[data-test-try-again-button]').doesNotExist();
150151

151-
this.server.get('/api/v1/crates', {}, 500);
152+
let error = HttpResponse.json({}, { status: 500 });
153+
this.worker.use(http.get('/api/v1/crates', () => error));
152154

153155
await fillIn('[data-test-search-input]', 'ru');
154156
await triggerEvent('[data-test-search-form]', 'submit');
@@ -157,10 +159,8 @@ module('Acceptance | search', function (hooks) {
157159
assert.dom('[data-test-try-again-button]').isEnabled();
158160

159161
let deferred = defer();
160-
this.server.get('/api/v1/crates', async function (schema, request) {
161-
await deferred.promise;
162-
return listCrates.call(this, schema, request);
163-
});
162+
this.worker.resetHandlers();
163+
this.worker.use(http.get('/api/v1/crates', () => deferred.promise));
164164

165165
click('[data-test-try-again-button]');
166166
await waitFor('[data-test-page-header] [data-test-spinner]');
@@ -174,64 +174,73 @@ module('Acceptance | search', function (hooks) {
174174
});
175175

176176
test('passes query parameters to the backend', async function (assert) {
177-
this.server.get('/api/v1/crates', function (schema, request) {
178-
assert.step('/api/v1/crates');
179-
180-
assert.deepEqual(request.queryParams, {
181-
all_keywords: 'fire ball',
182-
page: '3',
183-
per_page: '15',
184-
q: 'rust',
185-
sort: 'new',
186-
});
187-
188-
return { crates: [], meta: { total: 0 } };
189-
});
177+
this.worker.use(
178+
http.get('/api/v1/crates', function ({ request }) {
179+
assert.step('/api/v1/crates');
180+
181+
let url = new URL(request.url);
182+
assert.deepEqual(Object.fromEntries(url.searchParams.entries()), {
183+
all_keywords: 'fire ball',
184+
page: '3',
185+
per_page: '15',
186+
q: 'rust',
187+
sort: 'new',
188+
});
189+
190+
return HttpResponse.json({ crates: [], meta: { total: 0 } });
191+
}),
192+
);
190193

191194
await visit('/search?q=rust&page=3&per_page=15&sort=new&all_keywords=fire ball');
192195
assert.verifySteps(['/api/v1/crates']);
193196
});
194197

195198
test('supports `keyword:bla` filters', async function (assert) {
196-
this.server.get('/api/v1/crates', function (schema, request) {
197-
assert.step('/api/v1/crates');
198-
199-
assert.deepEqual(request.queryParams, {
200-
all_keywords: 'fire ball',
201-
page: '3',
202-
per_page: '15',
203-
q: 'rust',
204-
sort: 'new',
205-
});
206-
207-
return { crates: [], meta: { total: 0 } };
208-
});
199+
this.worker.use(
200+
http.get('/api/v1/crates', function ({ request }) {
201+
assert.step('/api/v1/crates');
202+
203+
let url = new URL(request.url);
204+
assert.deepEqual(Object.fromEntries(url.searchParams.entries()), {
205+
all_keywords: 'fire ball',
206+
page: '3',
207+
per_page: '15',
208+
q: 'rust',
209+
sort: 'new',
210+
});
211+
212+
return HttpResponse.json({ crates: [], meta: { total: 0 } });
213+
}),
214+
);
209215

210216
await visit('/search?q=rust keyword:fire keyword:ball&page=3&per_page=15&sort=new');
211217
assert.verifySteps(['/api/v1/crates']);
212218
});
213219

214220
test('`all_keywords` query parameter takes precedence over `keyword` filters', async function (assert) {
215-
this.server.get('/api/v1/crates', function (schema, request) {
216-
assert.step('/api/v1/crates');
217-
218-
assert.deepEqual(request.queryParams, {
219-
all_keywords: 'fire ball',
220-
page: '3',
221-
per_page: '15',
222-
q: 'rust keywords:foo',
223-
sort: 'new',
224-
});
225-
226-
return { crates: [], meta: { total: 0 } };
227-
});
221+
this.worker.use(
222+
http.get('/api/v1/crates', function ({ request }) {
223+
assert.step('/api/v1/crates');
224+
225+
let url = new URL(request.url);
226+
assert.deepEqual(Object.fromEntries(url.searchParams.entries()), {
227+
all_keywords: 'fire ball',
228+
page: '3',
229+
per_page: '15',
230+
q: 'rust keywords:foo',
231+
sort: 'new',
232+
});
233+
234+
return HttpResponse.json({ crates: [], meta: { total: 0 } });
235+
}),
236+
);
228237

229238
await visit('/search?q=rust keywords:foo&page=3&per_page=15&sort=new&all_keywords=fire ball');
230239
assert.verifySteps(['/api/v1/crates']);
231240
});
232241

233242
test('visiting without query parameters works', async function (assert) {
234-
this.server.loadFixtures();
243+
loadFixtures();
235244

236245
await visit('/search');
237246

0 commit comments

Comments
 (0)