Skip to content

Commit

Permalink
Merge pull request #66 from rpflorence/rejects_promises_properly
Browse files Browse the repository at this point in the history
Rejects promises properly
  • Loading branch information
kurko committed Jun 27, 2014
2 parents fe6a3e4 + 2456d77 commit 0d1a2a1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# ember-localstorage-adapter

## Master

### 0.3.2 (June 27, 2014)

* `#find` rejects its promise if no record is found.
* `#findQuery` rejects its promise if no records are found.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ember-localstorage-adapter",
"version": "0.3.1",
"version": "0.3.2",
"homepage": "https://github.com/rpflorence/ember-localstorage-adapter",
"authors": [
"Ryan Florence <[email protected]>"
Expand Down
21 changes: 11 additions & 10 deletions localstorage_adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,20 @@
return new Ember.RSVP.Promise(function(resolve, reject) {
var record = Ember.A(namespace.records[id]);

if (allowRecursive && record) {
if (!record || !record.hasOwnProperty('id')) {
store.dematerializeRecord(store.typeMapFor(type).idToRecord[id]);
reject();
return;
}

if (allowRecursive) {
adapter.loadRelationships(type, record).then(function(finalRecord) {
resolve(finalRecord);
});
} else {
if (!record) {
reject();
} else {
resolve(record);
}
resolve(record);
}
});

resolve(record);
},

findMany: function (store, type, ids) {
Expand Down Expand Up @@ -180,9 +180,10 @@

if (results.get('length')) {
results = this.loadRelationshipsForMany(type, results);
return Ember.RSVP.resolve(results);
} else {
return Ember.RSVP.reject();
}

return Ember.RSVP.resolve(results);
},

query: function (records, query) {
Expand Down
22 changes: 18 additions & 4 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ test('find with id', function() {
});
});

test('#find - rejects promise when non-existing record', function () {
expect(2);

stop();
store.find("list", "unknown").catch(function () {
ok(true);
equal(store.hasRecordForId("list", "unknown"), false);
start();
});
});

test('findQuery', function() {

stop();
Expand All @@ -89,10 +100,13 @@ test('findQuery', function() {
equal(get(records, 'length'), 1, 'found results for {b: true}');
start();
});
});

test('#findQuery - rejects promise when there are no records', function() {
stop();
store.findQuery('list', {whatever: "dude"}).then(function(records) {
equal(get(records, 'length'), 0, 'didn\'t find results for nonsense');
store.findQuery('list', {name: /unknown/}).catch(function() {
ok(true);
equal(store.hasRecordForId("list", "unknown"), false);
start();
});
});
Expand Down Expand Up @@ -207,8 +221,8 @@ test('deleteRecord', function() {
expect(2);
stop();
var AssertListIsDeleted = function() {
return store.findQuery('list', { name: 'one' }).then(function(records) {
equal(get(records, 'length'), 0, "No record was found");
return store.findQuery('list', { name: 'one' }).catch(function() {
ok(true, "List was deleted");
start();
});
}
Expand Down

0 comments on commit 0d1a2a1

Please sign in to comment.