Skip to content

Commit

Permalink
Jsdom v11.12.0 adds localStorage and sessionStorage (#80)
Browse files Browse the repository at this point in the history
* using --env=jsdom, can't overwrite localStorage

* add testURL to get rid of security error

* use object defineProperty on _localStorage and _sessionStorage

* only overwrite _localStorage and _sessionStorage if they exist

* build it

* Greenkeeper/rollup plugin babel 4.0.2 (#85)

* chore(package): update rollup-plugin-babel to version 4.0.2

Closes #84

* chore(package): update lockfile

https://npm.im/greenkeeper-lockfile

* Greenkeeper/eslint config prettier 3.0.1 (#87)

* chore(package): update eslint-config-prettier to version 3.0.1

Closes #82

* chore(package): update lockfile

https://npm.im/greenkeeper-lockfile

* Greenkeeper/rollup 0.65.0 (#86)

* chore(package): update rollup to version 0.65.0

Closes #79

* chore(package): update lockfile

https://npm.im/greenkeeper-lockfile

* Update rollup to the latest version 🚀 (#89)

* chore(package): update rollup to version 0.66.0

* chore(package): update lockfile yarn.lock

* using --env=jsdom, can't overwrite localStorage

* add testURL to get rid of security error

* use object defineProperty on _localStorage and _sessionStorage

* only overwrite _localStorage and _sessionStorage if they exist

* build it

* add more test coverage
  • Loading branch information
geoffdutton authored and Bryan Clark committed Oct 12, 2018
1 parent 069c592 commit 5e26c0f
Show file tree
Hide file tree
Showing 6 changed files with 686 additions and 610 deletions.
54 changes: 54 additions & 0 deletions __tests__/setup.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { LocalStorage } from '../src/localstorage';

describe('setup', () => {
const orignalImpGlobsl = {};

const setupGloabls = (restore = false) => {
[
'_localStorage',
'localStorage',
'_sessionStorage',
'sessionStorage',
].forEach(globalKey => {
if (restore) {
delete global[globalKey];
global[globalKey] = orignalImpGlobsl[globalKey];
} else {
orignalImpGlobsl[globalKey] = global[globalKey];
delete global[globalKey];
}
});
};

const restoreGlobals = () => setupGloabls(true);

beforeEach(() => {
setupGloabls();
jest.resetModuleRegistry();
});

afterEach(() => {
restoreGlobals();
});

['_localStorage', '_sessionStorage'].forEach(gKey => {
it(`[${gKey}] should define a property on the global object with writable false`, () => {
require('../src/setup');
expect(global[gKey.replace('_', '')].constructor.name).toBe(
'LocalStorage'
);
});

it(`[${gKey}] should define a property on the global object with writable false`, () => {
global[gKey] = true;
require('../src/setup');
let e;
try {
global[`_${gKey.replace('_', '')}`] = 'blah';
} catch (_e) {
e = _e;
}
expect(e).toBeDefined();
});
});
});
17 changes: 15 additions & 2 deletions dist/setup.js

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

2 changes: 1 addition & 1 deletion dist/setup.js.map

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

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,23 @@
"test": "./__tests__"
},
"scripts": {
"test": "jest",
"test": "yarn test:node && yarn test:jsdom",
"test:node": "jest --env=node",
"test:jsdom": "jest --env=jsdom",
"build": "rollup -c",
"prettier": "prettier --single-quote --trailing-comma es5 --write \"{src,__{tests,setups}__}/**/*.js\" README.md CONTRIBUTING.md *.json rollup.config.js",
"version": "yarn run build"
},
"dependencies": {},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-jest": "^23.0.1",
"babel-jest": "^23.4.2",
"babel-plugin-external-helpers": "^6.22.0",
"babel-preset-env": "^1.6.1",
"eslint": "^4.10.0",
"eslint-config-prettier": "^3.0.1",
"eslint-plugin-prettier": "^2.3.1",
"jest": "^23.0.1",
"jest": "^23.4.2",
"prettier": "^1.8.2",
"rollup": "^0.66.0",
"rollup-plugin-babel": "^4.0.2",
Expand All @@ -72,6 +74,7 @@
"node": ">=6.11.3"
},
"jest": {
"testURL": "http://localhost/",
"bail": true,
"coverageDirectory": "./coverage/",
"collectCoverage": true,
Expand Down
21 changes: 18 additions & 3 deletions src/setup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
import { LocalStorage } from './localstorage';
import { LocalStorage } from "./localstorage";

global.localStorage = new LocalStorage(jest);
global.sessionStorage = new LocalStorage(jest);
if (typeof global._localStorage !== "undefined") {
Object.defineProperty(global, "_localStorage", {
value: new LocalStorage(jest),
writable: false
});
} else {
global.localStorage = new LocalStorage(jest);
}

if (typeof global._sessionStorage !== "undefined") {
Object.defineProperty(global, "_sessionStorage", {
value: new LocalStorage(jest),
writable: false
});
} else {
global.sessionStorage = new LocalStorage(jest);
}
Loading

0 comments on commit 5e26c0f

Please sign in to comment.