Skip to content

Start using ember-cli-mirage factories #850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions mirage/factories/category.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Factory, faker } from 'ember-cli-mirage';
import Ember from 'ember';

export default Factory.extend({
category(i) {
return `Category ${i}`;
},

id() {
return Ember.String.dasherize(this.category);
},

slug() {
return Ember.String.dasherize(this.category);
},

description: () => faker.lorem.sentence(),
created_at: () => faker.date.past(),
crates_cnt: () => faker.random.number({ max: 5000 }),
});
48 changes: 48 additions & 0 deletions mirage/factories/crate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Factory, trait, faker } from 'ember-cli-mirage';

export default Factory.extend({
id(i) {
return `crate-${i}`;
},

name() {
return this.id;
},

description: () => faker.lorem.sentence(),
downloads: () => faker.random.number({ max: 10000 }),
documentation: () => faker.internet.url(),
homepage: () => faker.internet.url(),
repository: () => faker.internet.url(),
license: () => faker.hacker.abbreviation(),
max_version: () => faker.system.semver(),

created_at: () => faker.date.past(),
updated_at() {
return faker.date.between(this.created_at, new Date());
},

badges: () => [],
categories: () => [],
keywords: () => [],
versions: () => [],
_extra_downloads: () => [],
_owner_teams: () => [],
_owner_users: () => [],

links() {
return {
'owner_user': `/api/v1/crates/${this.id}/owner_user`,
'owner_team': `/api/v1/crates/${this.id}/owner_team`,
'reverse_dependencies': `/api/v1/crates/${this.id}/reverse_dependencies`,
'version_downloads': `/api/v1/crates/${this.id}/downloads`,
'versions': `/api/v1/crates/${this.id}/versions`,
};
},

withVersion: trait({
afterCreate(crate, server) {
server.create('version', { crate: crate.id });
}
}),
});
37 changes: 37 additions & 0 deletions mirage/factories/version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Factory, faker } from 'ember-cli-mirage';

export default Factory.extend({
id: i => i,

// crate: '...',

num: () => faker.system.semver(),

created_at: () => faker.date.past(),
updated_at() {
return faker.date.between(this.created_at, new Date());
},

yanked: false,

dl_path() {
return `/api/v1/crates/${this.crate}/${this.num}/download`;
},

downloads: () => faker.random.number({ max: 10000 }),
features: () => {},
_authors: () => [],

links() {
return {
'authors': `/api/v1/crates/${this.crate}/${this.num}/authors`,
'dependencies': `/api/v1/crates/${this.crate}/${this.num}/dependencies`,
'version_downloads': `/api/v1/crates/${this.crate}/${this.num}/downloads`,
};
},

afterCreate(version, server) {
let crate = server.schema.crates.find(version.crate);
crate.update({ versions: crate.versions.concat(parseInt(version.id, 10)) });
}
});
4 changes: 4 additions & 0 deletions tests/acceptance/categories-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import hasText from 'cargo/tests/helpers/has-text';
moduleForAcceptance('Acceptance | categories');

test('listing categories', async function(assert) {
server.create('category', { category: 'API bindings', crates_cnt: 0 });
server.create('category', { category: 'Algorithms', crates_cnt: 1 });
server.create('category', { category: 'Asynchronous', crates_cnt: 3910 });

await visit('/categories');

hasText(assert, '.row:eq(0) .desc .info span', '0 crates');
Expand Down
26 changes: 26 additions & 0 deletions tests/acceptance/crate-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import hasText from 'cargo/tests/helpers/has-text';
moduleForAcceptance('Acceptance | crate page');

test('visiting a crate page from the front page', async function(assert) {
server.create('crate', 'withVersion', { id: 'nanomsg' });

await visit('/');
await click('#just-updated ul > li:first a');

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

test('visiting /crates/nanomsg', async function(assert) {
server.create('crate', { id: 'nanomsg', max_version: '0.6.1' });
server.create('version', { crate: 'nanomsg', num: '0.6.0' });
server.create('version', { crate: 'nanomsg', num: '0.6.1' });

await visit('/crates/nanomsg');

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

test('visiting /crates/nanomsg/', async function(assert) {
server.create('crate', { id: 'nanomsg', max_version: '0.6.1' });
server.create('version', { crate: 'nanomsg', num: '0.6.0' });
server.create('version', { crate: 'nanomsg', num: '0.6.1' });

await visit('/crates/nanomsg/');

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

test('visiting /crates/nanomsg/0.6.0', async function(assert) {
server.create('crate', { id: 'nanomsg', max_version: '0.6.1' });
server.create('version', { crate: 'nanomsg', num: '0.6.0' });
server.create('version', { crate: 'nanomsg', num: '0.6.1' });

await visit('/crates/nanomsg/0.6.0');

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

test('navigating to the all versions page', async function(assert) {
server.loadFixtures();

await visit('/crates/nanomsg');
await click('#crate-versions span.small a');

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

test('navigating to the reverse dependencies page', async function(assert) {
server.loadFixtures();

await visit('/crates/nanomsg');
await click('a:contains("Dependent crates")');

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

test('navigating to a user page', async function(assert) {
server.loadFixtures();

await visit('/crates/nanomsg');
await click('.owners li:last a');

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

test('navigating to a team page', async function(assert) {
server.loadFixtures();

await visit('/crates/nanomsg');
await click('.owners li:first a ');

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

test('crates having user-owners', async function(assert) {
server.loadFixtures();

await visit('/crates/nanomsg');

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

test('crates having team-owners', async function(assert) {
server.loadFixtures();

await visit('/crates/nanomsg');

findWithAssert('ul.owners li:first a[href="/teams/github:org:thehydroimpulse"]');
Expand Down
8 changes: 8 additions & 0 deletions tests/acceptance/crates-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import hasText from 'cargo/tests/helpers/has-text';
moduleForAcceptance('Acceptance | crates page');

test('visiting the crates page from the front page', async function(assert) {
server.loadFixtures();

await visit('/');
await click('a[href="/crates"]');

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

test('visiting the crates page directly', async function(assert) {
server.loadFixtures();

await visit('/crates');
await click('a[href="/crates"]');

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

test('listing crates', async function(assert) {
server.loadFixtures();

await visit('/crates');

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

test('navigating to next page of crates', async function(assert) {
server.loadFixtures();

await visit('/crates');
await click('.pagination .next');

Expand Down
2 changes: 2 additions & 0 deletions tests/acceptance/front-page-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import hasText from 'cargo/tests/helpers/has-text';
moduleForAcceptance('Acceptance | front page');

test('visiting /', async function(assert) {
server.loadFixtures();

await visit('/');

assert.equal(currentURL(), '/');
Expand Down
4 changes: 4 additions & 0 deletions tests/acceptance/search-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import hasText from 'cargo/tests/helpers/has-text';
moduleForAcceptance('Acceptance | search');

test('searching for "rust"', async function(assert) {
server.loadFixtures();

await visit('/');
await fillIn('input.search', 'rust');

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

test('pressing S key to focus the search bar', async function(assert) {
server.loadFixtures();

const KEYCODE_S = 83;
const KEYCODE_A = 65;

Expand Down
8 changes: 8 additions & 0 deletions tests/acceptance/team-page-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,35 @@ import hasText from 'cargo/tests/helpers/has-text';
moduleForAcceptance('Acceptance | team page');

test('has team organization display', async function(assert) {
server.loadFixtures();

await visit('/teams/github:org:thehydroimpulse');

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

test('has link to github in team header', async function(assert) {
server.loadFixtures();

await visit('/teams/github:org:thehydroimpulse');

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

test('github link has image in team header', async function(assert) {
server.loadFixtures();

await visit('/teams/github:org:thehydroimpulse');

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

test('team organization details has github profile icon', async function(assert) {
server.loadFixtures();

await visit('/teams/github:org:thehydroimpulse');

const $githubProfileImg = findWithAssert('.info img');
Expand Down
8 changes: 8 additions & 0 deletions tests/acceptance/user-page-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,34 @@ import hasText from 'cargo/tests/helpers/has-text';
moduleForAcceptance('Acceptance | user page');

test('has user display', async function(assert) {
server.loadFixtures();

await visit('/users/thehydroimpulse');

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

test('has link to github in user header', async function(assert) {
server.loadFixtures();

await visit('/users/thehydroimpulse');

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

test('github link has image in user header', async function(assert) {
server.loadFixtures();

await visit('/users/thehydroimpulse');

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

test('user details has github profile icon', async function(assert) {
server.loadFixtures();

await visit('/users/thehydroimpulse');

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