Skip to content

Commit a64d1fb

Browse files
committed
mirage/categories: Calculate crates_cnt property
1 parent 5f802cd commit a64d1fb

File tree

6 files changed

+58
-14
lines changed

6 files changed

+58
-14
lines changed

mirage/factories/category.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,4 @@ export default Factory.extend({
1717
},
1818

1919
created_at: '2010-06-16T21:30:45Z',
20-
crates_cnt: 0,
2120
});

mirage/fixtures/categories.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export default [
22
{
33
category: 'API bindings',
4-
crates_cnt: 0,
54
created_at: '2017-01-20T14:51:49Z',
65
description:
76
'Idiomatic wrappers of specific APIs for convenient access from Rust. Includes HTTP API wrappers as well. Non-idiomatic or unsafe bindings can be found in External FFI bindings.',
@@ -10,15 +9,13 @@ export default [
109
},
1110
{
1211
category: 'Algorithms',
13-
crates_cnt: 1,
1412
created_at: '2017-01-20T14:51:49Z',
1513
description: 'Rust implementations of core algorithms such as hashing, sorting, searching, and more.',
1614
id: 'algorithms',
1715
slug: 'algorithms',
1816
},
1917
{
2018
category: 'Asynchronous',
21-
crates_cnt: 3910,
2219
created_at: '2017-01-20T14:51:49Z',
2320
description:
2421
'Crates to help you deal with events independently of the main program flow, using techniques like futures, promises, waiting, or eventing.',

mirage/serializers/category.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import BaseSerializer from './application';
2+
3+
export default BaseSerializer.extend({
4+
getHashForResource() {
5+
let [hash, addToIncludes] = BaseSerializer.prototype.getHashForResource.apply(this, arguments);
6+
7+
if (Array.isArray(hash)) {
8+
for (let resource of hash) {
9+
this._adjust(resource);
10+
}
11+
} else {
12+
this._adjust(hash);
13+
}
14+
15+
return [hash, addToIncludes];
16+
},
17+
18+
_adjust(hash) {
19+
let allCrates = this.schema.crates.all();
20+
let associatedCrates = allCrates.filter(it => it.categoryIds.includes(hash.id));
21+
22+
hash.crates_cnt = associatedCrates.length;
23+
},
24+
});

tests/acceptance/categories-test.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ module('Acceptance | categories', function(hooks) {
1313
test('is accessible', async function(assert) {
1414
assert.expect(0);
1515

16-
this.server.create('category', { category: 'API bindings', crates_cnt: 0 });
17-
this.server.create('category', { category: 'Algorithms', crates_cnt: 1 });
18-
this.server.create('category', { category: 'Asynchronous', crates_cnt: 3910 });
16+
this.server.create('category', { category: 'API bindings' });
17+
this.server.create('category', { category: 'Algorithms' });
18+
this.server.create('category', { category: 'Asynchronous' });
1919

2020
await visit('/categories');
2121
percySnapshot(assert);
@@ -26,7 +26,7 @@ module('Acceptance | categories', function(hooks) {
2626
test('category/:category_id is accessible', async function(assert) {
2727
assert.expect(0);
2828

29-
this.server.create('category', { category: 'Algorithms', crates_cnt: 1 });
29+
this.server.create('category', { category: 'Algorithms' });
3030

3131
await visit('/categories/algorithms');
3232
percySnapshot(assert);
@@ -35,19 +35,21 @@ module('Acceptance | categories', function(hooks) {
3535
});
3636

3737
test('listing categories', async function(assert) {
38-
this.server.create('category', { category: 'API bindings', crates_cnt: 0 });
39-
this.server.create('category', { category: 'Algorithms', crates_cnt: 1 });
40-
this.server.create('category', { category: 'Asynchronous', crates_cnt: 3910 });
38+
this.server.create('category', { category: 'API bindings' });
39+
this.server.create('category', { category: 'Algorithms' });
40+
this.server.createList('crate', 1, { categoryIds: ['algorithms'] });
41+
this.server.create('category', { category: 'Asynchronous' });
42+
this.server.createList('crate', 15, { categoryIds: ['asynchronous'] });
4143

4244
await visit('/categories');
4345

4446
assert.dom('[data-test-category="api-bindings"] [data-test-crate-count]').hasText('0 crates');
4547
assert.dom('[data-test-category="algorithms"] [data-test-crate-count]').hasText('1 crate');
46-
assert.dom('[data-test-category="asynchronous"] [data-test-crate-count]').hasText('3,910 crates');
48+
assert.dom('[data-test-category="asynchronous"] [data-test-crate-count]').hasText('15 crates');
4749
});
4850

4951
test('category/:category_id index default sort is recent-downloads', async function(assert) {
50-
this.server.create('category', { category: 'Algorithms', crates_cnt: 1 });
52+
this.server.create('category', { category: 'Algorithms' });
5153

5254
await visit('/categories/algorithms');
5355

tests/mirage/categories-test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,28 @@ module('Mirage | Categories', function(hooks) {
125125
},
126126
});
127127
});
128+
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+
});
128150
});
129151

130152
module('GET /api/v1/category_slugs', function() {

tests/mirage/crates-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ module('Mirage | Keywords', function(hooks) {
294294
{
295295
id: 'no-std',
296296
category: 'no-std',
297-
crates_cnt: 0,
297+
crates_cnt: 1,
298298
created_at: '2010-06-16T21:30:45Z',
299299
description: 'This is the description for the category called "no-std"',
300300
slug: 'no-std',

0 commit comments

Comments
 (0)