2017-06-14 13:57:58 -04:00
|
|
|
QUnit.module("rest-model");
|
2015-03-06 12:56:32 -05:00
|
|
|
|
|
|
|
import createStore from "helpers/create-store";
|
2015-04-08 14:44:44 -04:00
|
|
|
import RestModel from "discourse/models/rest";
|
2019-10-30 11:25:42 -04:00
|
|
|
import RestAdapter from "discourse/adapters/rest";
|
2015-04-08 14:44:44 -04:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("munging", (assert) => {
|
2015-04-08 14:44:44 -04:00
|
|
|
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 });
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal(g.get("inverse"), 0.6, "it runs `munge` on `create`");
|
2015-04-08 14:44:44 -04:00
|
|
|
});
|
2015-03-06 12:56:32 -05:00
|
|
|
|
2020-03-02 15:20:19 -05:00
|
|
|
QUnit.test("update", async (assert) => {
|
2015-03-06 12:56:32 -05:00
|
|
|
const store = createStore();
|
2020-03-02 15:20:19 -05:00
|
|
|
const widget = await store.find("widget", 123);
|
|
|
|
assert.equal(widget.get("name"), "Trout Lure");
|
|
|
|
assert.ok(!widget.get("isSaving"), "it is not saving");
|
2015-04-14 14:21:02 -04:00
|
|
|
|
2020-03-23 11:58:40 -04:00
|
|
|
const spyBeforeUpdate = sandbox.spy(widget, "beforeUpdate");
|
|
|
|
const spyAfterUpdate = sandbox.spy(widget, "afterUpdate");
|
2020-03-02 15:20:19 -05:00
|
|
|
const promise = widget.update({ name: "new name" });
|
|
|
|
assert.ok(widget.get("isSaving"), "it is saving");
|
2020-03-23 11:58:40 -04:00
|
|
|
assert.ok(spyBeforeUpdate.calledOn(widget));
|
2018-07-17 11:56:32 -04:00
|
|
|
|
2020-03-02 15:20:19 -05:00
|
|
|
const result = await promise;
|
2020-03-23 11:58:40 -04:00
|
|
|
assert.ok(spyAfterUpdate.calledOn(widget));
|
2020-03-02 15:20:19 -05:00
|
|
|
assert.ok(!widget.get("isSaving"), "it is no longer saving");
|
|
|
|
assert.equal(widget.get("name"), "new name");
|
2018-07-17 11:56:32 -04:00
|
|
|
|
2020-03-02 15:20:19 -05:00
|
|
|
assert.ok(result.target, "it has a reference to the record");
|
|
|
|
assert.equal(result.target.name, widget.get("name"));
|
2015-03-06 12:56:32 -05:00
|
|
|
});
|
|
|
|
|
2020-03-02 15:20:19 -05:00
|
|
|
QUnit.test("updating simultaneously", async (assert) => {
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.expect(2);
|
2015-04-14 14:21:02 -04:00
|
|
|
|
|
|
|
const store = createStore();
|
2020-03-02 15:20:19 -05:00
|
|
|
const widget = await store.find("widget", 123);
|
|
|
|
|
|
|
|
const firstPromise = widget.update({ name: "new name" });
|
|
|
|
const secondPromise = widget.update({ name: "new name" });
|
|
|
|
|
|
|
|
firstPromise.then(function () {
|
|
|
|
assert.ok(true, "the first promise succeeeds");
|
|
|
|
});
|
|
|
|
|
|
|
|
secondPromise.catch(function () {
|
|
|
|
assert.ok(true, "the second promise fails");
|
2015-04-14 14:21:02 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-02 15:20:19 -05:00
|
|
|
QUnit.test("save new", async (assert) => {
|
2015-04-09 14:54:17 -04:00
|
|
|
const store = createStore();
|
|
|
|
const widget = store.createRecord("widget");
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.ok(widget.get("isNew"), "it is a new record");
|
|
|
|
assert.ok(!widget.get("isCreated"), "it is not created");
|
2018-07-17 11:56:32 -04:00
|
|
|
assert.ok(!widget.get("isSaving"), "it is not saving");
|
2015-04-14 14:21:02 -04:00
|
|
|
|
2020-03-23 11:58:40 -04:00
|
|
|
const spyBeforeCreate = sandbox.spy(widget, "beforeCreate");
|
|
|
|
const spyAfterCreate = sandbox.spy(widget, "afterCreate");
|
2015-04-14 14:21:02 -04:00
|
|
|
const promise = widget.save({ name: "Evil Widget" });
|
2018-07-17 11:56:32 -04:00
|
|
|
assert.ok(widget.get("isSaving"), "it is not saving");
|
2020-03-23 11:58:40 -04:00
|
|
|
assert.ok(spyBeforeCreate.calledOn(widget));
|
2015-04-09 14:54:17 -04:00
|
|
|
|
2020-03-02 15:20:19 -05:00
|
|
|
const result = await promise;
|
2020-03-23 11:58:40 -04:00
|
|
|
assert.ok(spyAfterCreate.calledOn(widget));
|
2020-03-02 15:20:19 -05:00
|
|
|
assert.ok(!widget.get("isSaving"), "it is no longer saving");
|
|
|
|
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");
|
2018-07-17 11:56:32 -04:00
|
|
|
|
2020-03-02 15:20:19 -05:00
|
|
|
assert.ok(result.target, "it has a reference to the record");
|
|
|
|
assert.equal(result.target.name, widget.get("name"));
|
2015-04-09 14:54:17 -04:00
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("creating simultaneously", (assert) => {
|
|
|
|
assert.expect(2);
|
2015-04-14 14:21:02 -04:00
|
|
|
|
|
|
|
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 () {
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.ok(true, "the first promise succeeeds");
|
2015-04-14 14:21:02 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
secondPromise.catch(function () {
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.ok(true, "the second promise fails");
|
2015-04-14 14:21:02 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-26 16:00:10 -04:00
|
|
|
QUnit.test("destroyRecord", async (assert) => {
|
2015-03-06 12:56:32 -05:00
|
|
|
const store = createStore();
|
2020-03-26 16:00:10 -04:00
|
|
|
const widget = await store.find("widget", 123);
|
|
|
|
|
|
|
|
assert.ok(await widget.destroyRecord());
|
2015-03-06 12:56:32 -05:00
|
|
|
});
|
2019-10-30 11:25:42 -04:00
|
|
|
|
|
|
|
QUnit.test("custom api name", async (assert) => {
|
|
|
|
const store = createStore((type) => {
|
|
|
|
if (type === "adapter:my-widget") {
|
|
|
|
return RestAdapter.extend({
|
|
|
|
// An adapter like this is used when the server-side key/url
|
|
|
|
// do not match the name of the es6 class
|
|
|
|
apiNameFor() {
|
|
|
|
return "widget";
|
|
|
|
},
|
|
|
|
}).create();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// The pretenders only respond to requests for `widget`
|
|
|
|
// If these basic tests pass, the name override worked correctly
|
|
|
|
|
|
|
|
//Create
|
|
|
|
const widget = store.createRecord("my-widget");
|
|
|
|
await widget.save({ name: "Evil Widget" });
|
|
|
|
assert.equal(widget.id, 100, "it saved a new record successully");
|
|
|
|
assert.equal(widget.get("name"), "Evil Widget");
|
|
|
|
|
|
|
|
// Update
|
|
|
|
await widget.update({ name: "new name" });
|
|
|
|
assert.equal(widget.get("name"), "new name");
|
|
|
|
|
|
|
|
// Destroy
|
|
|
|
await widget.destroyRecord();
|
|
|
|
|
|
|
|
// Lookup
|
|
|
|
const foundWidget = await store.find("my-widget", 123);
|
|
|
|
assert.equal(foundWidget.name, "Trout Lure");
|
|
|
|
});
|