2017-06-14 13:57:58 -04:00
|
|
|
QUnit.module('store:main');
|
2015-03-06 12:37:24 -05:00
|
|
|
|
|
|
|
import createStore from 'helpers/create-store';
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test('createRecord', assert => {
|
2015-03-06 12:37:24 -05:00
|
|
|
const store = createStore();
|
|
|
|
const widget = store.createRecord('widget', {id: 111, name: 'hello'});
|
2015-04-09 14:54:17 -04:00
|
|
|
|
2017-06-14 13:57:58 -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
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test('createRecord without an `id`', assert => {
|
2015-04-08 14:44:44 -04:00
|
|
|
const store = createStore();
|
|
|
|
const widget = store.createRecord('widget', {name: 'hello'});
|
|
|
|
|
2017-06-14 13:57:58 -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();
|
|
|
|
const widget = store.createRecord('widget', {id: 1, name: 'hello'});
|
|
|
|
|
|
|
|
const obj = { id: 1, name: 'something' };
|
|
|
|
|
|
|
|
const other = store.createRecord('widget', obj);
|
2017-06-14 13:57:58 -04:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test('createRecord without attributes', assert => {
|
2015-04-08 14:44:44 -04:00
|
|
|
const store = createStore();
|
|
|
|
const widget = store.createRecord('widget');
|
|
|
|
|
2017-06-14 13:57:58 -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
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test('createRecord with a record as attributes returns that record from the map', assert => {
|
2015-04-08 14:44:44 -04:00
|
|
|
const store = createStore();
|
|
|
|
const widget = store.createRecord('widget', {id: 33});
|
|
|
|
const secondWidget = store.createRecord('widget', {id: 33});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal(widget, secondWidget, 'they should be the same');
|
2015-04-08 14:44:44 -04:00
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test('find', assert => {
|
2015-03-06 12:37:24 -05:00
|
|
|
const store = createStore();
|
2015-05-01 16:35:52 -04:00
|
|
|
return store.find('widget', 123).then(function(w) {
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal(w.get('name'), 'Trout Lure');
|
|
|
|
assert.equal(w.get('id'), 123);
|
|
|
|
assert.ok(!w.get('isNew'), 'found records are not new');
|
2015-03-06 12:37:24 -05:00
|
|
|
|
|
|
|
// A second find by id returns the same object
|
|
|
|
store.find('widget', 123).then(function(w2) {
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal(w, w2);
|
2015-03-06 12:37:24 -05:00
|
|
|
});
|
2015-03-16 15:14:33 -04:00
|
|
|
});
|
|
|
|
});
|
2015-03-06 12:37:24 -05:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test('find with object id', assert => {
|
2015-03-16 15:14:33 -04:00
|
|
|
const store = createStore();
|
2015-05-01 16:35:52 -04:00
|
|
|
return store.find('widget', {id: 123}).then(function(w) {
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal(w.get('firstObject.name'), 'Trout Lure');
|
2015-03-16 15:14:33 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test('find with query param', assert => {
|
2015-03-16 15:14:33 -04:00
|
|
|
const store = createStore();
|
2015-05-01 16:35:52 -04:00
|
|
|
return store.find('widget', {name: 'Trout Lure'}).then(function(w) {
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal(w.get('firstObject.id'), 123);
|
2015-03-06 12:37:24 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test('findStale with no stale results', (assert) => {
|
2015-08-31 14:09:57 -04:00
|
|
|
const store = createStore();
|
|
|
|
const stale = store.findStale('widget', {name: 'Trout Lure'});
|
|
|
|
|
|
|
|
assert.ok(!stale.hasResults, 'there are no stale results');
|
|
|
|
assert.ok(!stale.results, 'results are present');
|
|
|
|
return stale.refresh().then(function(w) {
|
|
|
|
assert.equal(w.get('firstObject.id'), 123, 'a `refresh()` method provides results for stale');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test('update', assert => {
|
2015-03-06 12:56:32 -05:00
|
|
|
const store = createStore();
|
2015-05-01 16:35:52 -04:00
|
|
|
return store.update('widget', 123, {name: 'hello'}).then(function(result) {
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.ok(result);
|
2015-03-06 12:56:32 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test('update with a multi world name', function(assert) {
|
2015-07-30 14:52:53 -04:00
|
|
|
const store = createStore();
|
|
|
|
return store.update('cool-thing', 123, {name: 'hello'}).then(function(result) {
|
|
|
|
assert.ok(result);
|
|
|
|
assert.equal(result.payload.name, 'hello');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test('findAll', assert => {
|
2015-03-06 12:37:24 -05:00
|
|
|
const store = createStore();
|
2015-05-01 16:35:52 -04:00
|
|
|
return store.findAll('widget').then(function(result) {
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal(result.get('length'), 2);
|
2015-03-06 12:37:24 -05:00
|
|
|
const w = result.findBy('id', 124);
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.ok(!w.get('isNew'), 'found records are not new');
|
|
|
|
assert.equal(w.get('name'), 'Evil Repellant');
|
2015-03-06 12:37:24 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test('destroyRecord', function(assert) {
|
2015-03-06 12:37:24 -05:00
|
|
|
const store = createStore();
|
2015-05-01 16:35:52 -04:00
|
|
|
return store.find('widget', 123).then(function(w) {
|
2015-03-06 12:56:32 -05:00
|
|
|
store.destroyRecord('widget', w).then(function(result) {
|
2015-07-30 14:52:53 -04:00
|
|
|
assert.ok(result);
|
2015-03-06 12:56:32 -05:00
|
|
|
});
|
2015-03-06 12:37:24 -05:00
|
|
|
});
|
|
|
|
});
|
2015-04-14 14:21:02 -04:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test('destroyRecord when new', function(assert) {
|
2015-08-06 12:43:56 -04:00
|
|
|
const store = createStore();
|
|
|
|
const w = store.createRecord('widget', {name: 'hello'});
|
|
|
|
store.destroyRecord('widget', w).then(function(result) {
|
|
|
|
assert.ok(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test('find embedded', function(assert) {
|
2015-04-14 14:21:02 -04:00
|
|
|
const store = createStore();
|
2015-08-18 17:15:46 -04:00
|
|
|
return store.find('fruit', 2).then(function(f) {
|
|
|
|
assert.ok(f.get('farmer'), 'it has the embedded object');
|
|
|
|
|
|
|
|
const fruitCols = f.get('colors');
|
|
|
|
assert.equal(fruitCols.length, 2);
|
|
|
|
assert.equal(fruitCols[0].get('id'), 1);
|
|
|
|
assert.equal(fruitCols[1].get('id'), 2);
|
|
|
|
|
|
|
|
assert.ok(f.get('category'), 'categories are found automatically');
|
2015-04-14 14:21:02 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test('findAll embedded', function(assert) {
|
2015-04-21 13:15:40 -04:00
|
|
|
const store = createStore();
|
2015-05-01 16:35:52 -04:00
|
|
|
return store.findAll('fruit').then(function(fruits) {
|
2015-08-18 17:15:46 -04:00
|
|
|
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');
|
2015-11-30 15:22:58 -05:00
|
|
|
assert.equal(fruits.get('extras.hello'), 'world', 'it can supply extra information');
|
2015-08-18 17:15:46 -04: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);
|
|
|
|
|
|
|
|
assert.equal(fruits.objectAt(2).get('farmer.name'), 'Luke Skywalker');
|
2015-04-21 13:15:40 -04:00
|
|
|
});
|
2017-06-14 13:57:58 -04:00
|
|
|
});
|