Skip to content
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

refactor: use async/await in tests #1221

Merged
merged 1 commit into from
Nov 25, 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
68 changes: 29 additions & 39 deletions tests/integration/http-verbs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,89 +19,79 @@ module('Integration | HTTP Verbs', function(hooks) {
this.server.shutdown();
});

test('mirage responds to get', function(assert) {
test('mirage responds to get', async function(assert) {
assert.expect(1);
let done = assert.async();

this.server.get('/contacts', function() {
return true;
});

promiseAjax({
let { data } = await promiseAjax({
method: 'GET',
url: '/contacts'
}).then((response) => {
assert.equal(response.data, true);
done();
});

assert.equal(data, true);
});

test('mirage responds to post', function(assert) {
test('mirage responds to post', async function(assert) {
assert.expect(1);
let done = assert.async();

this.server.post('/contacts', function() {
return true;
});

promiseAjax({
let { data } = await promiseAjax({
method: 'POST',
url: '/contacts'
}).then((response) => {
assert.equal(response.data, true);
done();
});

assert.equal(data, true);
});

test('mirage responds to put', function(assert) {
test('mirage responds to put', async function(assert) {
assert.expect(1);
let done = assert.async();

this.server.put('/contacts', function() {
return true;
});

promiseAjax({
let { data } = await promiseAjax({
method: 'PUT',
url: '/contacts'
}).then((response) => {
assert.equal(response.data, true);
done();
});

assert.equal(data, true);
});

test('mirage responds to delete', function(assert) {
test('mirage responds to delete', async function(assert) {
assert.expect(1);
let done = assert.async();

this.server.delete('/contacts', function() {
return true;
});

promiseAjax({
let { data } = await promiseAjax({
method: 'DELETE',
url: '/contacts'
}).then((response) => {
assert.equal(response.data, true);
done();
});

assert.equal(data, true);
});

test('mirage responds to patch', function(assert) {
test('mirage responds to patch', async function(assert) {
assert.expect(1);
let done = assert.async();

this.server.patch('/contacts', function() {
return true;
});

promiseAjax({
let { data } = await promiseAjax({
method: 'PATCH',
url: '/contacts'
}).then((response) => {
assert.equal(response.data, true);
done();
});

assert.equal(data, true);
});

test('mirage responds to resource', function(assert) {
Expand All @@ -118,18 +108,18 @@ module('Integration | HTTP Verbs', function(hooks) {
});
});

test('response code can be customized', function(assert) {
test('response code can be customized', async function(assert) {
assert.expect(1);
let done = assert.async();

this.server.get('/contacts', {}, 404);

promiseAjax({
method: 'GET',
url: '/contacts'
}).catch(function(error) {
assert.ok(error.xhr.status, 404);
done();
});
try {
await promiseAjax({
method: 'GET',
url: '/contacts'
});
} catch(e) {
assert.ok(e.xhr.status, 404);
}
});
});
57 changes: 24 additions & 33 deletions tests/integration/route-handlers/function-handler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,56 +33,52 @@ module('Integration | Route handlers | Function handler', function(hooks) {
this.server.shutdown();
});

test('a meaningful error is thrown if a custom route handler throws an error', function(assert) {
test('a meaningful error is thrown if a custom route handler throws an error', async function(assert) {
assert.expect(1);
let done = assert.async();

this.server.get('/users', function() {
throw 'I goofed';
});

promiseAjax({
method: 'GET',
url: '/users'
}).catch((error) => {
assert.equal(error.xhr.responseText, 'Mirage: Your GET handler for the url /users threw an error: I goofed');
done();
});
try {
await promiseAjax({
method: 'GET',
url: '/users'
});
} catch(e) {
assert.equal(
e.xhr.responseText,
'Mirage: Your GET handler for the url /users threw an error: I goofed'
);
}
});

test('mirage response string is not serialized to string', function(assert) {
test('mirage response string is not serialized to string', async function(assert) {
assert.expect(1);
let done = assert.async();

this.server.get('/users', function() {
return new Response(200, { 'Content-Type': 'text/csv' }, 'firstname,lastname\nbob,dylon');
});

promiseAjax({ method: 'GET', url: '/users' }).then((response) => {
assert.equal(response.data, 'firstname,lastname\nbob,dylon');
done();
});
let { data } = await promiseAjax({ method: 'GET', url: '/users' });
assert.equal(data, 'firstname,lastname\nbob,dylon');
});

test('function can return a promise with non-serializable content', function(assert) {
test('function can return a promise with non-serializable content', async function(assert) {
assert.expect(1);
let done = assert.async();

this.server.get('/users', function() {
return new Promise(resolve => {
resolve(new Response(200, { 'Content-Type': 'text/csv' }, 'firstname,lastname\nbob,dylan'));
});
});

promiseAjax({ method: 'GET', url: '/users' }).then((response) => {
assert.equal(response.data, 'firstname,lastname\nbob,dylan');
done();
});
let { data } = await promiseAjax({ method: 'GET', url: '/users' });
assert.equal(data, 'firstname,lastname\nbob,dylan');
});

test('function can return a promise with serializable content', function(assert) {
test('function can return a promise with serializable content', async function(assert) {
assert.expect(1);
let done = assert.async();

let user = this.schema.users.create({ name: 'Sam' });

Expand All @@ -92,26 +88,21 @@ module('Integration | Route handlers | Function handler', function(hooks) {
});
});

promiseAjax({ method: 'GET', url: '/users' }).then((response) => {
assert.deepEqual(response.data, { users: [ { id: user.id, name: 'Sam' } ] });
done();
});
let { data } = await promiseAjax({ method: 'GET', url: '/users' });
assert.deepEqual(data, { users: [ { id: user.id, name: 'Sam' } ] });
});

test('function can return a promise with an empty string', function(assert) {
test('function can return a promise with an empty string', async function(assert) {
assert.expect(1);
let done = assert.async();

this.server.get('/users', function() {
return new Promise(resolve => {
resolve(new Response(200, { 'Content-Type': 'text/csv' }, ''));
});
});

promiseAjax({ method: 'GET', url: '/users' }).then((response) => {
assert.equal(response.data, '');
done();
});
let { data } = await promiseAjax({ method: 'GET', url: '/users' });
assert.equal(data, '');
});

test('#serialize uses the default serializer on a model', function(assert) {
Expand Down
81 changes: 36 additions & 45 deletions tests/integration/serializers/base/full-request-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ module('Integration | Serializers | Base | Full Request', function(hooks) {
this.server.shutdown();
});

test('the appropriate serializer is used', function(assert) {
test('the appropriate serializer is used', async function(assert) {
assert.expect(1);
let done = assert.async();

let author = this.server.schema.authors.create({
first: 'Link',
last: 'of Hyrule',
Expand All @@ -64,47 +64,41 @@ module('Integration | Serializers | Base | Full Request', function(hooks) {
return schema.authors.find(id);
});

promiseAjax({
let { data } = await promiseAjax({
method: 'GET',
url: '/authors/1'
}).then((response) => {
assert.deepEqual(response.data, {
author: {
id: '1',
first: 'Link',
posts: [
{ id: '1', title: 'Lorem ipsum' }
]
}
});
done();
});

assert.deepEqual(data, {
author: {
id: '1',
first: 'Link',
posts: [
{ id: '1', title: 'Lorem ipsum' }
]
}
});
});

test('components decoded', function(assert) {
test('components decoded', async function(assert) {
assert.expect(1);
let done = assert.async();

this.server.get('/authors/:id', function(schema, request) {
let { id } = request.params;

return { data: { id } };
});

promiseAjax({
let { data } = await promiseAjax({
method: 'GET',
url: '/authors/%3A1'
}).then((response) => {
assert.deepEqual(response.data, {
data: { id: ':1' }
});
done();
});

assert.deepEqual(data, { data: { id: ':1' } });
});

test('a response falls back to the application serializer, if it exists', function(assert) {
test('a response falls back to the application serializer, if it exists', async function(assert) {
assert.expect(1);
let done = assert.async();
this.server.schema.posts.create({
title: 'Lorem',
date: '20001010'
Expand All @@ -116,22 +110,20 @@ module('Integration | Serializers | Base | Full Request', function(hooks) {
return schema.posts.find(id);
});

promiseAjax({
let { data } = await promiseAjax({
method: 'GET',
url: '/posts/1'
}).then((response) => {
assert.deepEqual(response.data, {
id: '1',
title: 'Lorem',
date: '20001010'
});
done();
});

assert.deepEqual(data, {
id: '1',
title: 'Lorem',
date: '20001010'
});
});

test('serializer.include is invoked when it is a function', function(assert) {
test('serializer.include is invoked when it is a function', async function(assert) {
assert.expect(1);
let done = assert.async();
let post = this.server.schema.posts.create({
title: 'Lorem',
date: '20001010'
Expand All @@ -145,20 +137,19 @@ module('Integration | Serializers | Base | Full Request', function(hooks) {
return schema.comments.find(id);
});

promiseAjax({
let { data } = await promiseAjax({
method: 'GET',
url: '/comments/1?include_post=true'
}).then((response) => {
assert.deepEqual(response.data, {
});

assert.deepEqual(data, {
id: '1',
description: 'Lorem is the best',
post: {
id: '1',
description: 'Lorem is the best',
post: {
id: '1',
title: 'Lorem',
date: '20001010'
}
});
done();
title: 'Lorem',
date: '20001010'
}
});
});
});
Loading