Add tests for our store

This commit is contained in:
Robin Ward 2015-03-06 12:37:24 -05:00
parent ea1cd8dcd4
commit ecb553af3f
4 changed files with 82 additions and 2 deletions

View File

@ -47,7 +47,7 @@ export default Ember.Object.extend({
find(type, id) {
var self = this;
return Discourse.ajax(this.pathFor(type, id)).then(function(result) {
return self._hydrate(type, result[self.serverName(type)]);
return self._hydrate(type, result[Ember.String.underscore(type)]);
});
},
@ -67,7 +67,8 @@ export default Ember.Object.extend({
destroyRecord(type, id) {
return Discourse.ajax(this.pathFor(type, id), { method: 'DELETE' }).then(function(result) {
delete _identityMap[type][id];
const forType = _identityMap[type];
if (forType) { delete forType[id]; }
return result;
});
},

View File

@ -19,6 +19,11 @@ function success() {
return response({ success: true });
}
const _widgets = [
{id: 123, name: 'Trout Lure'},
{id: 124, name: 'Evil Repellant'}
];
export default function() {
var server = new Pretender(function() {
@ -84,6 +89,21 @@ export default function() {
this.delete('/posts/:post_id', success);
this.put('/posts/:post_id/recover', success);
this.get('/widgets/:widget_id', function(request) {
const w = _widgets.findBy('id', parseInt(request.params.widget_id));
if (w) {
return response({widget: w});
} else {
return response(404);
}
});
this.get('/widgets', function() {
return response({ widgets: _widgets });
});
this.delete('/widgets/:widget_id', success);
});

View File

@ -0,0 +1,19 @@
import Store from "discourse/models/store";
import RestAdapter from 'discourse/adapters/rest';
let _restAdapter;
export default function() {
return Store.create({
container: {
lookup(type) {
if (type === "adapter:rest") {
_restAdapter = _restAdapter || RestAdapter.create({ container: this });
return (_restAdapter);
}
},
lookupFactory: function() { }
}
});
}

View File

@ -0,0 +1,40 @@
module('store:main');
import createStore from 'helpers/create-store';
test('createRecord', function() {
const store = createStore();
const widget = store.createRecord('widget', {id: 111, name: 'hello'});
equal(widget.get('name'), 'hello');
equal(widget.get('id'), 111);
});
test('find', function() {
const store = createStore();
store.find('widget', 123).then(function(w) {
equal(w.get('name'), 'Trout Lure');
equal(w.get('id'), 123);
// A second find by id returns the same object
store.find('widget', 123).then(function(w2) {
equal(w, w2);
});
});
});
test('findAll', function() {
const store = createStore();
store.findAll('widget').then(function(result) {
equal(result.length, 2);
const w = result.findBy('id', 124);
equal(w.get('name'), 'Evil Repellant');
});
});
test('destroyRecord', function() {
const store = createStore();
store.destroyRecord('widget', 124).then(function(result) {
ok(result);
});
});