Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
Modify acceptance tests to return promises to force non-async execution;
various other updates.
  • Loading branch information
dustinfarris committed Apr 9, 2015
1 parent decc040 commit 66943b8
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 190 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
"ember-cli-htmlbars": "0.7.4",
"ember-cli-ic-ajax": "0.1.1",
"ember-cli-inject-live-reload": "^1.3.0",
"ember-cli-pretender": "^0.3.1",
"ember-cli-pretender": "0.3.1",
"ember-cli-qunit": "0.3.10",
"ember-cli-uglify": "1.0.1",
"ember-data": "1.0.0-beta.16.1",
"ember-export-application-global": "^1.0.2",
"ember-try": "0.0.4",
"ember-sinon": "0.0.3"
"ember-sinon": "0.0.3",
"ember-try": "0.0.4"
},
"keywords": [
"adapter",
Expand Down
83 changes: 36 additions & 47 deletions tests/acceptance/crud-failure-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
test,
} from 'qunit';
import Pretender from 'pretender';
import startApp from 'ember-django-adapter/tests/helpers/start-app';
import startApp from 'dummy/tests/helpers/start-app';

var application;
var store;
Expand Down Expand Up @@ -62,20 +62,14 @@ module('Acceptance: CRUD Failure', {
}
});

/*
* These integration tests need to use the QUnit.stop() / QUnit.start()
* pattern as described in the following stackoverflow question:
*
* https://stackoverflow.com/questions/26317855/ember-cli-how-to-do-asynchronous-model-unit-testing-with-restadapter
*/
test('Permission denied error', function(assert) {
assert.expect(4);

stop();
Ember.run(function() {
store.find('post', 1).then({}, function(response) {
return Ember.run(function() {

return store.find('post', 1).then({}, function(response) {

assert.ok(response);
start();
assert.equal(response.status, 401);
assert.equal(response.statusText, 'Unauthorized');
assert.equal(response.responseText, JSON.stringify({detail: 'Authentication credentials were not provided.'}));
Expand All @@ -86,11 +80,11 @@ test('Permission denied error', function(assert) {
test('Server error', function(assert) {
assert.expect(4);

stop();
Ember.run(function() {
store.find('post', 2).then({}, function(response) {
return Ember.run(function() {

return store.find('post', 2).then({}, function(response) {

assert.ok(response);
start();
assert.equal(response.status, 500);
assert.equal(response.statusText, 'Internal Server Error');
assert.equal(response.responseText, '<h1>Server Error (500)</h1>');
Expand All @@ -101,59 +95,54 @@ test('Server error', function(assert) {
test('Create field errors', function(assert) {
assert.expect(6);

var record,
data = {postTitle: '', body: ''};
return Ember.run(function() {

var post = store.createRecord('post', {
postTitle: '',
body: ''
});

return post.save().then({}, function(response) {

stop();
Ember.run(function() {
record = store.createRecord('post', data);
record.save().then({}, function(response) {
assert.ok(response);
assert.ok(response.errors);

var errors = response.errors;

// Test camelCase field.
assert.equal(errors.postTitle.length, 1);
assert.equal(errors.postTitle[0], 'This field is required.');
assert.equal(response.errors.postTitle.length, 1);
assert.equal(response.errors.postTitle[0], 'This field is required.');

// Test non-camelCase field.
assert.equal(errors.body.length, 1);
assert.equal(errors.body[0], 'This field is required.');

start();
assert.equal(response.errors.body.length, 1);
assert.equal(response.errors.body[0], 'This field is required.');
});
});
});

test('Update field errors', function(assert) {
assert.expect(9);

stop();
Ember.run(function() {
store.find('post', 3).then(function(response) {
assert.ok(response);
return Ember.run(function() {

return store.find('post', 3).then(function(post) {

assert.equal(response.get('isDirty'), false);
response.set('postTitle', 'Lorem ipsum dolor sit amet, consectetur adipiscing el');
response.set('body', '');
assert.equal(response.get('isDirty'), true);
assert.ok(post);
assert.equal(post.get('isDirty'), false);
post.set('postTitle', 'Lorem ipsum dolor sit amet, consectetur adipiscing el');
post.set('body', '');
assert.equal(post.get('isDirty'), true);

response.save().then({}, function(updateResponse) {
assert.ok(updateResponse);
assert.ok(updateResponse.errors);
post.save().then({}, function(response) {

var errors = updateResponse.errors;
assert.ok(response);
assert.ok(response.errors);

// Test camelCase field.
assert.equal(errors.postTitle.length, 1);
assert.equal(errors.postTitle[0], 'Ensure this value has at most 50 characters (it has 53).');
assert.equal(response.errors.postTitle.length, 1);
assert.equal(response.errors.postTitle[0], 'Ensure this value has at most 50 characters (it has 53).');

// Test non-camelCase field.
assert.equal(errors.body.length, 1);
assert.equal(errors.body[0], 'This field is required.');

start();
assert.equal(response.errors.body.length, 1);
assert.equal(response.errors.body[0], 'This field is required.');
});
});
});
Expand Down
102 changes: 44 additions & 58 deletions tests/acceptance/crud-success-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
test
} from 'qunit';
import Pretender from 'pretender';
import startApp from 'ember-django-adapter/tests/helpers/start-app';
import startApp from 'dummy/tests/helpers/start-app';

var application;
var store;
Expand Down Expand Up @@ -75,92 +75,78 @@ module('Acceptance: CRUD Success', {
}
});

/*
* These integration tests need to use the QUnit.stop() / QUnit.start()
* pattern as described in the following stackoverflow question:
*
* https://stackoverflow.com/questions/26317855/ember-cli-how-to-do-asynchronous-model-unit-testing-with-restadapter
*/
test('Retrieve list of non-paginated records', function(assert) {
assert.expect(4);

stop();
Ember.run(function() {
store.find('post').then(function(response) {
assert.ok(response);
return store.find('post').then(function(posts) {

assert.equal(response.get('length'), 3);
assert.ok(posts);
assert.equal(posts.get('length'), 3);

var post = response.objectAt(2);
assert.equal(post.get('postTitle'), 'post title 3');
assert.equal(post.get('body'), 'post body 3');
var post = posts.objectAt(2);

start();
});
assert.equal(post.get('postTitle'), 'post title 3');
assert.equal(post.get('body'), 'post body 3');
});
});

test('Retrieve single record', function(assert) {
assert.expect(3);

stop();
Ember.run(function() {
store.find('post', 1).then(function(response) {
assert.ok(response);
return Ember.run(function() {

assert.equal(response.get('postTitle'), 'post title 1');
assert.equal(response.get('body'), 'post body 1');
return store.find('post', 1).then(function(post) {

start();
assert.ok(post);
assert.equal(post.get('postTitle'), 'post title 1');
assert.equal(post.get('body'), 'post body 1');
});
});
});

test('Create record', function(assert) {
assert.expect(4);

var record,
data = {postTitle: 'post title 4', body: 'post body 4'};
return Ember.run(function() {

stop();
Ember.run(function() {
record = store.createRecord('post', data);
record.save().then(function(response) {
assert.ok(response);
var post = store.createRecord('post', {
postTitle: 'my new post title',
body: 'my new post body'
});

assert.equal(response.get('id'), 4);
assert.equal(response.get('title'), data['title']);
assert.equal(response.get('body'), data['body']);
return post.save().then(function(post) {

start();
assert.ok(post);
assert.equal(post.get('id'), 4);
assert.equal(post.get('postTitle'), 'my new post title');
assert.equal(post.get('body'), 'my new post body');
});
});
});

test('Update record', function(assert) {
assert.expect(7);

var postTitleUpdate = 'updated post title 1',
bodyUpdate = 'updated post body 1';
return Ember.run(function() {

return store.find('post', 1).then(function(post) {

stop();
Ember.run(function() {
store.find('post', 1).then(function(response) {
assert.ok(response);
assert.ok(post);
assert.equal(post.get('isDirty'), false);

assert.equal(response.get('isDirty'), false);
response.set('postTitle', postTitleUpdate);
response.set('body', bodyUpdate);
assert.equal(response.get('isDirty'), true);
return Ember.run(function() {

response.save().then(function(updateResponse) {
assert.ok(updateResponse);
post.set('postTitle', 'new post title');
post.set('body', 'new post body');
assert.equal(post.get('isDirty'), true);

assert.equal(updateResponse.get('isDirty'), false);
assert.equal(updateResponse.get('postTitle'), postTitleUpdate);
assert.equal(updateResponse.get('body'), bodyUpdate);
return post.save().then(function(post) {

start();
assert.ok(post);
assert.equal(post.get('isDirty'), false);
assert.equal(post.get('postTitle'), 'new post title');
assert.equal(post.get('body'), 'new post body');
});
});
});
});
Expand All @@ -169,15 +155,15 @@ test('Update record', function(assert) {
test('Delete record', function(assert) {
assert.expect(2);

stop();
Ember.run(function() {
store.find('post', 1).then(function(response) {
assert.ok(response);
return Ember.run(function() {

return store.find('post', 1).then(function(post) {

assert.ok(post);

response.destroyRecord().then(function(deleteResponse) {
assert.ok(deleteResponse);
return post.destroyRecord().then(function(post) {

start();
assert.ok(post);
});
});
});
Expand Down
Loading

0 comments on commit 66943b8

Please sign in to comment.