Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jest-localstorage-mock

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-localstorage-mock - npm Package Compare versions

Comparing version 1.2.0 to 2.0.0

.circleci/config.yml

217

__tests__/index.js

@@ -1,117 +0,126 @@

// https://html.spec.whatwg.org/multipage/webstorage.html#storage
beforeEach(() => {
localStorage.__STORE__ = {};
jest.clearAllMocks();
});
describe('storage', () =>
[localStorage, sessionStorage].map(storage => {
// https://html.spec.whatwg.org/multipage/webstorage.html#storage
beforeEach(() => {
storage.__STORE__ = {};
jest.clearAllMocks();
});
// clear
// https://html.spec.whatwg.org/multipage/webstorage.html#dom-storage-clear
test('localstorage.clear', () => {
const KEY = 'foo', VALUE = 'bar';
localStorage.setItem(KEY, VALUE);
expect(localStorage.setItem).toHaveBeenLastCalledWith(KEY, VALUE);
expect(localStorage.__STORE__[KEY]).toBe(VALUE);
expect(Object.keys(localStorage.__STORE__).length).toBe(1);
localStorage.clear();
expect(localStorage.clear).toHaveBeenCalledTimes(1);
expect(Object.keys(localStorage.__STORE__).length).toBe(0);
expect(localStorage.__STORE__[KEY]).toBeUndefined();
localStorage.setItem(KEY, VALUE);
expect(localStorage.setItem).toHaveBeenLastCalledWith(KEY, VALUE);
localStorage.clear();
expect(localStorage.clear).toHaveBeenCalledTimes(2);
expect(Object.keys(localStorage.__STORE__).length).toBe(0);
expect(localStorage.__STORE__[KEY]).toBeUndefined();
});
// clear
// https://html.spec.whatwg.org/multipage/webstorage.html#dom-storage-clear
test('storage.clear', () => {
const KEY = 'foo',
VALUE = 'bar';
storage.setItem(KEY, VALUE);
expect(storage.setItem).toHaveBeenLastCalledWith(KEY, VALUE);
expect(storage.__STORE__[KEY]).toBe(VALUE);
expect(Object.keys(storage.__STORE__).length).toBe(1);
storage.clear();
expect(storage.clear).toHaveBeenCalledTimes(1);
expect(Object.keys(storage.__STORE__).length).toBe(0);
expect(storage.__STORE__[KEY]).toBeUndefined();
storage.setItem(KEY, VALUE);
expect(storage.setItem).toHaveBeenLastCalledWith(KEY, VALUE);
storage.clear();
expect(storage.clear).toHaveBeenCalledTimes(2);
expect(Object.keys(storage.__STORE__).length).toBe(0);
expect(storage.__STORE__[KEY]).toBeUndefined();
});
// setItem
// https://html.spec.whatwg.org/multipage/webstorage.html#dom-storage-setitem
test('localstorage.setItem', () => {
const KEY = 'foo', VALUE1 = 'bar', VALUE2 = 'baz';
localStorage.setItem(KEY, VALUE1);
expect(localStorage.setItem).toHaveBeenLastCalledWith(KEY, VALUE1);
expect(localStorage.__STORE__[KEY]).toBe(VALUE1);
localStorage.setItem(KEY, VALUE2);
expect(localStorage.setItem).toHaveBeenLastCalledWith(KEY, VALUE2);
expect(localStorage.__STORE__[KEY]).toBe(VALUE2);
});
// setItem
// https://html.spec.whatwg.org/multipage/webstorage.html#dom-storage-setitem
test('storage.setItem', () => {
const KEY = 'foo',
VALUE1 = 'bar',
VALUE2 = 'baz';
storage.setItem(KEY, VALUE1);
expect(storage.setItem).toHaveBeenLastCalledWith(KEY, VALUE1);
expect(storage.__STORE__[KEY]).toBe(VALUE1);
storage.setItem(KEY, VALUE2);
expect(storage.setItem).toHaveBeenLastCalledWith(KEY, VALUE2);
expect(storage.__STORE__[KEY]).toBe(VALUE2);
});
// getItem
// https://html.spec.whatwg.org/multipage/webstorage.html#dom-storage-getitem
test('localstorage.getItem', () => {
const KEY = 'foo',
VALUE1 = 'bar',
VALUE2 = 'baz',
DOES_NOT_EXIST = 'does not exist';
// getItem
// https://html.spec.whatwg.org/multipage/webstorage.html#dom-storage-getitem
test('storage.getItem', () => {
const KEY = 'foo',
VALUE1 = 'bar',
VALUE2 = 'baz',
DOES_NOT_EXIST = 'does not exist';
localStorage.setItem(KEY, VALUE1);
expect(localStorage.getItem(KEY)).toBe(VALUE1);
expect(localStorage.getItem).toHaveBeenLastCalledWith(KEY);
storage.setItem(KEY, VALUE1);
expect(storage.getItem(KEY)).toBe(VALUE1);
expect(storage.getItem).toHaveBeenLastCalledWith(KEY);
localStorage.setItem(KEY, VALUE2);
expect(localStorage.getItem(KEY)).toBe(VALUE2);
expect(localStorage.getItem).toHaveBeenLastCalledWith(KEY);
storage.setItem(KEY, VALUE2);
expect(storage.getItem(KEY)).toBe(VALUE2);
expect(storage.getItem).toHaveBeenLastCalledWith(KEY);
expect(localStorage.getItem(DOES_NOT_EXIST)).toBeNull();
expect(localStorage.getItem).toHaveBeenLastCalledWith(DOES_NOT_EXIST);
});
expect(storage.getItem(DOES_NOT_EXIST)).toBeNull();
expect(storage.getItem).toHaveBeenLastCalledWith(DOES_NOT_EXIST);
});
// removeItem
// https://html.spec.whatwg.org/multipage/webstorage.html#dom-storage-removeitem
test('localstorage.removeItem', () => {
const KEY = 'foo',
VALUE1 = 'bar',
VALUE2 = 'baz',
DOES_NOT_EXIST = 'does not exist';
// removeItem
// https://html.spec.whatwg.org/multipage/webstorage.html#dom-storage-removeitem
test('storage.removeItem', () => {
const KEY = 'foo',
VALUE1 = 'bar',
VALUE2 = 'baz',
DOES_NOT_EXIST = 'does not exist';
localStorage.setItem(KEY, VALUE1);
expect(localStorage.getItem(KEY)).toBe(VALUE1);
localStorage.removeItem(KEY);
expect(localStorage.removeItem).toHaveBeenLastCalledWith(KEY);
storage.setItem(KEY, VALUE1);
expect(storage.getItem(KEY)).toBe(VALUE1);
storage.removeItem(KEY);
expect(storage.removeItem).toHaveBeenLastCalledWith(KEY);
expect(localStorage.getItem(KEY)).toBeNull();
localStorage.setItem(KEY, VALUE2);
localStorage.removeItem(KEY);
expect(localStorage.removeItem).toHaveBeenLastCalledWith(KEY);
expect(storage.getItem(KEY)).toBeNull();
storage.setItem(KEY, VALUE2);
storage.removeItem(KEY);
expect(storage.removeItem).toHaveBeenLastCalledWith(KEY);
expect(localStorage.getItem(KEY)).toBeNull();
localStorage.removeItem(DOES_NOT_EXIST); // does not throw
expect(localStorage.removeItem).toHaveBeenLastCalledWith(DOES_NOT_EXIST);
});
expect(storage.getItem(KEY)).toBeNull();
storage.removeItem(DOES_NOT_EXIST); // does not throw
expect(storage.removeItem).toHaveBeenLastCalledWith(DOES_NOT_EXIST);
});
// length
// https://html.spec.whatwg.org/multipage/webstorage.html#dom-storage-length
// length is not mocked
test('localstorage set and remove', () => {
const KEY1 = 'foo', VALUE = 'bar', KEY2 = 'baz';
expect(localStorage.length).toBe(0);
localStorage.setItem(KEY1, VALUE);
expect(localStorage.setItem).toHaveBeenLastCalledWith(KEY1, VALUE);
expect(localStorage.setItem).toHaveBeenCalledTimes(1);
expect(localStorage.length).toBe(1);
localStorage.setItem(KEY2, VALUE);
expect(localStorage.setItem).toHaveBeenLastCalledWith(KEY2, VALUE);
expect(localStorage.setItem).toHaveBeenCalledTimes(2);
expect(localStorage.length).toBe(2);
localStorage.clear();
expect(localStorage.clear).toHaveBeenCalledTimes(1);
expect(localStorage.length).toBe(0);
});
// length
// https://html.spec.whatwg.org/multipage/webstorage.html#dom-storage-length
// length is not mocked
test('storage set and remove', () => {
const KEY1 = 'foo',
VALUE = 'bar',
KEY2 = 'baz';
expect(storage.length).toBe(0);
storage.setItem(KEY1, VALUE);
expect(storage.setItem).toHaveBeenLastCalledWith(KEY1, VALUE);
expect(storage.setItem).toHaveBeenCalledTimes(1);
expect(storage.length).toBe(1);
storage.setItem(KEY2, VALUE);
expect(storage.setItem).toHaveBeenLastCalledWith(KEY2, VALUE);
expect(storage.setItem).toHaveBeenCalledTimes(2);
expect(storage.length).toBe(2);
storage.clear();
expect(storage.clear).toHaveBeenCalledTimes(1);
expect(storage.length).toBe(0);
});
// key
// https://html.spec.whatwg.org/multipage/webstorage.html#dom-storage-key
test('localstorage.key', () => {
const KEY = 'foo', VALUE = 'bar';
localStorage.setItem(KEY, VALUE);
expect(localStorage.getItem(KEY)).toBe(VALUE);
expect(localStorage.key(0)).toBe(KEY);
expect(localStorage.key).toHaveBeenLastCalledWith(0);
expect(localStorage.length).toBe(1);
expect(localStorage.key(1)).toBeNull();
expect(localStorage.key).toHaveBeenLastCalledWith(1);
});
// key
// https://html.spec.whatwg.org/multipage/webstorage.html#dom-storage-key
test('storage.key', () => {
const KEY = 'foo',
VALUE = 'bar';
storage.setItem(KEY, VALUE);
expect(storage.getItem(KEY)).toBe(VALUE);
expect(storage.key(0)).toBe(KEY);
expect(storage.key).toHaveBeenLastCalledWith(0);
expect(storage.length).toBe(1);
expect(storage.key(1)).toBeNull();
expect(storage.key).toHaveBeenLastCalledWith(1);
});
test('localstorage.toString', () => {
expect(localStorage.toString()).toEqual('[object Storage]');
expect(localStorage.toString).toHaveBeenCalledTimes(1);
});
test('storage.toString', () => {
expect(storage.toString()).toEqual('[object Storage]');
expect(storage.toString).toHaveBeenCalledTimes(1);
});
}));
'use strict';
var asyncGenerator = function () {
function AwaitValue(value) {
this.value = value;
}
function AsyncGenerator(gen) {
var front, back;
function send(key, arg) {
return new Promise(function (resolve, reject) {
var request = {
key: key,
arg: arg,
resolve: resolve,
reject: reject,
next: null
};
if (back) {
back = back.next = request;
} else {
front = back = request;
resume(key, arg);
}
});
}
function resume(key, arg) {
try {
var result = gen[key](arg);
var value = result.value;
if (value instanceof AwaitValue) {
Promise.resolve(value.value).then(function (arg) {
resume("next", arg);
}, function (arg) {
resume("throw", arg);
});
} else {
settle(result.done ? "return" : "normal", result.value);
}
} catch (err) {
settle("throw", err);
}
}
function settle(type, value) {
switch (type) {
case "return":
front.resolve({
value: value,
done: true
});
break;
case "throw":
front.reject(value);
break;
default:
front.resolve({
value: value,
done: false
});
break;
}
front = front.next;
if (front) {
resume(front.key, front.arg);
} else {
back = null;
}
}
this._invoke = send;
if (typeof gen.return !== "function") {
this.return = undefined;
}
}
if (typeof Symbol === "function" && Symbol.asyncIterator) {
AsyncGenerator.prototype[Symbol.asyncIterator] = function () {
return this;
};
}
AsyncGenerator.prototype.next = function (arg) {
return this._invoke("next", arg);
};
AsyncGenerator.prototype.throw = function (arg) {
return this._invoke("throw", arg);
};
AsyncGenerator.prototype.return = function (arg) {
return this._invoke("return", arg);
};
return {
wrap: function (fn) {
return function () {
return new AsyncGenerator(fn.apply(this, arguments));
};
},
await: function (value) {
return new AwaitValue(value);
}
};
}();
var classCallCheck = function (instance, Constructor) {

@@ -75,2 +192,3 @@ if (!(instance instanceof Constructor)) {

var local = new LocalStorage();
var session = new LocalStorage();

@@ -106,1 +224,32 @@ global.localStorage = {

};
global.sessionStorage = {
clear: jest.fn(function () {
return session.clear();
}),
getItem: jest.fn(function (key) {
return session.getItem(key);
}),
setItem: jest.fn(function (key, value) {
return session.setItem(key, value);
}),
removeItem: jest.fn(function (key) {
return session.removeItem(key);
}),
key: jest.fn(function (index) {
return session.key(index);
}),
toString: jest.fn(function () {
return session.toString();
}),
get __STORE__() {
return session.store;
},
set __STORE__(store) {
session.store = store;
},
get length() {
return session.length;
}
};
//# sourceMappingURL=setup.js.map
{
"name": "jest-localstorage-mock",
"version": "1.2.0",
"version": "2.0.0",
"description": "Mock localstorage in your Jest tests",

@@ -8,2 +8,6 @@ "main": "dist/setup.js",

"author": "Bryan Clark <clarkbw@gmail.com> (https://twitter.com/clarkbw)",
"repository": {
"type": "git",
"url": "https://github.com/clarkbw/jest-localstorage-mock"
},
"license": "ISC",

@@ -18,12 +22,12 @@ "scripts": {

"devDependencies": {
"babel-jest": "^20.0.0",
"babel-jest": "^21.0.0",
"babel-plugin-external-helpers": "^6.22.0",
"babel-preset-env": "^1.4.0",
"eslint": "^3.19.0",
"eslint": "^4.1.0",
"eslint-config-prettier": "^2.0.0",
"eslint-plugin-prettier": "^2.0.1",
"jest": "^20.0.0",
"jest": "^21.0.0",
"prettier": "^1.3.1",
"rollup": "^0.42.0",
"rollup-plugin-babel": "^2.7.1",
"rollup": "^0.50.0",
"rollup-plugin-babel": "^3.0.0",
"rollup-plugin-node-resolve": "^3.0.0"

@@ -30,0 +34,0 @@ },

Use this module with [Jest](https://facebook.github.io/jest/) to run web tests that rely on `localstorage` where you want a working localStorage like API and mocked localStorage functions.
Use this module with [Jest](https://facebook.github.io/jest/) to run web tests that rely on `localstorage` and / or `sessionStorage` where you want a working localStorage API with mocked functions.

@@ -78,5 +78,5 @@ This module has no runtime dependencies so your project won't pull in additional module dependencies by using this.

By including this in your Jest setup you'll allow tests that expect a `localStorage` object to continue to run. The module can also allow you to use the mocks provided to check that your localStorage is being used as expected.
By including this in your Jest setup you'll allow tests that expect a `localStorage` and `sessionStorage` object to continue to run. The module can also allow you to use the mocks provided to check that your localStorage is being used as expected.
The `__STORE__` attribute of `localStorage.__STORE__` is made available for you to directly access the localStorage object if needed.
The `__STORE__` attribute of `localStorage.__STORE__` or `sessionStorage.__STORE__` is made available for you to directly access the storage object if needed.

@@ -97,14 +97,14 @@ ### Test Examples

Check that your storage is empty.
Check that your `sessionStorage` is empty, examples work with either `localStorage` or `sessionStorage`.
```js
test('should have cleared the localStorage', () => {
test('should have cleared the sessionStorage', () => {
dispatch(action.reset());
expect(localStorage.clear).toHaveBeenCalledTimes(1);
expect(localStorage.__STORE__).toEqual({}); // check store values
expect(localStorage.length).toBe(0); // or check length
expect(sessionStorage.clear).toHaveBeenCalledTimes(1);
expect(sessionStorage.__STORE__).toEqual({}); // check store values
expect(sessionStorage.length).toBe(0); // or check length
});
```
Check that localStorage calls were not made when they shouldn't have been.
Check that `localStorage` calls were not made when they shouldn't have been.

@@ -111,0 +111,0 @@ ```js

@@ -5,4 +5,3 @@ import resolve from 'rollup-plugin-node-resolve';

export default {
entry: 'src/setup.js',
format: 'cjs',
input: 'src/setup.js',
plugins: [

@@ -17,3 +16,7 @@ resolve(),

],
dest: 'dist/setup.js',
output: {
file: 'dist/setup.js',
format: 'cjs',
sourcemap: true,
},
};
import { LocalStorage } from './localstorage';
const local = new LocalStorage();
const session = new LocalStorage();

@@ -22,1 +23,19 @@ global.localStorage = {

};
global.sessionStorage = {
clear: jest.fn(() => session.clear()),
getItem: jest.fn(key => session.getItem(key)),
setItem: jest.fn((key, value) => session.setItem(key, value)),
removeItem: jest.fn(key => session.removeItem(key)),
key: jest.fn(index => session.key(index)),
toString: jest.fn(() => session.toString()),
get __STORE__() {
return session.store;
},
set __STORE__(store) {
session.store = store;
},
get length() {
return session.length;
},
};

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc