Skip to content

Commit b40a141

Browse files
committed
Auto merge of #3847 - Turbo87:mirage, r=locks
Reorganize Mirage test cases Having files with 600+ lines that test 7 different endpoints is starting to become unmaintainable, so this PR splits them up and simplifies them. We will now have a single test file per API endpoint instead, which should make the setup a little more pleasant to work with. Probably best reviewed commit by commit
2 parents 0db72cb + ba15b2c commit b40a141

39 files changed

+2303
-2304
lines changed

tests/mirage/categories-test.js

Lines changed: 0 additions & 207 deletions
This file was deleted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { module, test } from 'qunit';
2+
3+
import fetch from 'fetch';
4+
5+
import { setupTest } from '../../helpers';
6+
import setupMirage from '../../helpers/setup-mirage';
7+
8+
module('Mirage | GET /api/v1/categories/:id', function (hooks) {
9+
setupTest(hooks);
10+
setupMirage(hooks);
11+
12+
test('returns 404 for unknown categories', async function (assert) {
13+
let response = await fetch('/api/v1/categories/foo');
14+
assert.equal(response.status, 404);
15+
assert.deepEqual(await response.json(), { errors: [{ detail: 'Not Found' }] });
16+
});
17+
18+
test('returns a category object for known categories', async function (assert) {
19+
this.server.create('category', {
20+
category: 'no-std',
21+
description: 'Crates that are able to function without the Rust standard library.',
22+
});
23+
24+
let response = await fetch('/api/v1/categories/no-std');
25+
assert.equal(response.status, 200);
26+
assert.deepEqual(await response.json(), {
27+
category: {
28+
id: 'no-std',
29+
category: 'no-std',
30+
crates_cnt: 0,
31+
created_at: '2010-06-16T21:30:45Z',
32+
description: 'Crates that are able to function without the Rust standard library.',
33+
slug: 'no-std',
34+
},
35+
});
36+
});
37+
38+
test('calculates `crates_cnt` correctly', async function (assert) {
39+
this.server.create('category', { category: 'cli' });
40+
this.server.createList('crate', 7, { categoryIds: ['cli'] });
41+
this.server.create('category', { category: 'not-cli' });
42+
this.server.createList('crate', 3, { categoryIds: ['not-cli'] });
43+
44+
let response = await fetch('/api/v1/categories/cli');
45+
assert.equal(response.status, 200);
46+
assert.deepEqual(await response.json(), {
47+
category: {
48+
category: 'cli',
49+
crates_cnt: 7,
50+
created_at: '2010-06-16T21:30:45Z',
51+
description: 'This is the description for the category called "cli"',
52+
id: 'cli',
53+
slug: 'cli',
54+
},
55+
});
56+
});
57+
});

tests/mirage/categories/list-test.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { module, test } from 'qunit';
2+
3+
import fetch from 'fetch';
4+
5+
import { setupTest } from '../../helpers';
6+
import setupMirage from '../../helpers/setup-mirage';
7+
8+
module('Mirage | GET /api/v1/categories', function (hooks) {
9+
setupTest(hooks);
10+
setupMirage(hooks);
11+
12+
test('empty case', async function (assert) {
13+
let response = await fetch('/api/v1/categories');
14+
assert.equal(response.status, 200);
15+
assert.deepEqual(await response.json(), {
16+
categories: [],
17+
meta: {
18+
total: 0,
19+
},
20+
});
21+
});
22+
23+
test('returns a paginated categories list', async function (assert) {
24+
this.server.create('category', {
25+
category: 'no-std',
26+
description: 'Crates that are able to function without the Rust standard library.',
27+
});
28+
this.server.createList('category', 2);
29+
30+
let response = await fetch('/api/v1/categories');
31+
assert.equal(response.status, 200);
32+
assert.deepEqual(await response.json(), {
33+
categories: [
34+
{
35+
id: 'category-1',
36+
category: 'Category 1',
37+
crates_cnt: 0,
38+
created_at: '2010-06-16T21:30:45Z',
39+
description: 'This is the description for the category called "Category 1"',
40+
slug: 'category-1',
41+
},
42+
{
43+
id: 'category-2',
44+
category: 'Category 2',
45+
crates_cnt: 0,
46+
created_at: '2010-06-16T21:30:45Z',
47+
description: 'This is the description for the category called "Category 2"',
48+
slug: 'category-2',
49+
},
50+
{
51+
id: 'no-std',
52+
category: 'no-std',
53+
crates_cnt: 0,
54+
created_at: '2010-06-16T21:30:45Z',
55+
description: 'Crates that are able to function without the Rust standard library.',
56+
slug: 'no-std',
57+
},
58+
],
59+
meta: {
60+
total: 3,
61+
},
62+
});
63+
});
64+
65+
test('never returns more than 10 results', async function (assert) {
66+
this.server.createList('category', 25);
67+
68+
let response = await fetch('/api/v1/categories');
69+
assert.equal(response.status, 200);
70+
71+
let responsePayload = await response.json();
72+
assert.equal(responsePayload.categories.length, 10);
73+
assert.equal(responsePayload.meta.total, 25);
74+
});
75+
76+
test('supports `page` and `per_page` parameters', async function (assert) {
77+
this.server.createList('category', 25, {
78+
category: i => `cat-${String(i + 1).padStart(2, '0')}`,
79+
});
80+
81+
let response = await fetch('/api/v1/categories?page=2&per_page=5');
82+
assert.equal(response.status, 200);
83+
84+
let responsePayload = await response.json();
85+
assert.equal(responsePayload.categories.length, 5);
86+
assert.deepEqual(
87+
responsePayload.categories.map(it => it.id),
88+
['cat-06', 'cat-07', 'cat-08', 'cat-09', 'cat-10'],
89+
);
90+
assert.equal(responsePayload.meta.total, 25);
91+
});
92+
});

0 commit comments

Comments
 (0)