Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make setup function callable to reset mocks between tests #107

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion __setups__/localstorage.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require('../src/setup');
require('../src/index');
22 changes: 18 additions & 4 deletions __tests__/setup.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { LocalStorage } from '../src/localstorage';

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

Expand Down Expand Up @@ -33,15 +31,15 @@ describe('setup', () => {

['_localStorage', '_sessionStorage'].forEach(gKey => {
it(`[${gKey}] should define a property on the global object with writable false`, () => {
require('../src/setup');
require('../src/index');
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');
require('../src/index');
let e;
try {
global[`_${gKey.replace('_', '')}`] = 'blah';
Expand All @@ -51,4 +49,20 @@ describe('setup', () => {
expect(e).toBeDefined();
});
});

it('setup should be callable to manually restore mocks', () => {
const { setup } = require('../src/setup');
const KEY = 'foo';
const VALUE = 'bar';

setup();
localStorage.setItem(KEY, VALUE);
expect(localStorage.getItem(KEY)).toBe(VALUE);
jest.resetAllMocks();
localStorage.setItem(KEY, VALUE);
expect(localStorage.getItem(KEY)).toBe(undefined);
setup();
localStorage.setItem(KEY, VALUE);
expect(localStorage.getItem(KEY)).toBe(VALUE);
});
});
73 changes: 73 additions & 0 deletions dist/index.js

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

1 change: 1 addition & 0 deletions dist/index.js.map

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

38 changes: 22 additions & 16 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.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "jest-localstorage-mock",
"version": "2.4.0",
"description": "Auto mock all localstorage and sessionstorage APIs for your Jest tests",
"main": "dist/setup.js",
"module": "src/setup.js",
"main": "dist/index.js",
"module": "src/index.js",
"author": "Bryan Clark <[email protected]> (https://twitter.com/clarkbw)",
"maintainers": [
{
Expand Down
45 changes: 31 additions & 14 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
import resolve from 'rollup-plugin-node-resolve';
import babel from 'rollup-plugin-babel';

export default {
input: 'src/setup.js',
plugins: [
resolve(),
babel({
exclude: 'node_modules/**',
babelrc: false,
}),
],
output: {
file: 'dist/setup.js',
format: 'cjs',
sourcemap: true,
export default [
{
input: 'src/index.js',
plugins: [
resolve(),
babel({
exclude: 'node_modules/**',
babelrc: false,
}),
],
output: {
file: 'dist/index.js',
format: 'cjs',
sourcemap: true,
},
},
};
{
input: 'src/setup.js',
plugins: [
resolve(),
babel({
exclude: 'node_modules/**',
babelrc: false,
}),
],
output: {
file: 'dist/setup.js',
format: 'cjs',
sourcemap: true,
},
},
];
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { setup } from './setup';
setup();
34 changes: 18 additions & 16 deletions src/setup.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { LocalStorage } from './localstorage';

if (typeof global._localStorage !== 'undefined') {
Object.defineProperty(global, '_localStorage', {
value: new LocalStorage(jest),
writable: false,
});
} else {
global.localStorage = new LocalStorage(jest);
}
export const setup = () => {
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);
}
if (typeof global._sessionStorage !== 'undefined') {
Object.defineProperty(global, '_sessionStorage', {
value: new LocalStorage(jest),
writable: false,
});
} else {
global.sessionStorage = new LocalStorage(jest);
}
};