Last active
August 29, 2015 14:15
-
-
Save kellysutton/033ce19269ee5c4677f2 to your computer and use it in GitHub Desktop.
Unit Testing Ember Data in Controllers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Include everything from above | |
test('the "save" action calls #save', function () { | |
// The expect is crucial here, otherwise we can get | |
// false positives. | |
expect(1); | |
var model = null; | |
store.scheduleSave = function(context, resolver) { | |
ok(resolver.promise); // The actual assert | |
resolver.resolve(context); | |
}; | |
// In this example, a vanilla Ember.Object.create() | |
// would work here. However, once you start dealing with | |
// setting up relationships in tests, Ember.Object.create() | |
// will need to be replaced with the following format. | |
Ember.run(function () { | |
model = store.createRecord('my-model'); | |
}); | |
var controller = this.subject({ | |
store: store, | |
model: model | |
}); | |
controller.send('save'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Include everything from the first snippet | |
test('"changeAuthor" sends off an AJAX request', function () { | |
expect(1); | |
var sentACreateRelationshipRequest = false; | |
var post = null; | |
var tag = null; | |
// We assume that both the Post and Tag model | |
// are defined and extend `DS.Model` | |
Ember.run(function () { | |
post = store.createRecord('post'); | |
tag = store.createRecord('tag'); | |
}); | |
store.adapterFor = function(model) { | |
return Ember.Object.create({ | |
ajax: function (path, method, extra) { | |
// We use this method of checking to make | |
// sure that we don’t fail if other requests | |
// are made | |
if (path.indexOf(`/posts/${post.get('id')}/links/tags`) > -1 && | |
method === "POST" && | |
extra.data && | |
extra.data.tags[0] === tag.get('id') | |
) { | |
sentACreateRelationshipRequest = true; | |
} | |
// Finally, make sure that we resolve any promises to | |
// not block things up. | |
return new Ember.RSVP.Promise(function (resolve, reject) { | |
resolve(); | |
}); | |
} | |
}); | |
}; | |
var controller = this.subject({ | |
store: store, | |
model: model | |
}); | |
ok(sentACreateRelationshipRequest); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// tests/unit/controllers/posts/show-test.js | |
import Ember from 'ember'; | |
import startApp from '../../../helpers/start-app'; | |
import { | |
moduleFor, | |
test | |
} from 'ember-qunit'; | |
var App; | |
var store; | |
moduleFor('controller:posts/show', 'PostsShowController', { | |
needs: ['controller:application'], | |
setup: function () { | |
App = startApp(); | |
store = App.__container__.lookup('store:main'); | |
}, | |
teardown: function () { | |
Ember.run(App, App.destroy); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//app/controllers/posts/show.js | |
import Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
actions: { | |
addTag: (tag) { | |
var post = this.get('model'); | |
// Push the tag immediately to update the UI | |
post.get('tags').pushObject(tag); | |
// Fire off the JSON API-compliant AJAX request | |
// to persist the relationship modification | |
// | |
// This request will add the given `tag` to the | |
// specified `post`. | |
var adapter = this.adapterFor(post) | |
adapter.ajax( | |
`/api/v1/posts/${post.get('id')}/links/tags`, | |
"POST", | |
{ | |
tags: [tag.get('id')] | |
} | |
); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment