Skip to content

Commit 4200f30

Browse files
committed
tests/acceptance/search: Migrate from mirage to @crates-io/msw
1 parent f9f8b5a commit 4200f30

File tree

1 file changed

+69
-61
lines changed

1 file changed

+69
-61
lines changed

tests/acceptance/search-test.js

Lines changed: 69 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@ 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

13-
import { list as listCrates } from '../../mirage/route-handlers/crates';
1415
import axeConfig from '../axe-config';
1516

1617
module('Acceptance | search', function (hooks) {
17-
setupApplicationTest(hooks);
18+
setupApplicationTest(hooks, { msw: true });
1819

1920
test('searching for "rust"', async function (assert) {
20-
this.server.loadFixtures();
21+
loadFixtures(this.db);
2122

2223
await visit('/');
2324
await fillIn('[data-test-search-input]', 'rust');
@@ -45,7 +46,7 @@ module('Acceptance | search', function (hooks) {
4546
});
4647

4748
test('searching for "rust" from query', async function (assert) {
48-
this.server.loadFixtures();
49+
loadFixtures(this.db);
4950

5051
await visit('/search?q=rust');
5152

@@ -58,7 +59,7 @@ module('Acceptance | search', function (hooks) {
5859
});
5960

6061
test('clearing search results', async function (assert) {
61-
this.server.loadFixtures();
62+
loadFixtures(this.db);
6263

6364
await visit('/search?q=rust');
6465

@@ -72,7 +73,7 @@ module('Acceptance | search', function (hooks) {
7273
});
7374

7475
test('pressing S key to focus the search bar', async function (assert) {
75-
this.server.loadFixtures();
76+
loadFixtures(this.db);
7677

7778
await visit('/');
7879

@@ -98,7 +99,7 @@ module('Acceptance | search', function (hooks) {
9899
});
99100

100101
test('check search results are by default displayed by relevance', async function (assert) {
101-
this.server.loadFixtures();
102+
loadFixtures(this.db);
102103

103104
await visit('/');
104105
await fillIn('[data-test-search-input]', 'rust');
@@ -108,10 +109,11 @@ module('Acceptance | search', function (hooks) {
108109
});
109110

110111
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' });
112+
let crate = this.db.crate.create({ name: 'rust' });
113+
this.db.version.create({ crate, num: '1.0.0' });
113114

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

116118
await visit('/');
117119
await fillIn('[data-test-search-input]', 'rust');
@@ -121,10 +123,8 @@ module('Acceptance | search', function (hooks) {
121123
assert.dom('[data-test-try-again-button]').isEnabled();
122124

123125
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-
});
126+
this.worker.resetHandlers();
127+
this.worker.use(http.get('/api/v1/crates', () => deferred.promise));
128128

129129
click('[data-test-try-again-button]');
130130
await waitFor('[data-test-page-header] [data-test-spinner]');
@@ -140,15 +140,16 @@ module('Acceptance | search', function (hooks) {
140140
});
141141

142142
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' });
143+
let crate = this.db.crate.create({ name: 'rust' });
144+
this.db.version.create({ crate, num: '1.0.0' });
145145

146146
await visit('/search?q=rust');
147147
assert.dom('[data-test-crate-row]').exists({ count: 1 });
148148
assert.dom('[data-test-error-message]').doesNotExist();
149149
assert.dom('[data-test-try-again-button]').doesNotExist();
150150

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

153154
await fillIn('[data-test-search-input]', 'ru');
154155
await triggerEvent('[data-test-search-form]', 'submit');
@@ -157,10 +158,8 @@ module('Acceptance | search', function (hooks) {
157158
assert.dom('[data-test-try-again-button]').isEnabled();
158159

159160
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-
});
161+
this.worker.resetHandlers();
162+
this.worker.use(http.get('/api/v1/crates', () => deferred.promise));
164163

165164
click('[data-test-try-again-button]');
166165
await waitFor('[data-test-page-header] [data-test-spinner]');
@@ -174,64 +173,73 @@ module('Acceptance | search', function (hooks) {
174173
});
175174

176175
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-
});
176+
this.worker.use(
177+
http.get('/api/v1/crates', function ({ request }) {
178+
assert.step('/api/v1/crates');
179+
180+
let url = new URL(request.url);
181+
assert.deepEqual(Object.fromEntries(url.searchParams.entries()), {
182+
all_keywords: 'fire ball',
183+
page: '3',
184+
per_page: '15',
185+
q: 'rust',
186+
sort: 'new',
187+
});
188+
189+
return HttpResponse.json({ crates: [], meta: { total: 0 } });
190+
}),
191+
);
190192

191193
await visit('/search?q=rust&page=3&per_page=15&sort=new&all_keywords=fire ball');
192194
assert.verifySteps(['/api/v1/crates']);
193195
});
194196

195197
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-
});
198+
this.worker.use(
199+
http.get('/api/v1/crates', function ({ request }) {
200+
assert.step('/api/v1/crates');
201+
202+
let url = new URL(request.url);
203+
assert.deepEqual(Object.fromEntries(url.searchParams.entries()), {
204+
all_keywords: 'fire ball',
205+
page: '3',
206+
per_page: '15',
207+
q: 'rust',
208+
sort: 'new',
209+
});
210+
211+
return HttpResponse.json({ crates: [], meta: { total: 0 } });
212+
}),
213+
);
209214

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

214219
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-
});
220+
this.worker.use(
221+
http.get('/api/v1/crates', function ({ request }) {
222+
assert.step('/api/v1/crates');
223+
224+
let url = new URL(request.url);
225+
assert.deepEqual(Object.fromEntries(url.searchParams.entries()), {
226+
all_keywords: 'fire ball',
227+
page: '3',
228+
per_page: '15',
229+
q: 'rust keywords:foo',
230+
sort: 'new',
231+
});
232+
233+
return HttpResponse.json({ crates: [], meta: { total: 0 } });
234+
}),
235+
);
228236

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

233241
test('visiting without query parameters works', async function (assert) {
234-
this.server.loadFixtures();
242+
loadFixtures(this.db);
235243

236244
await visit('/search');
237245

0 commit comments

Comments
 (0)