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

Commit

Permalink
Fixed normalization of attrs in Mirage config and tests (#872)
Browse files Browse the repository at this point in the history
no issue

We weren't being consistent in our use of Mirage's `normalizedRequestAttrs()` method which meant that in certain cases Mirage's internal database had duplicated attrs, the original set being `camelCase` and the new/updated set being `underscore_case` which was not only confusing but can lead to errors or unexpected behaviour in tests.

- updated Mirage config to always normalize where necessary
- updated tests to always use `camelCase` attrs
- added `HEAD` route handler for gravatar to avoid unknown route noise in tests
  • Loading branch information
kevinansfield authored and aileen committed Sep 28, 2017
1 parent baa8565 commit fb3dbbd
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 41 deletions.
4 changes: 4 additions & 0 deletions mirage/config.js
Expand Up @@ -72,6 +72,10 @@ export function testConfig() {
};
});

this.head('http://www.gravatar.com/avatar/:md5', function () {
return '';
}, 200);

this.get('http://www.gravatar.com/avatar/:md5', function () {
return '';
}, 200);
Expand Down
4 changes: 2 additions & 2 deletions mirage/config/authentication.js
Expand Up @@ -53,8 +53,8 @@ export default function mockAuthentication(server) {

/* Setup ---------------------------------------------------------------- */

server.post('/authentication/setup', function ({roles, users}, request) {
let [attrs] = JSON.parse(request.requestBody).setup;
server.post('/authentication/setup', function ({roles, users}) {
let attrs = this.normalizedRequestAttrs();
let role = roles.findBy({name: 'Owner'});

// create owner role unless already exists
Expand Down
8 changes: 4 additions & 4 deletions mirage/config/invites.js
Expand Up @@ -28,10 +28,10 @@ export default function mockInvites(server) {
/* eslint-disable camelcase */
attrs.token = `${invites.all().models.length}-token`;
attrs.expires = moment.utc().add(1, 'day').valueOf();
attrs.created_at = moment.utc().format();
attrs.created_by = 1;
attrs.updated_at = moment.utc().format();
attrs.updated_by = 1;
attrs.createdAt = moment.utc().format();
attrs.createdBy = 1;
attrs.updatedAt = moment.utc().format();
attrs.updatedBy = 1;
attrs.status = 'sent';
/* eslint-enable camelcase */

Expand Down
4 changes: 2 additions & 2 deletions mirage/config/posts.js
Expand Up @@ -56,8 +56,8 @@ export default function mockPosts(server) {
});

// Handle embedded author in post
server.put('/posts/:id/', ({posts}, request) => {
let {posts: [post]} = JSON.parse(request.requestBody);
server.put('/posts/:id/', function ({posts}, request) {
let post = this.normalizedRequestAttrs();
let {author} = post;
delete post.author;

Expand Down
8 changes: 4 additions & 4 deletions mirage/config/subscribers.js
Expand Up @@ -5,8 +5,8 @@ import {paginatedResponse} from '../utils';
export default function mockSubscribers(server) {
server.get('/subscribers/', paginatedResponse('subscribers'));

server.post('/subscribers/', function ({subscribers}, request) {
let [attrs] = JSON.parse(request.requestBody).subscribers;
server.post('/subscribers/', function ({subscribers}) {
let attrs = this.normalizedRequestAttrs();
let subscriber = subscribers.findBy({email: attrs.email});

if (subscriber) {
Expand All @@ -18,8 +18,8 @@ export default function mockSubscribers(server) {
}]
});
} else {
attrs.created_at = new Date();
attrs.created_by = 0;
attrs.createdAt = new Date();
attrs.createdBy = 0;

return subscribers.create(attrs);
}
Expand Down
4 changes: 2 additions & 2 deletions mirage/config/tags.js
Expand Up @@ -3,8 +3,8 @@ import {isBlank} from '@ember/utils';
import {paginatedResponse} from '../utils';

export default function mockTags(server) {
server.post('/tags/', function ({tags}, {requestBody}) {
let [attrs] = JSON.parse(requestBody).tags;
server.post('/tags/', function ({tags}) {
let attrs = this.normalizedRequestAttrs();

if (isBlank(attrs.slug) && !isBlank(attrs.name)) {
attrs.slug = dasherize(attrs.name);
Expand Down
28 changes: 14 additions & 14 deletions tests/acceptance/editor-test.js
Expand Up @@ -567,7 +567,7 @@ describe('Acceptance: Editor', function() {
).to.match(/cannot be longer than 300/);

expect(
server.db.posts[0].custom_excerpt,
server.db.posts.find(post.id).customExcerpt,
'saved excerpt after validation error'
).to.be.blank;

Expand All @@ -576,7 +576,7 @@ describe('Acceptance: Editor', function() {
await triggerEvent('[data-test-field="custom-excerpt"]', 'blur');

expect(
server.db.posts[0].custom_excerpt,
server.db.posts.find(post.id).customExcerpt,
'saved excerpt'
).to.equal('Testing excerpt');

Expand All @@ -596,7 +596,7 @@ describe('Acceptance: Editor', function() {
).to.match(/cannot be longer than 65535/);

expect(
server.db.posts[0].codeinjection_head,
server.db.posts.find(post.id).codeinjectionHead,
'saved header injection after validation error'
).to.be.blank;

Expand All @@ -605,7 +605,7 @@ describe('Acceptance: Editor', function() {
await triggerEvent(headerCM.getInputField(), 'blur');

expect(
server.db.posts[0].codeinjection_head,
server.db.posts.find(post.id).codeinjectionHead,
'saved header injection'
).to.equal('<script src="http://example.com/inject-head.js"></script>');

Expand All @@ -620,7 +620,7 @@ describe('Acceptance: Editor', function() {
).to.match(/cannot be longer than 65535/);

expect(
server.db.posts[0].codeinjection_foot,
server.db.posts.find(post.id).codeinjectionFoot,
'saved footer injection after validation error'
).to.be.blank;

Expand All @@ -629,7 +629,7 @@ describe('Acceptance: Editor', function() {
await triggerEvent(footerCM.getInputField(), 'blur');

expect(
server.db.posts[0].codeinjection_foot,
server.db.posts.find(post.id).codeinjectionFoot,
'saved footer injection'
).to.equal('<script src="http://example.com/inject-foot.js"></script>');

Expand All @@ -656,7 +656,7 @@ describe('Acceptance: Editor', function() {
).to.match(/cannot be longer than 300/);

expect(
server.db.posts[0].twitter_title,
server.db.posts.find(post.id).twitterTitle,
'saved twitter title after validation error'
).to.be.blank;

Expand All @@ -666,7 +666,7 @@ describe('Acceptance: Editor', function() {
await triggerEvent('[data-test-field="twitter-title"]', 'blur');

expect(
server.db.posts[0].twitter_title,
server.db.posts.find(post.id).twitterTitle,
'saved twitter title'
).to.equal('Test Twitter Title');

Expand All @@ -680,7 +680,7 @@ describe('Acceptance: Editor', function() {
).to.match(/cannot be longer than 500/);

expect(
server.db.posts[0].twitter_description,
server.db.posts.find(post.id).twitterDescription,
'saved twitter description after validation error'
).to.be.blank;

Expand All @@ -690,7 +690,7 @@ describe('Acceptance: Editor', function() {
await triggerEvent('[data-test-field="twitter-description"]', 'blur');

expect(
server.db.posts[0].twitter_description,
server.db.posts.find(post.id).twitterDescription,
'saved twitter description'
).to.equal('Test Twitter Description');

Expand All @@ -717,7 +717,7 @@ describe('Acceptance: Editor', function() {
).to.match(/cannot be longer than 300/);

expect(
server.db.posts[0].og_title,
server.db.posts.find(post.id).ogTitle,
'saved facebook title after validation error'
).to.be.blank;

Expand All @@ -727,7 +727,7 @@ describe('Acceptance: Editor', function() {
await triggerEvent('[data-test-field="og-title"]', 'blur');

expect(
server.db.posts[0].og_title,
server.db.posts.find(post.id).ogTitle,
'saved facebook title'
).to.equal('Test Facebook Title');

Expand All @@ -741,7 +741,7 @@ describe('Acceptance: Editor', function() {
).to.match(/cannot be longer than 500/);

expect(
server.db.posts[0].og_description,
server.db.posts.find(post.id).ogDescription,
'saved facebook description after validation error'
).to.be.blank;

Expand All @@ -751,7 +751,7 @@ describe('Acceptance: Editor', function() {
await triggerEvent('[data-test-field="og-description"]', 'blur');

expect(
server.db.posts[0].og_description,
server.db.posts.find(post.id).ogDescription,
'saved facebook description'
).to.equal('Test Facebook Description');

Expand Down
24 changes: 11 additions & 13 deletions tests/acceptance/setup-test.js
Expand Up @@ -238,8 +238,8 @@ describe('Acceptance: Setup', function () {
invalidateSession(application);
server.loadFixtures('roles');

server.post('/invites', function ({invites}, request) {
let [params] = JSON.parse(request.requestBody).invites;
server.post('/invites/', function ({invites}) {
let attrs = this.normalizedRequestAttrs();

postCount++;

Expand All @@ -256,17 +256,15 @@ describe('Acceptance: Setup', function () {
}

// TODO: duplicated from mirage/config/invites - extract method?
/* eslint-disable camelcase */
params.token = `${invites.all().models.length}-token`;
params.expires = moment.utc().add(1, 'day').valueOf();
params.created_at = moment.utc().format();
params.created_by = 1;
params.updated_at = moment.utc().format();
params.updated_by = 1;
params.status = 'sent';
/* eslint-enable camelcase */

return invites.create(params);
attrs.token = `${invites.all().models.length}-token`;
attrs.expires = moment.utc().add(1, 'day').valueOf();
attrs.createdAt = moment.utc().format();
attrs.createdBy = 1;
attrs.updatedAt = moment.utc().format();
attrs.updatedBy = 1;
attrs.status = 'sent';

return invites.create(attrs);
});

// complete step 2 so we can access step 3
Expand Down

0 comments on commit fb3dbbd

Please sign in to comment.