Skip to content

Commit

Permalink
fix: Change getItem(string[]) to return null for unknown keys instead…
Browse files Browse the repository at this point in the history
… of undefined
  • Loading branch information
Eluinhost committed Dec 8, 2015
1 parent 7c95b69 commit 4397b7d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/angular-localForage.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@
return res;
}
}).then(function() {
for (var i = 0; i < key.length; i++) {
if (angular.isUndefined(res[i])) {
res[i] = null;
}
}
deferred.resolve(res);
});
} else {
Expand Down
56 changes: 52 additions & 4 deletions tests/angular-localForage.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,54 @@ describe('Module: LocalForageModule', function() {
}, done);
});

describe('getItem for an array with unknown keys', function() {
it('should produce null values with all unknown keys', function(done) {
var interval = triggerDigests();

$localForage.getItem(['unknown key 1', 'unknown key 2']).then(function(values) {
stopDigests(interval);
expect(values).toEqual([null, null]);
done()
}, done)
});

it('should produce null value for an unknown trailing key', function(done) {
var interval = triggerDigests();

$localForage.setItem('known key', 'known value').then(function() {
$localForage.getItem(['known key', 'unknown key']).then(function(values) {
stopDigests(interval);
expect(values).toEqual(['known value', null]);
done()
}, done)
});
});

it('should produce null value for an unknown initial key', function(done) {
var interval = triggerDigests();

$localForage.setItem('known key', 'known value').then(function() {
$localForage.getItem(['unknown key', 'known key']).then(function(values) {
stopDigests(interval);
expect(values).toEqual([null, 'known value']);
done()
}, done)
});
});

it('should produce null value for a unknown middle key', function(done) {
var interval = triggerDigests();

$localForage.setItem(['known key', 'known key 2'], ['known value', 'known value 2']).then(function() {
$localForage.getItem(['known key', 'unknown key', 'known key 2']).then(function(values) {
stopDigests(interval);
expect(values).toEqual(['known value', null, 'known value 2']);
done()
}, done)
});
});
});

describe("iterate", function() {
var interval;

Expand Down Expand Up @@ -173,8 +221,8 @@ describe('Module: LocalForageModule', function() {

$localForage.getItem(['myName', 'myPassion', 'myHobbie']).then(function(data) {
stopDigests(interval);
expect(data[0]).toBeUndefined();
expect(data[1]).toBeUndefined();
expect(data[0]).toBeNull();
expect(data[1]).toBeNull();
expect(data[2]).toEqual('Open Source');
done();
}, done);
Expand Down Expand Up @@ -216,8 +264,8 @@ describe('Module: LocalForageModule', function() {

$localForage.getItem(['myName', 'myPassion', 'myHobbie']).then(function(data) {
stopDigests(interval);
expect(data[0]).toBeUndefined();
expect(data[1]).toBeUndefined();
expect(data[0]).toBeNull();
expect(data[1]).toBeNull();
expect(data[2]).toEqual('Open Source');
done();
}, done);
Expand Down

0 comments on commit 4397b7d

Please sign in to comment.