Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Bump ember-cli-mirage to 0.4.2 (#969)
Browse files Browse the repository at this point in the history
closes TryGhost/Ghost#9433
- remove unnecessary bower deps
- update mirage usage
- fix tests
  • Loading branch information
acburdine authored and kevinansfield committed Mar 19, 2018
1 parent e1e2d6e commit c6ddf3e
Show file tree
Hide file tree
Showing 19 changed files with 170 additions and 102 deletions.
2 changes: 0 additions & 2 deletions bower.json
Expand Up @@ -2,11 +2,9 @@
"name": "ghost-admin",
"dependencies": {
"devicejs": "0.2.7",
"Faker": "3.1.0",
"google-caja": "6005.0.0",
"keymaster": "1.6.3",
"normalize.css": "3.0.3",
"pretender": "1.1.0",
"rangyinputs": "1.2.0",
"validator-js": "3.39.0"
}
Expand Down
5 changes: 5 additions & 0 deletions config/environment.js
Expand Up @@ -59,6 +59,11 @@ module.exports = function (environment) {
ENV.APP.LOG_VIEW_LOOKUPS = false;

ENV.APP.rootElement = '#ember-testing';

// Withuot manually setting this, pretender won't track requests
ENV['ember-cli-mirage'] = {
trackRequests: true
};
}

return ENV;
Expand Down
8 changes: 1 addition & 7 deletions mirage/config/settings.js
@@ -1,16 +1,10 @@
export default function mockSettings(server) {
// These endpoints use the raw database & fixtures without going
// through the ORM at all (meaning no setting model). This is due
// to https://github.com/samselikoff/ember-cli-mirage/issues/943
// as far as can be determined.
// potential TODO: update once the above issue is fixed? We don't really
// gain anything from using the ORM for settings so it may not be a good idea
server.get('/settings/', function ({db}, {queryParams}) {
let {type} = queryParams;
let filters = type.split(',');
let settings = [];

if (!db.settings) {
if (!db.settings.length) {
server.loadFixtures('settings');
}

Expand Down
3 changes: 1 addition & 2 deletions mirage/factories/invite.js
Expand Up @@ -9,6 +9,5 @@ export default Factory.extend({
createdBy() { return 1; },
updatedAt() { return moment.utc().format(); },
updatedBy() { return 1; },
status() { return 'sent'; },
roleId() { return 1; }
status() { return 'sent'; }
});
1 change: 0 additions & 1 deletion mirage/factories/post.js
Expand Up @@ -13,7 +13,6 @@ export default Factory.extend({
status(i) { return faker.list.cycle('draft', 'published', 'scheduled')(i); },
metaDescription(i) { return `Meta description for post ${i}.`; },
metaTitle(i) { return `Meta Title for post ${i}`; },
authorId: 1,
updatedAt: '2015-10-19T16:25:07.756Z',
updatedBy: 1,
publishedAt: '2015-12-19T16:25:07.000Z',
Expand Down
1 change: 1 addition & 0 deletions mirage/factories/user.js
Expand Up @@ -19,5 +19,6 @@ export default Factory.extend({
updatedAt: '2015-11-02T16:12:05.000Z',
updatedBy: '1',
website: 'http://example.com',
posts() { return []; },
roles() { return []; }
});
9 changes: 9 additions & 0 deletions mirage/fixtures/roles.js
Expand Up @@ -35,5 +35,14 @@ export default [
created_by: 1,
updated_at: '2015-11-13T16:01:29.132Z',
updated_by: 1
},
{
id: 5,
name: 'Contributor',
description: 'Contributors',
created_at: '2015-11-13T16:01:29.132Z',
created_by: 1,
updated_at: '2015-11-13T16:01:29.132Z',
updated_by: 1
}
];
3 changes: 2 additions & 1 deletion mirage/models/invite.js
@@ -1,4 +1,5 @@
import {Model} from 'ember-cli-mirage';
import {Model, belongsTo} from 'ember-cli-mirage';

export default Model.extend({
role: belongsTo()
});
5 changes: 3 additions & 2 deletions mirage/models/post.js
@@ -1,5 +1,6 @@
import {Model, belongsTo} from 'ember-cli-mirage';
import {Model, belongsTo, hasMany} from 'ember-cli-mirage';

export default Model.extend({
author: belongsTo('user')
author: belongsTo('user'),
tags: hasMany()
});
3 changes: 2 additions & 1 deletion mirage/models/tag.js
@@ -1,4 +1,5 @@
import {Model} from 'ember-cli-mirage';
import {Model, hasMany} from 'ember-cli-mirage';

export default Model.extend({
posts: hasMany()
});
28 changes: 28 additions & 0 deletions mirage/serializers/post.js
@@ -0,0 +1,28 @@
import BaseSerializer from './application';
import {RestSerializer} from 'ember-cli-mirage';

export default BaseSerializer.extend({
embed: true,

include(request) {
if (request.queryParams.include && request.queryParams.include.indexOf('tags') >= 0) {
return ['tags'];
}

return [];
},

serialize(object, request) {
if (this.isCollection(object)) {
return BaseSerializer.prototype.serialize.apply(this, arguments);
}

let {post} = RestSerializer.prototype.serialize.call(this, object, request);

if (object.author) {
post.author = object.author.id;
}

return {posts: [post]};
}
});
17 changes: 10 additions & 7 deletions mirage/serializers/user.js
Expand Up @@ -2,6 +2,16 @@ import BaseSerializer from './application';
import {RestSerializer} from 'ember-cli-mirage';

export default BaseSerializer.extend({
embed: true,

include(request) {
if (request.queryParams.include && request.queryParams.include.indexOf('roles') >= 0) {
return ['roles'];
}

return [];
},

serialize(object, request) {
if (this.isCollection(object)) {
return BaseSerializer.prototype.serialize.apply(this, arguments);
Expand All @@ -15,13 +25,6 @@ export default BaseSerializer.extend({
user.count = {posts};
}

let roles = BaseSerializer.prototype.serialize.call(this, object.roles, request);
let [role] = roles.roles;

if (role) {
user.roles = [role];
}

return {users: [user]};
}
});
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -55,7 +55,7 @@
"ember-cli-htmlbars": "2.0.3",
"ember-cli-htmlbars-inline-precompile": "1.0.2",
"ember-cli-inject-live-reload": "1.7.0",
"ember-cli-mirage": "0.2.8",
"ember-cli-mirage": "0.4.2",
"ember-cli-mocha": "^0.15.0",
"ember-cli-moment-shim": "3.5.0",
"ember-cli-node-assets": "0.2.2",
Expand Down
10 changes: 5 additions & 5 deletions tests/acceptance/content-test.js
Expand Up @@ -35,11 +35,11 @@ describe('Acceptance: Content', function () {
let editorRole = server.create('role', {name: 'Editor'});
editor = server.create('user', {roles: [editorRole]});

publishedPost = server.create('post', {authorId: admin.id, status: 'published', title: 'Published Post'});
scheduledPost = server.create('post', {authorId: admin.id, status: 'scheduled', title: 'Scheduled Post'});
draftPost = server.create('post', {authorId: admin.id, status: 'draft', title: 'Draft Post'});
publishedPage = server.create('post', {authorId: admin.id, status: 'published', page: true, title: 'Published Page'});
authorPost = server.create('post', {authorId: editor.id, status: 'published', title: 'Editor Published Post'});
publishedPost = server.create('post', {author: admin, status: 'published', title: 'Published Post'});
scheduledPost = server.create('post', {author: admin, status: 'scheduled', title: 'Scheduled Post'});
draftPost = server.create('post', {author: admin, status: 'draft', title: 'Draft Post'});
publishedPage = server.create('post', {author: admin, status: 'published', page: true, title: 'Published Page'});
authorPost = server.create('post', {author: editor, status: 'published', title: 'Editor Published Post'});

return authenticateSession(application);
});
Expand Down
92 changes: 44 additions & 48 deletions tests/acceptance/editor-test.js
Expand Up @@ -19,8 +19,8 @@ describe('Acceptance: Editor', function () {
});

it('redirects to signin when not authenticated', async function () {
server.create('user'); // necesary for post-author association
server.create('post');
let author = server.create('user'); // necesary for post-author association
server.create('post', {author});

invalidateSession(application);
await visit('/editor/1');
Expand All @@ -30,8 +30,8 @@ describe('Acceptance: Editor', function () {

it('does not redirect to team page when authenticated as contributor', async function () {
let role = server.create('role', {name: 'Contributor'});
server.create('user', {roles: [role], slug: 'test-user'});
server.create('post');
let author = server.create('user', {roles: [role], slug: 'test-user'});
server.create('post', {author});

authenticateSession(application);
await visit('/editor/1');
Expand All @@ -41,8 +41,8 @@ describe('Acceptance: Editor', function () {

it('does not redirect to team page when authenticated as author', async function () {
let role = server.create('role', {name: 'Author'});
server.create('user', {roles: [role], slug: 'test-user'});
server.create('post');
let author = server.create('user', {roles: [role], slug: 'test-user'});
server.create('post', {author});

authenticateSession(application);
await visit('/editor/1');
Expand All @@ -52,8 +52,8 @@ describe('Acceptance: Editor', function () {

it('does not redirect to team page when authenticated as editor', async function () {
let role = server.create('role', {name: 'Editor'});
server.create('user', {roles: [role], slug: 'test-user'});
server.create('post');
let author = server.create('user', {roles: [role], slug: 'test-user'});
server.create('post', {author});

authenticateSession(application);
await visit('/editor/1');
Expand All @@ -72,56 +72,52 @@ describe('Acceptance: Editor', function () {
expect(currentURL()).to.equal('/editor/1');
});

describe('when logged in as contributor', function () {
beforeEach(function () {
let role = server.create('role', {name: 'Contributor'});
server.create('user', {roles: [role]});
server.loadFixtures('settings');

return authenticateSession(application);
});

it('renders a save button instead of a publish menu & hides tags input', async function () {
server.createList('post', 2);
it('when logged in as a contributor, renders a save button instead of a publish menu & hides tags input', async function () {
let role = server.create('role', {name: 'Contributor'});
let author = server.create('user', {roles: [role]});
server.createList('post', 2, {author});
server.loadFixtures('settings');
authenticateSession(application);

// post id 1 is a draft, checking for draft behaviour now
await visit('/editor/1');
// post id 1 is a draft, checking for draft behaviour now
await visit('/editor/1');

expect(currentURL(), 'currentURL').to.equal('/editor/1');
expect(currentURL(), 'currentURL').to.equal('/editor/1');

// Expect publish menu to not exist
expect(
find('[data-test-publishmenu-trigger]'),
'publish menu trigger'
).to.not.exist;
// Expect publish menu to not exist
expect(
find('[data-test-publishmenu-trigger]'),
'publish menu trigger'
).to.not.exist;

// Open post settings menu
await click('[data-test-psm-trigger]');
// Open post settings menu
await click('[data-test-psm-trigger]');

// Check to make sure that tags input doesn't exist
expect(
find('[data-test-token-input]'),
'tags input'
).to.not.exist;
// Check to make sure that tags input doesn't exist
expect(
find('[data-test-token-input]'),
'tags input'
).to.not.exist;

// post id 2 is published, we should be redirected to index
await visit('/editor/2');
// post id 2 is published, we should be redirected to index
await visit('/editor/2');

expect(currentURL(), 'currentURL').to.equal('/');
});
expect(currentURL(), 'currentURL').to.equal('/');
});

describe('when logged in', function () {
let author;

beforeEach(function () {
let role = server.create('role', {name: 'Administrator'});
server.create('user', {roles: [role]});
author = server.create('user', {roles: [role]});
server.loadFixtures('settings');

return authenticateSession(application);
});

it('renders the editor correctly, PSM Publish Date and Save Button', async function () {
let [post1] = server.createList('post', 2);
let [post1] = server.createList('post', 2, {author});
let futureTime = moment().tz('Etc/UTC').add(10, 'minutes');

// post id 1 is a draft, checking for draft behaviour now
Expand Down Expand Up @@ -437,7 +433,7 @@ describe('Acceptance: Editor', function () {
});
});

let post = server.create('post', 1);
let post = server.create('post', 1, {author});
let plusTenMin = moment().utc().add(10, 'minutes');

await visit(`/editor/${post.id}`);
Expand All @@ -461,7 +457,7 @@ describe('Acceptance: Editor', function () {
});

it('handles title validation errors correctly', async function () {
server.createList('post', 1);
server.create('post', {author});

// post id 1 is a draft, checking for draft behaviour now
await visit('/editor/1');
Expand Down Expand Up @@ -528,7 +524,7 @@ describe('Acceptance: Editor', function () {
let compareDate = moment().tz('Etc/UTC').add(4, 'minutes');
let compareDateString = compareDate.format('MM/DD/YYYY');
let compareTimeString = compareDate.format('HH:mm');
server.create('post', {publishedAt: moment.utc().add(4, 'minutes'), status: 'scheduled'});
server.create('post', {publishedAt: moment.utc().add(4, 'minutes'), status: 'scheduled', author});
server.create('setting', {activeTimezone: 'Europe/Dublin'});
clock.restore();

Expand All @@ -549,9 +545,9 @@ describe('Acceptance: Editor', function () {
});

it('shows author list and allows switching of author in PSM', async function () {
server.create('post', {authorId: 1});
let role = server.create('role', {name: 'Author'});
let author = server.create('user', {name: 'Waldo', roles: [role]});
let otherAuthor = server.create('user', {name: 'Waldo', roles: [role]});
server.create('post', {author});

await visit('/editor/1');

Expand All @@ -566,7 +562,7 @@ describe('Acceptance: Editor', function () {
await fillIn('select[name="post-setting-author"]', '2');

expect(find('select[name="post-setting-author"]').val()).to.equal('2');
expect(server.db.posts[0].authorId).to.equal(author.id);
expect(server.db.posts[0].authorId).to.equal(otherAuthor.id);
});

it('autosaves when title loses focus', async function () {
Expand Down Expand Up @@ -598,7 +594,7 @@ describe('Acceptance: Editor', function () {
});

it('saves post settings fields', async function () {
let post = server.create('post');
let post = server.create('post', {author});

await visit(`/editor/${post.id}`);

Expand Down Expand Up @@ -814,7 +810,7 @@ describe('Acceptance: Editor', function () {
});

it('has unsplash icon when server doesn\'t return unsplash settings key', async function () {
server.createList('post', 1);
server.createList('post', 1, {author});

await visit('/editor/1');

Expand Down
1 change: 0 additions & 1 deletion tests/acceptance/subscribers-test.js
Expand Up @@ -69,7 +69,6 @@ describe('Acceptance: Subscribers', function () {
it('can manage subscribers', async function () {
server.createList('subscriber', 40);

authenticateSession(application);
await visit('/');
await click('.gh-nav-main a:contains("Subscribers")');

Expand Down

0 comments on commit c6ddf3e

Please sign in to comment.