2015-03-06 12:37:24 -05:00
|
|
|
module('store:main');
|
|
|
|
|
|
|
|
import createStore from 'helpers/create-store';
|
|
|
|
|
|
|
|
test('createRecord', function() {
|
|
|
|
const store = createStore();
|
|
|
|
const widget = store.createRecord('widget', {id: 111, name: 'hello'});
|
2015-04-09 14:54:17 -04:00
|
|
|
|
|
|
|
ok(!widget.get('isNew'), 'it is not a new record');
|
2015-03-06 12:37:24 -05:00
|
|
|
equal(widget.get('name'), 'hello');
|
|
|
|
equal(widget.get('id'), 111);
|
|
|
|
});
|
|
|
|
|
2015-04-08 14:44:44 -04:00
|
|
|
test('createRecord without an `id`', function() {
|
|
|
|
const store = createStore();
|
|
|
|
const widget = store.createRecord('widget', {name: 'hello'});
|
|
|
|
|
2015-04-09 14:54:17 -04:00
|
|
|
ok(widget.get('isNew'), 'it is a new record');
|
2015-04-08 14:44:44 -04:00
|
|
|
ok(!widget.get('id'), 'there is no id');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('createRecord without attributes', function() {
|
|
|
|
const store = createStore();
|
|
|
|
const widget = store.createRecord('widget');
|
|
|
|
|
|
|
|
ok(!widget.get('id'), 'there is no id');
|
2015-04-09 14:54:17 -04:00
|
|
|
ok(widget.get('isNew'), 'it is a new record');
|
2015-04-08 14:44:44 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('createRecord with a record as attributes returns that record from the map', function() {
|
|
|
|
const store = createStore();
|
|
|
|
const widget = store.createRecord('widget', {id: 33});
|
|
|
|
const secondWidget = store.createRecord('widget', {id: 33});
|
|
|
|
|
|
|
|
equal(widget, secondWidget, 'they should be the same');
|
|
|
|
});
|
|
|
|
|
2015-03-06 12:37:24 -05:00
|
|
|
test('find', function() {
|
|
|
|
const store = createStore();
|
2015-05-01 16:35:52 -04:00
|
|
|
return store.find('widget', 123).then(function(w) {
|
2015-03-06 12:37:24 -05:00
|
|
|
equal(w.get('name'), 'Trout Lure');
|
|
|
|
equal(w.get('id'), 123);
|
2015-04-09 14:54:17 -04:00
|
|
|
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) {
|
|
|
|
equal(w, w2);
|
|
|
|
});
|
2015-03-16 15:14:33 -04:00
|
|
|
});
|
|
|
|
});
|
2015-03-06 12:37:24 -05:00
|
|
|
|
2015-03-16 15:14:33 -04:00
|
|
|
test('find with object id', function() {
|
|
|
|
const store = createStore();
|
2015-05-01 16:35:52 -04:00
|
|
|
return store.find('widget', {id: 123}).then(function(w) {
|
2015-03-16 15:14:33 -04:00
|
|
|
equal(w.get('firstObject.name'), 'Trout Lure');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('find with query param', function() {
|
|
|
|
const store = createStore();
|
2015-05-01 16:35:52 -04:00
|
|
|
return store.find('widget', {name: 'Trout Lure'}).then(function(w) {
|
2015-03-16 15:14:33 -04:00
|
|
|
equal(w.get('firstObject.id'), 123);
|
2015-03-06 12:37:24 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-03-06 12:56:32 -05:00
|
|
|
test('update', function() {
|
|
|
|
const store = createStore();
|
2015-05-01 16:35:52 -04:00
|
|
|
return store.update('widget', 123, {name: 'hello'}).then(function(result) {
|
2015-03-06 12:56:32 -05:00
|
|
|
ok(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-07-30 14:52:53 -04:00
|
|
|
test('update with a multi world name', function(assert) {
|
|
|
|
const store = createStore();
|
|
|
|
return store.update('cool-thing', 123, {name: 'hello'}).then(function(result) {
|
|
|
|
assert.ok(result);
|
|
|
|
assert.equal(result.payload.name, 'hello');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-03-06 12:37:24 -05:00
|
|
|
test('findAll', function() {
|
|
|
|
const store = createStore();
|
2015-05-01 16:35:52 -04:00
|
|
|
return store.findAll('widget').then(function(result) {
|
2015-03-16 15:14:33 -04:00
|
|
|
equal(result.get('length'), 2);
|
2015-03-06 12:37:24 -05:00
|
|
|
const w = result.findBy('id', 124);
|
2015-04-09 14:54:17 -04:00
|
|
|
ok(!w.get('isNew'), 'found records are not new');
|
2015-03-06 12:37:24 -05:00
|
|
|
equal(w.get('name'), 'Evil Repellant');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-05-01 16:35:52 -04:00
|
|
|
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
|
|
|
|
2015-08-06 12:43:56 -04:00
|
|
|
test('destroyRecord when new', function(assert) {
|
|
|
|
const store = createStore();
|
|
|
|
const w = store.createRecord('widget', {name: 'hello'});
|
|
|
|
store.destroyRecord('widget', w).then(function(result) {
|
|
|
|
assert.ok(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2015-04-14 14:21:02 -04:00
|
|
|
test('find embedded', function() {
|
|
|
|
const store = createStore();
|
2015-05-01 16:35:52 -04:00
|
|
|
return store.find('fruit', 1).then(function(f) {
|
2015-04-14 14:21:02 -04:00
|
|
|
ok(f.get('farmer'), 'it has the embedded object');
|
2015-04-21 13:15:40 -04:00
|
|
|
ok(f.get('category'), 'categories are found automatically');
|
2015-04-14 14:21:02 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-04-21 13:15:40 -04:00
|
|
|
test('findAll embedded', function() {
|
|
|
|
const store = createStore();
|
2015-05-01 16:35:52 -04:00
|
|
|
return store.findAll('fruit').then(function(fruits) {
|
2015-04-21 13:15:40 -04:00
|
|
|
equal(fruits.objectAt(0).get('farmer.name'), 'Old MacDonald');
|
|
|
|
equal(fruits.objectAt(0).get('farmer'), fruits.objectAt(1).get('farmer'), 'points at the same object');
|
|
|
|
equal(fruits.objectAt(2).get('farmer.name'), 'Luke Skywalker');
|
|
|
|
});
|
|
|
|
});
|