discourse/test/javascripts/models/rest-model-test.js.es6

97 lines
2.7 KiB
Plaintext
Raw Normal View History

2018-06-15 23:03:24 +08:00
QUnit.module("rest-model");
2018-06-15 23:03:24 +08:00
import createStore from "helpers/create-store";
import RestModel from "discourse/models/rest";
2018-06-15 23:03:24 +08:00
QUnit.test("munging", assert => {
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 });
2018-06-15 23:03:24 +08:00
assert.equal(g.get("inverse"), 0.6, "it runs `munge` on `create`");
});
2018-06-15 23:03:24 +08:00
QUnit.test("update", assert => {
const store = createStore();
2018-06-15 23:03:24 +08:00
return store.find("widget", 123).then(function(widget) {
assert.equal(widget.get("name"), "Trout Lure");
2018-06-15 23:03:24 +08:00
assert.ok(!widget.get("isSaving"));
const promise = widget.update({ name: "new name" });
assert.ok(widget.get("isSaving"));
promise.then(function() {
2018-06-15 23:03:24 +08:00
assert.ok(!widget.get("isSaving"));
assert.equal(widget.get("name"), "new name");
});
});
});
2018-06-15 23:03:24 +08:00
QUnit.test("updating simultaneously", assert => {
2017-06-15 01:57:58 +08:00
assert.expect(2);
const store = createStore();
2018-06-15 23:03:24 +08:00
return store.find("widget", 123).then(function(widget) {
const firstPromise = widget.update({ name: "new name" });
const secondPromise = widget.update({ name: "new name" });
firstPromise.then(function() {
2018-06-15 23:03:24 +08:00
assert.ok(true, "the first promise succeeeds");
});
secondPromise.catch(function() {
2018-06-15 23:03:24 +08:00
assert.ok(true, "the second promise fails");
});
});
});
2018-06-15 23:03:24 +08:00
QUnit.test("save new", assert => {
2015-04-10 02:54:17 +08:00
const store = createStore();
2018-06-15 23:03:24 +08:00
const widget = store.createRecord("widget");
2015-04-10 02:54:17 +08:00
2018-06-15 23:03:24 +08:00
assert.ok(widget.get("isNew"), "it is a new record");
assert.ok(!widget.get("isCreated"), "it is not created");
assert.ok(!widget.get("isSaving"));
2018-06-15 23:03:24 +08:00
const promise = widget.save({ name: "Evil Widget" });
assert.ok(widget.get("isSaving"));
2015-04-10 02:54:17 +08:00
2015-05-11 23:30:14 +08:00
return promise.then(function() {
2018-06-15 23:03:24 +08:00
assert.ok(!widget.get("isSaving"));
assert.ok(widget.get("id"), "it has an id");
assert.ok(widget.get("name"), "Evil Widget");
assert.ok(widget.get("isCreated"), "it is created");
assert.ok(!widget.get("isNew"), "it is no longer new");
2015-04-10 02:54:17 +08:00
});
});
2018-06-15 23:03:24 +08:00
QUnit.test("creating simultaneously", assert => {
2017-06-15 01:57:58 +08:00
assert.expect(2);
const store = createStore();
2018-06-15 23:03:24 +08:00
const widget = store.createRecord("widget");
2018-06-15 23:03:24 +08:00
const firstPromise = widget.save({ name: "Evil Widget" });
const secondPromise = widget.save({ name: "Evil Widget" });
firstPromise.then(function() {
2018-06-15 23:03:24 +08:00
assert.ok(true, "the first promise succeeeds");
});
secondPromise.catch(function() {
2018-06-15 23:03:24 +08:00
assert.ok(true, "the second promise fails");
});
});
2018-06-15 23:03:24 +08:00
QUnit.test("destroyRecord", assert => {
const store = createStore();
2018-06-15 23:03:24 +08:00
return store.find("widget", 123).then(function(widget) {
widget.destroyRecord().then(function(result) {
2017-06-15 01:57:58 +08:00
assert.ok(result);
});
});
});