Skip to content

Commit 4ce3c80

Browse files
committed
mirage: Split test files into one file per endpoint
Having files with 600+ lines that test 7 different endpoints is starting to become unmaintainable...
1 parent 0435e20 commit 4ce3c80

33 files changed

+2046
-1812
lines changed

tests/mirage/categories-test.js

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

tests/mirage/categories/list-test.js

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

0 commit comments

Comments
 (0)