DEV: Support custom server-side names in rest models (#8265)

This commit is contained in:
David Taylor
2019-10-30 15:25:42 +00:00
committed by GitHub
parent c3f06943c7
commit 7191835989
4 changed files with 62 additions and 8 deletions

View File

@ -2,6 +2,7 @@ QUnit.module("rest-model");
import createStore from "helpers/create-store";
import RestModel from "discourse/models/rest";
import RestAdapter from "discourse/adapters/rest";
QUnit.test("munging", assert => {
const store = createStore();
@ -101,3 +102,37 @@ QUnit.test("destroyRecord", assert => {
});
});
});
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");
});