2015-03-06 12:56:32 -05:00
|
|
|
module('rest-model');
|
|
|
|
|
|
|
|
import createStore from 'helpers/create-store';
|
2015-04-08 14:44:44 -04:00
|
|
|
import RestModel from 'discourse/models/rest';
|
|
|
|
|
|
|
|
test('munging', function() {
|
|
|
|
const store = createStore();
|
|
|
|
const Grape = RestModel.extend();
|
|
|
|
Grape.reopenClass({
|
|
|
|
munge: function(json) {
|
|
|
|
json.inverse = 1 - json.percent;
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var g = Grape.create({ store, percent: 0.4 });
|
|
|
|
equal(g.get('inverse'), 0.6, 'it runs `munge` on `create`');
|
|
|
|
});
|
2015-03-06 12:56:32 -05:00
|
|
|
|
|
|
|
test('update', function() {
|
|
|
|
const store = createStore();
|
2015-05-11 11:30:14 -04:00
|
|
|
return store.find('widget', 123).then(function(widget) {
|
2015-03-06 12:56:32 -05:00
|
|
|
equal(widget.get('name'), 'Trout Lure');
|
2015-04-14 14:21:02 -04:00
|
|
|
|
|
|
|
ok(!widget.get('isSaving'));
|
|
|
|
const promise = widget.update({ name: 'new name' });
|
|
|
|
ok(widget.get('isSaving'));
|
|
|
|
promise.then(function() {
|
|
|
|
ok(!widget.get('isSaving'));
|
2015-03-06 12:56:32 -05:00
|
|
|
equal(widget.get('name'), 'new name');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-04-14 14:21:02 -04:00
|
|
|
test('updating simultaneously', function() {
|
|
|
|
expect(2);
|
|
|
|
|
|
|
|
const store = createStore();
|
2015-05-05 13:44:19 -04:00
|
|
|
return store.find('widget', 123).then(function(widget) {
|
2015-04-14 14:21:02 -04:00
|
|
|
|
|
|
|
const firstPromise = widget.update({ name: 'new name' });
|
|
|
|
const secondPromise = widget.update({ name: 'new name' });
|
|
|
|
firstPromise.then(function() {
|
|
|
|
ok(true, 'the first promise succeeeds');
|
|
|
|
});
|
|
|
|
|
|
|
|
secondPromise.catch(function() {
|
|
|
|
ok(true, 'the second promise fails');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-04-09 14:54:17 -04:00
|
|
|
test('save new', function() {
|
|
|
|
const store = createStore();
|
|
|
|
const widget = store.createRecord('widget');
|
|
|
|
|
|
|
|
ok(widget.get('isNew'), 'it is a new record');
|
|
|
|
ok(!widget.get('isCreated'), 'it is not created');
|
2015-04-14 14:21:02 -04:00
|
|
|
ok(!widget.get('isSaving'));
|
|
|
|
|
|
|
|
const promise = widget.save({ name: 'Evil Widget' });
|
|
|
|
ok(widget.get('isSaving'));
|
2015-04-09 14:54:17 -04:00
|
|
|
|
2015-05-11 11:30:14 -04:00
|
|
|
return promise.then(function() {
|
2015-04-14 14:21:02 -04:00
|
|
|
ok(!widget.get('isSaving'));
|
2015-04-09 14:54:17 -04:00
|
|
|
ok(widget.get('id'), 'it has an id');
|
|
|
|
ok(widget.get('name'), 'Evil Widget');
|
|
|
|
ok(widget.get('isCreated'), 'it is created');
|
|
|
|
ok(!widget.get('isNew'), 'it is no longer new');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-04-14 14:21:02 -04:00
|
|
|
test('creating simultaneously', function() {
|
|
|
|
expect(2);
|
|
|
|
|
|
|
|
const store = createStore();
|
|
|
|
const widget = store.createRecord('widget');
|
|
|
|
|
|
|
|
const firstPromise = widget.save({ name: 'Evil Widget' });
|
|
|
|
const secondPromise = widget.save({ name: 'Evil Widget' });
|
|
|
|
firstPromise.then(function() {
|
|
|
|
ok(true, 'the first promise succeeeds');
|
|
|
|
});
|
|
|
|
|
|
|
|
secondPromise.catch(function() {
|
|
|
|
ok(true, 'the second promise fails');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-03-06 12:56:32 -05:00
|
|
|
test('destroyRecord', function() {
|
|
|
|
const store = createStore();
|
2015-05-05 13:44:19 -04:00
|
|
|
return store.find('widget', 123).then(function(widget) {
|
2015-03-06 12:56:32 -05:00
|
|
|
widget.destroyRecord().then(function(result) {
|
|
|
|
ok(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|