Skip to content

Commit aeecbed

Browse files
committed
mirage: Collapse nested test modules
1 parent 7de044c commit aeecbed

29 files changed

+1794
-1852
lines changed

tests/mirage/categories/list-test.js

Lines changed: 161 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -5,202 +5,200 @@ import fetch from 'fetch';
55
import { setupTest } from '../../helpers';
66
import setupMirage from '../../helpers/setup-mirage';
77

8-
module('Mirage | Categories', function (hooks) {
8+
module('Mirage | GET /api/v1/categories', function (hooks) {
99
setupTest(hooks);
1010
setupMirage(hooks);
1111

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);
12+
test('empty case', async function (assert) {
13+
let response = await fetch('/api/v1/categories');
14+
assert.equal(response.status, 200);
1615

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);
16+
let responsePayload = await response.json();
17+
assert.deepEqual(responsePayload, {
18+
categories: [],
19+
meta: {
20+
total: 0,
21+
},
9622
});
9723
});
9824

99-
module('GET /api/v1/categories/:id', function () {
100-
test('returns 404 for unknown categories', async function (assert) {
101-
let response = await fetch('/api/v1/categories/foo');
102-
assert.equal(response.status, 404);
103-
104-
let responsePayload = await response.json();
105-
assert.deepEqual(responsePayload, { errors: [{ detail: 'Not Found' }] });
25+
test('returns a paginated categories list', async function (assert) {
26+
this.server.create('category', {
27+
category: 'no-std',
28+
description: 'Crates that are able to function without the Rust standard library.',
10629
});
30+
this.server.createList('category', 2);
10731

108-
test('returns a category object for known categories', async function (assert) {
109-
this.server.create('category', {
110-
category: 'no-std',
111-
description: 'Crates that are able to function without the Rust standard library.',
112-
});
32+
let response = await fetch('/api/v1/categories');
33+
assert.equal(response.status, 200);
11334

114-
let response = await fetch('/api/v1/categories/no-std');
115-
assert.equal(response.status, 200);
116-
117-
let responsePayload = await response.json();
118-
assert.deepEqual(responsePayload, {
119-
category: {
35+
let responsePayload = await response.json();
36+
assert.deepEqual(responsePayload, {
37+
categories: [
38+
{
39+
id: 'category-1',
40+
category: 'Category 1',
41+
crates_cnt: 0,
42+
created_at: '2010-06-16T21:30:45Z',
43+
description: 'This is the description for the category called "Category 1"',
44+
slug: 'category-1',
45+
},
46+
{
47+
id: 'category-2',
48+
category: 'Category 2',
49+
crates_cnt: 0,
50+
created_at: '2010-06-16T21:30:45Z',
51+
description: 'This is the description for the category called "Category 2"',
52+
slug: 'category-2',
53+
},
54+
{
12055
id: 'no-std',
12156
category: 'no-std',
12257
crates_cnt: 0,
12358
created_at: '2010-06-16T21:30:45Z',
12459
description: 'Crates that are able to function without the Rust standard library.',
12560
slug: 'no-std',
12661
},
127-
});
62+
],
63+
meta: {
64+
total: 3,
65+
},
12866
});
67+
});
12968

130-
test('calculates `crates_cnt` correctly', async function (assert) {
131-
this.server.create('category', { category: 'cli' });
132-
this.server.createList('crate', 7, { categoryIds: ['cli'] });
133-
this.server.create('category', { category: 'not-cli' });
134-
this.server.createList('crate', 3, { categoryIds: ['not-cli'] });
69+
test('never returns more than 10 results', async function (assert) {
70+
this.server.createList('category', 25);
13571

136-
let response = await fetch('/api/v1/categories/cli');
137-
assert.equal(response.status, 200);
72+
let response = await fetch('/api/v1/categories');
73+
assert.equal(response.status, 200);
13874

139-
let responsePayload = await response.json();
140-
assert.deepEqual(responsePayload, {
141-
category: {
142-
category: 'cli',
143-
crates_cnt: 7,
144-
created_at: '2010-06-16T21:30:45Z',
145-
description: 'This is the description for the category called "cli"',
146-
id: 'cli',
147-
slug: 'cli',
148-
},
149-
});
75+
let responsePayload = await response.json();
76+
assert.equal(responsePayload.categories.length, 10);
77+
assert.equal(responsePayload.meta.total, 25);
78+
});
79+
80+
test('supports `page` and `per_page` parameters', async function (assert) {
81+
this.server.createList('category', 25, {
82+
category: i => `cat-${String(i + 1).padStart(2, '0')}`,
15083
});
84+
85+
let response = await fetch('/api/v1/categories?page=2&per_page=5');
86+
assert.equal(response.status, 200);
87+
88+
let responsePayload = await response.json();
89+
assert.equal(responsePayload.categories.length, 5);
90+
assert.deepEqual(
91+
responsePayload.categories.map(it => it.id),
92+
['cat-06', 'cat-07', 'cat-08', 'cat-09', 'cat-10'],
93+
);
94+
assert.equal(responsePayload.meta.total, 25);
15195
});
96+
});
15297

153-
module('GET /api/v1/category_slugs', function () {
154-
test('empty case', async function (assert) {
155-
let response = await fetch('/api/v1/category_slugs');
156-
assert.equal(response.status, 200);
98+
module('GET /api/v1/categories/:id', function () {
99+
test('returns 404 for unknown categories', async function (assert) {
100+
let response = await fetch('/api/v1/categories/foo');
101+
assert.equal(response.status, 404);
157102

158-
let responsePayload = await response.json();
159-
assert.deepEqual(responsePayload, {
160-
category_slugs: [],
161-
});
103+
let responsePayload = await response.json();
104+
assert.deepEqual(responsePayload, { errors: [{ detail: 'Not Found' }] });
105+
});
106+
107+
test('returns a category object for known categories', async function (assert) {
108+
this.server.create('category', {
109+
category: 'no-std',
110+
description: 'Crates that are able to function without the Rust standard library.',
162111
});
163112

164-
test('returns a category slugs list', async function (assert) {
165-
this.server.create('category', {
113+
let response = await fetch('/api/v1/categories/no-std');
114+
assert.equal(response.status, 200);
115+
116+
let responsePayload = await response.json();
117+
assert.deepEqual(responsePayload, {
118+
category: {
119+
id: 'no-std',
166120
category: 'no-std',
121+
crates_cnt: 0,
122+
created_at: '2010-06-16T21:30:45Z',
167123
description: 'Crates that are able to function without the Rust standard library.',
168-
});
169-
this.server.createList('category', 2);
170-
171-
let response = await fetch('/api/v1/category_slugs');
172-
assert.equal(response.status, 200);
173-
174-
let responsePayload = await response.json();
175-
assert.deepEqual(responsePayload, {
176-
category_slugs: [
177-
{
178-
description: 'This is the description for the category called "Category 1"',
179-
id: 'category-1',
180-
slug: 'category-1',
181-
},
182-
{
183-
description: 'This is the description for the category called "Category 2"',
184-
id: 'category-2',
185-
slug: 'category-2',
186-
},
187-
{
188-
description: 'Crates that are able to function without the Rust standard library.',
189-
id: 'no-std',
190-
slug: 'no-std',
191-
},
192-
],
193-
});
124+
slug: 'no-std',
125+
},
194126
});
127+
});
195128

196-
test('has no pagination', async function (assert) {
197-
this.server.createList('category', 25);
129+
test('calculates `crates_cnt` correctly', async function (assert) {
130+
this.server.create('category', { category: 'cli' });
131+
this.server.createList('crate', 7, { categoryIds: ['cli'] });
132+
this.server.create('category', { category: 'not-cli' });
133+
this.server.createList('crate', 3, { categoryIds: ['not-cli'] });
134+
135+
let response = await fetch('/api/v1/categories/cli');
136+
assert.equal(response.status, 200);
137+
138+
let responsePayload = await response.json();
139+
assert.deepEqual(responsePayload, {
140+
category: {
141+
category: 'cli',
142+
crates_cnt: 7,
143+
created_at: '2010-06-16T21:30:45Z',
144+
description: 'This is the description for the category called "cli"',
145+
id: 'cli',
146+
slug: 'cli',
147+
},
148+
});
149+
});
150+
});
198151

199-
let response = await fetch('/api/v1/category_slugs');
200-
assert.equal(response.status, 200);
152+
module('GET /api/v1/category_slugs', function () {
153+
test('empty case', async function (assert) {
154+
let response = await fetch('/api/v1/category_slugs');
155+
assert.equal(response.status, 200);
201156

202-
let responsePayload = await response.json();
203-
assert.equal(responsePayload.category_slugs.length, 25);
157+
let responsePayload = await response.json();
158+
assert.deepEqual(responsePayload, {
159+
category_slugs: [],
204160
});
205161
});
162+
163+
test('returns a category slugs list', async function (assert) {
164+
this.server.create('category', {
165+
category: 'no-std',
166+
description: 'Crates that are able to function without the Rust standard library.',
167+
});
168+
this.server.createList('category', 2);
169+
170+
let response = await fetch('/api/v1/category_slugs');
171+
assert.equal(response.status, 200);
172+
173+
let responsePayload = await response.json();
174+
assert.deepEqual(responsePayload, {
175+
category_slugs: [
176+
{
177+
description: 'This is the description for the category called "Category 1"',
178+
id: 'category-1',
179+
slug: 'category-1',
180+
},
181+
{
182+
description: 'This is the description for the category called "Category 2"',
183+
id: 'category-2',
184+
slug: 'category-2',
185+
},
186+
{
187+
description: 'Crates that are able to function without the Rust standard library.',
188+
id: 'no-std',
189+
slug: 'no-std',
190+
},
191+
],
192+
});
193+
});
194+
195+
test('has no pagination', async function (assert) {
196+
this.server.createList('category', 25);
197+
198+
let response = await fetch('/api/v1/category_slugs');
199+
assert.equal(response.status, 200);
200+
201+
let responsePayload = await response.json();
202+
assert.equal(responsePayload.category_slugs.length, 25);
203+
});
206204
});

0 commit comments

Comments
 (0)