Skip to content

Commit

Permalink
Rewrite to use creates
Browse files Browse the repository at this point in the history
  • Loading branch information
joshsmith committed Mar 29, 2017
1 parent 6776d73 commit a588c56
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 34 deletions.
42 changes: 13 additions & 29 deletions mirage/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,12 @@ export default function() {
this.patch('/comments/:id', function(schema) {
let attrs = this.normalizedRequestAttrs();
let comment = schema.comments.find(attrs.id);

// the API takes takes markdown and renders body
attrs.body = `<p>${attrs.markdown}</p>`;

// for some reason, comment.update(key, value) updates comment properties, but
// doesn't touch the comment.attrs object, which is what is used in response
// serialization
comment.attrs = attrs;

comment.commentUserMentions.models.forEach((mention) => mention.destroy());
comment.save();

return comment;
return comment.update(attrs);
});

/**
Expand Down Expand Up @@ -312,12 +306,9 @@ export default function() {
this.patch('/projects/:id', function(schema) {
// the API takes takes markdown and renders body
let attrs = this.normalizedRequestAttrs();
attrs.longDescriptionBody = `<p>${attrs.longDescriptionMarkdown}</p>`;

let project = schema.projects.find(attrs.id);
project.attrs = attrs;
project.save();
return project;
attrs.longDescriptionBody = `<p>${attrs.longDescriptionMarkdown}</p>`;
return project.update(attrs);
});

/**
Expand Down Expand Up @@ -406,6 +397,7 @@ export default function() {

this.patch('/stripe-connect-accounts/:id', function(schema) {
let attrs = this.normalizedRequestAttrs();
let stripeConnectAccount = schema.stripeConnectAccounts.find(attrs.id);

if (!isEmpty(attrs.legalEntityAddressCity)) {
attrs.recipientStatus = 'verifying';
Expand All @@ -427,10 +419,7 @@ export default function() {
attrs.bankAccountStatus = 'verified';
}

let stripeConnectAccount = schema.stripeConnectAccounts.find(attrs.id);
stripeConnectAccount.attrs = attrs;
stripeConnectAccount.save();
return stripeConnectAccount;
return stripeConnectAccount.update(attrs);
});

/**
Expand Down Expand Up @@ -495,13 +484,14 @@ export default function() {
// POST /tasks
this.post('/tasks', function(schema) {
let attrs = this.normalizedRequestAttrs();
let project = schema.projects.find(attrs.projectId);

// the API takes takes markdown and renders body
attrs.body = `<p>${attrs.markdown}</p>`;

// the API sets task number as an auto-incrementing value, scoped to project,
// so we need to simulate that here
attrs.number = schema.projects.find(attrs.projectId).tasks.models.length + 1;
// the API sets task number as an auto-incrementing value, scoped
// to project, so we need to simulate that here
attrs.number = project.tasks.models.length + 1;

return schema.create('task', attrs);
});
Expand All @@ -512,18 +502,14 @@ export default function() {
// PATCH /tasks/:id
this.patch('/tasks/:id', function(schema) {
let attrs = this.normalizedRequestAttrs();
let task = schema.tasks.find(attrs.id);

// the API takes takes markdown and renders body
attrs.body = `<p>${attrs.markdown}</p>`;

let task = schema.tasks.find(attrs.id);
task.attrs = attrs;

task.taskUserMentions.models.forEach((mention) => mention.destroy());
task.order = (task.position || 0) * 100;
task.save();

return task;
return task.update(attrs);
});

/**
Expand Down Expand Up @@ -608,9 +594,7 @@ export default function() {
}
}

user.attrs = attrs;
user.save();
return user;
return user.update(attrs);
});

// GET /users/email_available
Expand Down
2 changes: 1 addition & 1 deletion mirage/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { Model, belongsTo, hasMany } from 'ember-cli-mirage';
export default Model.extend({
projectUsers: hasMany(),
sluggedRoute: belongsTo(),
stripeConnectSubscriptions: hasMany('stripe-connect-subscription'),
stripePlatformCard: belongsTo('stripe-platform-card'),
stripePlatformCustomer: belongsTo('stripe-platform-customer'),
stripeConnectSubscriptions: hasMany('stripe-connect-subscription'),
userCategories: hasMany(),
userRoles: hasMany(),
userSkills: hasMany()
Expand Down
10 changes: 6 additions & 4 deletions tests/acceptance/project-about-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ test('When authenticated as owner, and project has no long description, it allow
});

andThen(() => {
project.reload();
assert.equal(project.longDescriptionMarkdown, 'A new body');
assert.equal(project.longDescriptionBody, '<p>A new body</p>');
assert.equal(projectAboutPage.projectLongDescription.longDescription.paragraph.text, project.longDescriptionMarkdown, 'The body is rendered');
Expand All @@ -80,7 +79,8 @@ test('When authenticated as owner, and project has long description, it allows e
longDescriptionMarkdown: 'A body'
});

let { user } = server.create('project-user', { project, role: 'owner' });
let user = server.create('user');
server.create('project-user', { project, user, role: 'owner' });

authenticateSession(this.application, { user_id: user.id });

Expand All @@ -90,11 +90,13 @@ test('When authenticated as owner, and project has long description, it allows e
});

andThen(() => {
projectAboutPage.projectLongDescription.clickEdit().fillInTextarea('An edited body').clickSave();
projectAboutPage.projectLongDescription
.clickEdit()
.fillInTextarea('An edited body')
.clickSave();
});

andThen(() => {
project.reload();
assert.equal(project.longDescriptionMarkdown, 'An edited body');
assert.equal(project.longDescriptionBody, '<p>An edited body</p>');
assert.equal(projectAboutPage.projectLongDescription.longDescription.paragraph.text, project.longDescriptionMarkdown, 'The body is rendered');
Expand Down

0 comments on commit a588c56

Please sign in to comment.