Hope you can help me with the following:
I am writing tests for my serializer (using Pretender), following this guide: http://ift.tt/1opSXkW.
I am confused by the fact that i can call store.findAll in normal test code (no Ember.run needed), but when I call store.findRecord i need to put my code inside Ember.run for it to work - why is that ?
I am using: "ember-data": "^2.3.0", "ember": "~2.3.1",
test/unit/serializers/activity-tests.js:
import { moduleForModel, test } from 'ember-qunit';
import Pretender from 'pretender';
import Ember from 'ember';
var server;
moduleForModel('activity', 'Unit | Serializer | activity', {
needs: ['serializer:activity'],
afterEach() {
if(server) {
server.shutdown();
server = undefined;
}
}
});
test('it serializes array responses', function(assert) {
server = new Pretender(function() {
this.get('/activities', function() {
var response = [
{ "id": "1", "reference": "Tubby" },
{ "id": "2", "reference": "Spot" },
{ "id": "3", "reference": "Chestnut" }
];
return [200, { "Content-Type": "application/json" }, JSON.stringify(response)];
});
});
//
// Why does this run without Ember.run loop
// (it also does when inside run)
//
// Ember.run(() => {
return this.store().findAll('activity').then((data) => {
assert.equal(data.get('length'), 3);
});
// });
});
test('it gets a single record response', function(assert) {
server = new Pretender(function() {
this.get('/activities/:id', function(/*request*/) {
var response = { "id": "1", "reference": "Tubby" };
return [200, { "Content-Type": "application/json" }, JSON.stringify(response)];
});
});
//
// Why does this only succeed when inside the Ember.run loop
// while the findAll (above) can run without it ?
//
Ember.run(() => {
return this.store().findRecord('activity', "1").then((data) => {
assert.ok(!!data);
assert.equal(data.get('reference'), 'Tubby');
});
});
});
app/serializers/activity.js:
import DS from 'ember-data';
export default DS.JSONSerializer.extend({
});
app/models/activity.js:
import DS from 'ember-data';
export default DS.Model.extend({
reference: DS.attr()
});
Aucun commentaire:
Enregistrer un commentaire