Skip to content

Commit

Permalink
solve sinon stubbing sessionStorage issue with dependency injection
Browse files Browse the repository at this point in the history
  • Loading branch information
snapwich committed Jun 6, 2018
1 parent d48c7c4 commit 9c5ba91
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/debugging.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ export function getConfig(debugging) {
}
config.getConfig('debugging', ({debugging}) => getConfig(debugging));

export function sessionLoader() {
export function sessionLoader(storage = window.sessionStorage) {
let overrides;
try {
overrides = JSON.parse(window.sessionStorage.getItem(OVERRIDE_KEY));
overrides = JSON.parse(storage.getItem(OVERRIDE_KEY));
} catch (e) {
}
if (overrides) {
Expand Down
17 changes: 9 additions & 8 deletions test/spec/debugging_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ describe('bid overrides', () => {
});

afterEach(() => {
window.sessionStorage.clear();
sandbox.restore();
});

describe('initialization', () => {
beforeEach(() => {
sandbox.stub(config, 'setConfig');
sandbox.stub(window.sessionStorage, 'setItem');
sandbox.stub(window.sessionStorage, 'removeItem');
});

afterEach(() => {
Expand All @@ -35,17 +34,19 @@ describe('bid overrides', () => {
});

it('should happen when configuration found in sessionStorage', () => {
sandbox.stub(window.sessionStorage, 'getItem').returns('{"enabled": true}');

sessionLoader();
sessionLoader({
getItem: () => ('{"enabled": true}')
});
expect(addBidResponse.hasHook(boundHook)).to.equal(true);
});

it('should not throw if sessionStorage is inaccessible', () => {
sandbox.stub(window.sessionStorage, 'getItem').throws();

expect(() => {
sessionLoader();
sessionLoader({
getItem() {
throw new Error('test');
}
});
}).not.to.throw();
});
});
Expand Down

0 comments on commit 9c5ba91

Please sign in to comment.