2018-06-15 11:03:24 -04:00
|
|
|
QUnit.module("service:store");
|
2015-03-06 12:37:24 -05:00
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
import createStore from "helpers/create-store";
|
2015-03-06 12:37:24 -05:00
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
QUnit.test("createRecord", assert => {
|
2015-03-06 12:37:24 -05:00
|
|
|
const store = createStore();
|
2018-06-15 11:03:24 -04:00
|
|
|
const widget = store.createRecord("widget", { id: 111, name: "hello" });
|
2015-04-09 14:54:17 -04:00
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
assert.ok(!widget.get("isNew"), "it is not a new record");
|
|
|
|
assert.equal(widget.get("name"), "hello");
|
|
|
|
assert.equal(widget.get("id"), 111);
|
2015-03-06 12:37:24 -05:00
|
|
|
});
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
QUnit.test("createRecord without an `id`", assert => {
|
2015-04-08 14:44:44 -04:00
|
|
|
const store = createStore();
|
2018-06-15 11:03:24 -04:00
|
|
|
const widget = store.createRecord("widget", { name: "hello" });
|
2015-04-08 14:44:44 -04:00
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
assert.ok(widget.get("isNew"), "it is a new record");
|
|
|
|
assert.ok(!widget.get("id"), "there is no id");
|
2015-04-08 14:44:44 -04:00
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("createRecord doesn't modify the input `id` field", assert => {
|
2016-07-06 13:52:30 -04:00
|
|
|
const store = createStore();
|
2018-06-15 11:03:24 -04:00
|
|
|
const widget = store.createRecord("widget", { id: 1, name: "hello" });
|
2016-07-06 13:52:30 -04:00
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
const obj = { id: 1, name: "something" };
|
2016-07-06 13:52:30 -04:00
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
const other = store.createRecord("widget", obj);
|
|
|
|
assert.equal(widget, other, "returns the same record");
|
|
|
|
assert.equal(widget.name, "something", "it updates the properties");
|
|
|
|
assert.equal(obj.id, 1, "it does not remove the id from the input");
|
2016-07-06 13:52:30 -04:00
|
|
|
});
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
QUnit.test("createRecord without attributes", assert => {
|
2015-04-08 14:44:44 -04:00
|
|
|
const store = createStore();
|
2018-06-15 11:03:24 -04:00
|
|
|
const widget = store.createRecord("widget");
|
2015-04-08 14:44:44 -04:00
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
assert.ok(!widget.get("id"), "there is no id");
|
|
|
|
assert.ok(widget.get("isNew"), "it is a new record");
|
2015-04-08 14:44:44 -04:00
|
|
|
});
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
QUnit.test(
|
|
|
|
"createRecord with a record as attributes returns that record from the map",
|
|
|
|
assert => {
|
|
|
|
const store = createStore();
|
|
|
|
const widget = store.createRecord("widget", { id: 33 });
|
|
|
|
const secondWidget = store.createRecord("widget", { id: 33 });
|
2015-04-08 14:44:44 -04:00
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
assert.equal(widget, secondWidget, "they should be the same");
|
|
|
|
}
|
|
|
|
);
|
2015-04-08 14:44:44 -04:00
|
|
|
|
2020-03-02 15:20:19 -05:00
|
|
|
QUnit.test("find", async assert => {
|
2015-03-06 12:37:24 -05:00
|
|
|
const store = createStore();
|
2018-03-29 02:57:10 -04:00
|
|
|
|
2020-03-02 15:20:19 -05:00
|
|
|
const widget = await store.find("widget", 123);
|
|
|
|
assert.equal(widget.get("name"), "Trout Lure");
|
|
|
|
assert.equal(widget.get("id"), 123);
|
|
|
|
assert.ok(!widget.get("isNew"), "found records are not new");
|
|
|
|
assert.equal(widget.get("extras.hello"), "world", "extra attributes are set");
|
|
|
|
|
|
|
|
// A second find by id returns the same object
|
|
|
|
const widget2 = await store.find("widget", 123);
|
|
|
|
assert.equal(widget, widget2);
|
|
|
|
assert.equal(widget.get("extras.hello"), "world", "extra attributes are set");
|
2015-03-16 15:14:33 -04:00
|
|
|
});
|
2015-03-06 12:37:24 -05:00
|
|
|
|
2020-03-02 15:20:19 -05:00
|
|
|
QUnit.test("find with object id", async assert => {
|
2015-03-16 15:14:33 -04:00
|
|
|
const store = createStore();
|
2020-03-02 15:20:19 -05:00
|
|
|
const widget = await store.find("widget", { id: 123 });
|
|
|
|
assert.equal(widget.get("firstObject.name"), "Trout Lure");
|
2015-03-16 15:14:33 -04:00
|
|
|
});
|
|
|
|
|
2020-03-02 15:20:19 -05:00
|
|
|
QUnit.test("find with query param", async assert => {
|
2015-03-16 15:14:33 -04:00
|
|
|
const store = createStore();
|
2020-03-02 15:20:19 -05:00
|
|
|
const widget = await store.find("widget", { name: "Trout Lure" });
|
|
|
|
assert.equal(widget.get("firstObject.id"), 123);
|
2015-03-06 12:37:24 -05:00
|
|
|
});
|
|
|
|
|
2020-03-02 15:20:19 -05:00
|
|
|
QUnit.test("findStale with no stale results", async assert => {
|
2015-08-31 14:09:57 -04:00
|
|
|
const store = createStore();
|
2018-06-15 11:03:24 -04:00
|
|
|
const stale = store.findStale("widget", { name: "Trout Lure" });
|
2015-08-31 14:09:57 -04:00
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
assert.ok(!stale.hasResults, "there are no stale results");
|
|
|
|
assert.ok(!stale.results, "results are present");
|
2020-03-02 15:20:19 -05:00
|
|
|
const widget = await stale.refresh();
|
|
|
|
assert.equal(
|
|
|
|
widget.get("firstObject.id"),
|
|
|
|
123,
|
|
|
|
"a `refresh()` method provides results for stale"
|
|
|
|
);
|
2015-08-31 14:09:57 -04: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 result = await store.update("widget", 123, { name: "hello" });
|
|
|
|
assert.ok(result);
|
2015-03-06 12:56:32 -05:00
|
|
|
});
|
|
|
|
|
2020-03-02 15:20:19 -05:00
|
|
|
QUnit.test("update with a multi world name", async assert => {
|
2015-07-30 14:52:53 -04:00
|
|
|
const store = createStore();
|
2020-03-02 15:20:19 -05:00
|
|
|
const result = await store.update("cool-thing", 123, { name: "hello" });
|
|
|
|
assert.ok(result);
|
|
|
|
assert.equal(result.payload.name, "hello");
|
2015-07-30 14:52:53 -04:00
|
|
|
});
|
|
|
|
|
2020-03-02 15:20:19 -05:00
|
|
|
QUnit.test("findAll", async assert => {
|
2015-03-06 12:37:24 -05:00
|
|
|
const store = createStore();
|
2020-03-02 15:20:19 -05:00
|
|
|
const result = await store.findAll("widget");
|
|
|
|
assert.equal(result.get("length"), 2);
|
|
|
|
|
|
|
|
const widget = result.findBy("id", 124);
|
|
|
|
assert.ok(!widget.get("isNew"), "found records are not new");
|
|
|
|
assert.equal(widget.get("name"), "Evil Repellant");
|
2015-03-06 12:37:24 -05:00
|
|
|
});
|
|
|
|
|
2020-03-26 16:00:10 -04:00
|
|
|
QUnit.test("destroyRecord", async assert => {
|
2015-03-06 12:37:24 -05:00
|
|
|
const store = createStore();
|
2020-03-26 16:00:10 -04:00
|
|
|
const widget = await store.find("widget", 123);
|
|
|
|
|
|
|
|
assert.ok(await store.destroyRecord("widget", widget));
|
2015-03-06 12:37:24 -05:00
|
|
|
});
|
2015-04-14 14:21:02 -04:00
|
|
|
|
2020-03-26 16:00:10 -04:00
|
|
|
QUnit.test("destroyRecord when new", async assert => {
|
2015-08-06 12:43:56 -04:00
|
|
|
const store = createStore();
|
2020-03-26 16:00:10 -04:00
|
|
|
const widget = store.createRecord("widget", { name: "hello" });
|
|
|
|
|
|
|
|
assert.ok(await store.destroyRecord("widget", widget));
|
2015-08-06 12:43:56 -04:00
|
|
|
});
|
|
|
|
|
2020-03-02 15:20:19 -05:00
|
|
|
QUnit.test("find embedded", async assert => {
|
2015-04-14 14:21:02 -04:00
|
|
|
const store = createStore();
|
2020-03-02 15:20:19 -05:00
|
|
|
const fruit = await store.find("fruit", 1);
|
|
|
|
assert.ok(fruit.get("farmer"), "it has the embedded object");
|
2015-08-18 17:15:46 -04:00
|
|
|
|
2020-03-02 15:20:19 -05:00
|
|
|
const fruitCols = fruit.get("colors");
|
|
|
|
assert.equal(fruitCols.length, 2);
|
|
|
|
assert.equal(fruitCols[0].get("id"), 1);
|
|
|
|
assert.equal(fruitCols[1].get("id"), 2);
|
2015-08-18 17:15:46 -04:00
|
|
|
|
2020-03-02 15:20:19 -05:00
|
|
|
assert.ok(fruit.get("category"), "categories are found automatically");
|
2015-04-14 14:21:02 -04:00
|
|
|
});
|
|
|
|
|
2019-05-08 10:20:51 -04:00
|
|
|
QUnit.test("embedded records can be cleared", async assert => {
|
|
|
|
const store = createStore();
|
2020-03-02 15:20:19 -05:00
|
|
|
let fruit = await store.find("fruit", 4);
|
|
|
|
fruit.set("farmer", { dummy: "object" });
|
|
|
|
|
|
|
|
fruit = await store.find("fruit", 4);
|
|
|
|
assert.ok(!fruit.get("farmer"));
|
2019-05-08 10:20:51 -04:00
|
|
|
});
|
|
|
|
|
2020-03-02 15:20:19 -05:00
|
|
|
QUnit.test("meta types", async assert => {
|
2017-09-12 13:04:53 -04:00
|
|
|
const store = createStore();
|
2020-03-02 15:20:19 -05:00
|
|
|
const barn = await store.find("barn", 1);
|
|
|
|
assert.equal(
|
|
|
|
barn.get("owner.name"),
|
|
|
|
"Old MacDonald",
|
|
|
|
"it has the embedded farmer"
|
|
|
|
);
|
2017-09-12 13:04:53 -04:00
|
|
|
});
|
|
|
|
|
2020-03-02 15:20:19 -05:00
|
|
|
QUnit.test("findAll embedded", async assert => {
|
2015-04-21 13:15:40 -04:00
|
|
|
const store = createStore();
|
2020-03-02 15:20:19 -05:00
|
|
|
const fruits = await store.findAll("fruit");
|
|
|
|
assert.equal(fruits.objectAt(0).get("farmer.name"), "Old MacDonald");
|
|
|
|
assert.equal(
|
|
|
|
fruits.objectAt(0).get("farmer"),
|
|
|
|
fruits.objectAt(1).get("farmer"),
|
|
|
|
"points at the same object"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
fruits.get("extras.hello"),
|
|
|
|
"world",
|
|
|
|
"it can supply extra information"
|
|
|
|
);
|
2015-08-18 17:15:46 -04:00
|
|
|
|
2020-03-02 15:20:19 -05:00
|
|
|
const fruitCols = fruits.objectAt(0).get("colors");
|
|
|
|
assert.equal(fruitCols.length, 2);
|
|
|
|
assert.equal(fruitCols[0].get("id"), 1);
|
|
|
|
assert.equal(fruitCols[1].get("id"), 2);
|
2015-08-18 17:15:46 -04:00
|
|
|
|
2020-03-02 15:20:19 -05:00
|
|
|
assert.equal(fruits.objectAt(2).get("farmer.name"), "Luke Skywalker");
|
2017-09-12 13:04:53 -04:00
|
|
|
});
|
2020-03-30 09:23:59 -04:00
|
|
|
|
|
|
|
QUnit.test("custom primaryKey", async assert => {
|
|
|
|
const store = createStore();
|
|
|
|
const cats = await store.findAll("cat");
|
|
|
|
assert.equal(cats.objectAt(0).name, "souna");
|
|
|
|
});
|