-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Jsdom v11.12.0 adds localStorage and sessionStorage (#80)
* 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
1 parent
069c592
commit 5e26c0f
Showing
6 changed files
with
686 additions
and
610 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.