Skip to content

Commit

Permalink
Mock the entire localStorage API for each test using Sinon
Browse files Browse the repository at this point in the history
  • Loading branch information
VasylMarchuk committed Jun 9, 2024
1 parent d2a2030 commit 0493e09
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion tests/test-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,45 @@ import { forceModulesToBeLoaded, sendCoverage } from 'ember-cli-code-coverage/te
import { setApplication } from '@ember/test-helpers';
import { setup as setupQunitDom } from 'qunit-dom';
import { setup as setupQunitAssertionsExtra } from 'qunit-assertions-extra';
import setupSinon from 'ember-sinon-qunit';
import sinon from 'sinon';

import start from 'ember-exam/test-support/start';
// import { start } from 'ember-qunit';

setApplication(Application.create(config.APP));

setupSinon();

setupQunitDom(QUnit.assert);
setupQunitAssertionsExtra(QUnit.assert);

QUnit.testStart(function () {
window.localStorage.clear(); // We use localStorage for session tokens
const localStorageCache = new Map();

sinon.stub(window.localStorage, 'getItem').callsFake(function (key) {
return localStorageCache.get(key);
});

sinon.stub(window.localStorage, 'setItem').callsFake(function (key, value) {
localStorageCache.set(key, value);
});

sinon.stub(window.localStorage, 'removeItem').callsFake(function (key) {
localStorageCache.delete(key);
});

sinon.stub(window.localStorage, 'clear').callsFake(function () {
localStorageCache.clear();
});

sinon.stub(window.localStorage, 'key').callsFake(function (index) {
return localStorageCache.key(index);
});

sinon.stub(window.localStorage.__proto__, 'length').get(function () {
return localStorageCache.size;
});
});

QUnit.done(async function () {
Expand Down

0 comments on commit 0493e09

Please sign in to comment.