Skip to content

Commit

Permalink
Adds tests to cover handling of background changes to window.localSto…
Browse files Browse the repository at this point in the history
…rage.
  • Loading branch information
kbaltrinic committed Sep 25, 2014
1 parent 59c7993 commit dc65402
Showing 1 changed file with 64 additions and 1 deletion.
65 changes: 64 additions & 1 deletion test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ describe('ngStorage', function() {

function initStorage(initialValues) {

$window = {}
$window = {
eventHandlers: {},
addEventListener: function(event, handler) {
this.eventHandlers[event] = handler;
}
};

$window[storageType] = {
length: Object.keys(initialValues).length,
data: initialValues,
Expand Down Expand Up @@ -237,8 +243,65 @@ describe('ngStorage', function() {

});
});

if (storageType == 'localStorage') {

describe('when an ngStorage- value in window.localStorage is updated', function() {

beforeEach(function() {

initStorage({'ngStorage-existing': '"update me"'});

var updateEvent = {
key: 'ngStorage-existing',
newValue: '"updated"'
};
$window.eventHandlers.storage(updateEvent);
});

it('should reflect the update', function() {
expect($storage.existing).to.equal('updated');
});
});

describe('when an ngStorage- value in window.localStorage is added', function() {

beforeEach(function() {

initStorage({});

var updateEvent = {
key: 'ngStorage-value',
newValue: '"new"'
};
$window.eventHandlers.storage(updateEvent);
});

it('should reflect the addition', function() {
expect($storage.value).to.equal('new');
});
});

describe('when an ngStorage- value in window.localStorage is deleted', function() {

beforeEach(function() {

initStorage({'ngStorage-existing': '"delete me"'});

var updateEvent = {
key: 'ngStorage-existing',
};
$window.eventHandlers.storage(updateEvent);
});

it('should reflect the deletion', function() {
expect($storage.existing).to.be.undefined;
});
});
}
});
}

});


0 comments on commit dc65402

Please sign in to comment.