Skip to content

Commit

Permalink
Merge pull request #1835 from codecrafters-io/isolate-localstorage-in…
Browse files Browse the repository at this point in the history
…-tests

Isolate `localStorage` in tests using Sinon.js
  • Loading branch information
VasylMarchuk authored Jun 10, 2024
2 parents cc14e79 + 76ac4d3 commit 999b8e2
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 1 deletion.
140 changes: 140 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
"ember-qunit": "^8.0.2",
"ember-resize-modifier": "^0.7.1",
"ember-resolver": "^11.0.1",
"ember-sinon-qunit": "^7.4.0",
"ember-sortable": "^5.0.2",
"ember-source": "~5.5.0",
"ember-svg-jar": "^2.4.9",
Expand Down Expand Up @@ -175,6 +176,7 @@
"qunit-assertions-extra": "^1.0",
"qunit-dom": "^3.1.2",
"shiki": "^1.6.2",
"sinon": "^17.0.1",
"stylelint": "^16.6.1",
"stylelint-config-standard": "^36.0.0",
"stylelint-prettier": "^5.0.0",
Expand Down
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 999b8e2

Please sign in to comment.