Skip to content

Commit f5a1523

Browse files
authored
Merge pull request #850 from Turbo87/mirage-factories
Start using ember-cli-mirage factories
2 parents a20eb96 + 3b4a499 commit f5a1523

File tree

10 files changed

+165
-0
lines changed

10 files changed

+165
-0
lines changed

mirage/factories/category.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Factory, faker } from 'ember-cli-mirage';
2+
import Ember from 'ember';
3+
4+
export default Factory.extend({
5+
category(i) {
6+
return `Category ${i}`;
7+
},
8+
9+
id() {
10+
return Ember.String.dasherize(this.category);
11+
},
12+
13+
slug() {
14+
return Ember.String.dasherize(this.category);
15+
},
16+
17+
description: () => faker.lorem.sentence(),
18+
created_at: () => faker.date.past(),
19+
crates_cnt: () => faker.random.number({ max: 5000 }),
20+
});

mirage/factories/crate.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Factory, trait, faker } from 'ember-cli-mirage';
2+
3+
export default Factory.extend({
4+
id(i) {
5+
return `crate-${i}`;
6+
},
7+
8+
name() {
9+
return this.id;
10+
},
11+
12+
description: () => faker.lorem.sentence(),
13+
downloads: () => faker.random.number({ max: 10000 }),
14+
documentation: () => faker.internet.url(),
15+
homepage: () => faker.internet.url(),
16+
repository: () => faker.internet.url(),
17+
license: () => faker.hacker.abbreviation(),
18+
max_version: () => faker.system.semver(),
19+
20+
created_at: () => faker.date.past(),
21+
updated_at() {
22+
return faker.date.between(this.created_at, new Date());
23+
},
24+
25+
badges: () => [],
26+
categories: () => [],
27+
keywords: () => [],
28+
versions: () => [],
29+
_extra_downloads: () => [],
30+
_owner_teams: () => [],
31+
_owner_users: () => [],
32+
33+
links() {
34+
return {
35+
'owner_user': `/api/v1/crates/${this.id}/owner_user`,
36+
'owner_team': `/api/v1/crates/${this.id}/owner_team`,
37+
'reverse_dependencies': `/api/v1/crates/${this.id}/reverse_dependencies`,
38+
'version_downloads': `/api/v1/crates/${this.id}/downloads`,
39+
'versions': `/api/v1/crates/${this.id}/versions`,
40+
};
41+
},
42+
43+
withVersion: trait({
44+
afterCreate(crate, server) {
45+
server.create('version', { crate: crate.id });
46+
}
47+
}),
48+
});

mirage/factories/version.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Factory, faker } from 'ember-cli-mirage';
2+
3+
export default Factory.extend({
4+
id: i => i,
5+
6+
// crate: '...',
7+
8+
num: () => faker.system.semver(),
9+
10+
created_at: () => faker.date.past(),
11+
updated_at() {
12+
return faker.date.between(this.created_at, new Date());
13+
},
14+
15+
yanked: false,
16+
17+
dl_path() {
18+
return `/api/v1/crates/${this.crate}/${this.num}/download`;
19+
},
20+
21+
downloads: () => faker.random.number({ max: 10000 }),
22+
features: () => {},
23+
_authors: () => [],
24+
25+
links() {
26+
return {
27+
'authors': `/api/v1/crates/${this.crate}/${this.num}/authors`,
28+
'dependencies': `/api/v1/crates/${this.crate}/${this.num}/dependencies`,
29+
'version_downloads': `/api/v1/crates/${this.crate}/${this.num}/downloads`,
30+
};
31+
},
32+
33+
afterCreate(version, server) {
34+
let crate = server.schema.crates.find(version.crate);
35+
crate.update({ versions: crate.versions.concat(parseInt(version.id, 10)) });
36+
}
37+
});

tests/acceptance/categories-test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import hasText from 'cargo/tests/helpers/has-text';
55
moduleForAcceptance('Acceptance | categories');
66

77
test('listing categories', async function(assert) {
8+
server.create('category', { category: 'API bindings', crates_cnt: 0 });
9+
server.create('category', { category: 'Algorithms', crates_cnt: 1 });
10+
server.create('category', { category: 'Asynchronous', crates_cnt: 3910 });
11+
812
await visit('/categories');
913

1014
hasText(assert, '.row:eq(0) .desc .info span', '0 crates');

tests/acceptance/crate-test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import hasText from 'cargo/tests/helpers/has-text';
66
moduleForAcceptance('Acceptance | crate page');
77

88
test('visiting a crate page from the front page', async function(assert) {
9+
server.create('crate', 'withVersion', { id: 'nanomsg' });
10+
911
await visit('/');
1012
await click('#just-updated ul > li:first a');
1113

@@ -14,6 +16,10 @@ test('visiting a crate page from the front page', async function(assert) {
1416
});
1517

1618
test('visiting /crates/nanomsg', async function(assert) {
19+
server.create('crate', { id: 'nanomsg', max_version: '0.6.1' });
20+
server.create('version', { crate: 'nanomsg', num: '0.6.0' });
21+
server.create('version', { crate: 'nanomsg', num: '0.6.1' });
22+
1723
await visit('/crates/nanomsg');
1824

1925
assert.equal(currentURL(), '/crates/nanomsg');
@@ -25,6 +31,10 @@ test('visiting /crates/nanomsg', async function(assert) {
2531
});
2632

2733
test('visiting /crates/nanomsg/', async function(assert) {
34+
server.create('crate', { id: 'nanomsg', max_version: '0.6.1' });
35+
server.create('version', { crate: 'nanomsg', num: '0.6.0' });
36+
server.create('version', { crate: 'nanomsg', num: '0.6.1' });
37+
2838
await visit('/crates/nanomsg/');
2939

3040
assert.equal(currentURL(), '/crates/nanomsg/');
@@ -36,6 +46,10 @@ test('visiting /crates/nanomsg/', async function(assert) {
3646
});
3747

3848
test('visiting /crates/nanomsg/0.6.0', async function(assert) {
49+
server.create('crate', { id: 'nanomsg', max_version: '0.6.1' });
50+
server.create('version', { crate: 'nanomsg', num: '0.6.0' });
51+
server.create('version', { crate: 'nanomsg', num: '0.6.1' });
52+
3953
await visit('/crates/nanomsg/0.6.0');
4054

4155
assert.equal(currentURL(), '/crates/nanomsg/0.6.0');
@@ -47,13 +61,17 @@ test('visiting /crates/nanomsg/0.6.0', async function(assert) {
4761
});
4862

4963
test('navigating to the all versions page', async function(assert) {
64+
server.loadFixtures();
65+
5066
await visit('/crates/nanomsg');
5167
await click('#crate-versions span.small a');
5268

5369
matchesText(assert, '.info', /All 13 versions of nanomsg since December \d+, 2014/);
5470
});
5571

5672
test('navigating to the reverse dependencies page', async function(assert) {
73+
server.loadFixtures();
74+
5775
await visit('/crates/nanomsg');
5876
await click('a:contains("Dependent crates")');
5977

@@ -65,6 +83,8 @@ test('navigating to the reverse dependencies page', async function(assert) {
6583
});
6684

6785
test('navigating to a user page', async function(assert) {
86+
server.loadFixtures();
87+
6888
await visit('/crates/nanomsg');
6989
await click('.owners li:last a');
7090

@@ -73,6 +93,8 @@ test('navigating to a user page', async function(assert) {
7393
});
7494

7595
test('navigating to a team page', async function(assert) {
96+
server.loadFixtures();
97+
7698
await visit('/crates/nanomsg');
7799
await click('.owners li:first a ');
78100

@@ -81,13 +103,17 @@ test('navigating to a team page', async function(assert) {
81103
});
82104

83105
test('crates having user-owners', async function(assert) {
106+
server.loadFixtures();
107+
84108
await visit('/crates/nanomsg');
85109

86110
findWithAssert('ul.owners li:first a[href="/teams/github:org:thehydroimpulse"] img[src="https://avatars.githubusercontent.com/u/565790?v=3&s=64"]');
87111
assert.equal(find('ul.owners li').length, 4);
88112
});
89113

90114
test('crates having team-owners', async function(assert) {
115+
server.loadFixtures();
116+
91117
await visit('/crates/nanomsg');
92118

93119
findWithAssert('ul.owners li:first a[href="/teams/github:org:thehydroimpulse"]');

tests/acceptance/crates-test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import hasText from 'cargo/tests/helpers/has-text';
55
moduleForAcceptance('Acceptance | crates page');
66

77
test('visiting the crates page from the front page', async function(assert) {
8+
server.loadFixtures();
9+
810
await visit('/');
911
await click('a[href="/crates"]');
1012

@@ -13,6 +15,8 @@ test('visiting the crates page from the front page', async function(assert) {
1315
});
1416

1517
test('visiting the crates page directly', async function(assert) {
18+
server.loadFixtures();
19+
1620
await visit('/crates');
1721
await click('a[href="/crates"]');
1822

@@ -21,13 +25,17 @@ test('visiting the crates page directly', async function(assert) {
2125
});
2226

2327
test('listing crates', async function(assert) {
28+
server.loadFixtures();
29+
2430
await visit('/crates');
2531

2632
hasText(assert, '.amt.small .cur', '1-10');
2733
hasText(assert, '.amt.small .total', '19');
2834
});
2935

3036
test('navigating to next page of crates', async function(assert) {
37+
server.loadFixtures();
38+
3139
await visit('/crates');
3240
await click('.pagination .next');
3341

tests/acceptance/front-page-test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import hasText from 'cargo/tests/helpers/has-text';
55
moduleForAcceptance('Acceptance | front page');
66

77
test('visiting /', async function(assert) {
8+
server.loadFixtures();
9+
810
await visit('/');
911

1012
assert.equal(currentURL(), '/');

tests/acceptance/search-test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import hasText from 'cargo/tests/helpers/has-text';
55
moduleForAcceptance('Acceptance | search');
66

77
test('searching for "rust"', async function(assert) {
8+
server.loadFixtures();
9+
810
await visit('/');
911
await fillIn('input.search', 'rust');
1012

@@ -26,6 +28,8 @@ test('searching for "rust"', async function(assert) {
2628
});
2729

2830
test('pressing S key to focus the search bar', async function(assert) {
31+
server.loadFixtures();
32+
2933
const KEYCODE_S = 83;
3034
const KEYCODE_A = 65;
3135

tests/acceptance/team-page-test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,35 @@ import hasText from 'cargo/tests/helpers/has-text';
55
moduleForAcceptance('Acceptance | team page');
66

77
test('has team organization display', async function(assert) {
8+
server.loadFixtures();
9+
810
await visit('/teams/github:org:thehydroimpulse');
911

1012
hasText(assert, '.team-info h1', 'org');
1113
hasText(assert, '.team-info h2', 'thehydroimpulseteam');
1214
});
1315

1416
test('has link to github in team header', async function(assert) {
17+
server.loadFixtures();
18+
1519
await visit('/teams/github:org:thehydroimpulse');
1620

1721
const $githubLink = findWithAssert('.info a');
1822
assert.equal($githubLink.attr('href').trim(), 'https://github.com/org_test');
1923
});
2024

2125
test('github link has image in team header', async function(assert) {
26+
server.loadFixtures();
27+
2228
await visit('/teams/github:org:thehydroimpulse');
2329

2430
const $githubImg = findWithAssert('.info a img');
2531
assert.equal($githubImg.attr('src').trim(), '/assets/GitHub-Mark-32px.png');
2632
});
2733

2834
test('team organization details has github profile icon', async function(assert) {
35+
server.loadFixtures();
36+
2937
await visit('/teams/github:org:thehydroimpulse');
3038

3139
const $githubProfileImg = findWithAssert('.info img');

tests/acceptance/user-page-test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,34 @@ import hasText from 'cargo/tests/helpers/has-text';
55
moduleForAcceptance('Acceptance | user page');
66

77
test('has user display', async function(assert) {
8+
server.loadFixtures();
9+
810
await visit('/users/thehydroimpulse');
911

1012
hasText(assert, '#crates-heading h1', 'thehydroimpulse');
1113
});
1214

1315
test('has link to github in user header', async function(assert) {
16+
server.loadFixtures();
17+
1418
await visit('/users/thehydroimpulse');
1519

1620
const $githubLink = findWithAssert('#crates-heading a');
1721
assert.equal($githubLink.attr('href').trim(), 'https://github.com/thehydroimpulse');
1822
});
1923

2024
test('github link has image in user header', async function(assert) {
25+
server.loadFixtures();
26+
2127
await visit('/users/thehydroimpulse');
2228

2329
const $githubImg = findWithAssert('#crates-heading a img');
2430
assert.equal($githubImg.attr('src').trim(), '/assets/GitHub-Mark-32px.png');
2531
});
2632

2733
test('user details has github profile icon', async function(assert) {
34+
server.loadFixtures();
35+
2836
await visit('/users/thehydroimpulse');
2937

3038
const $githubProfileImg = findWithAssert('#crates-heading img');

0 commit comments

Comments
 (0)