jest-localstorage-mock
Advanced tools
Comparing version 2.1.0 to 2.2.0
@@ -5,3 +5,3 @@ describe('storage', () => | ||
beforeEach(() => { | ||
storage.__STORE__ = {}; | ||
storage.clear(); | ||
jest.clearAllMocks(); | ||
@@ -8,0 +8,0 @@ }); |
'use strict'; | ||
class LocalStorage { | ||
constructor() { | ||
this.store = {}; | ||
} | ||
constructor(jest) { | ||
Object.defineProperty(this, 'getItem', { | ||
enumerable: false, | ||
value: jest.fn(key => this[key] || null) | ||
}); | ||
Object.defineProperty(this, 'setItem', { | ||
enumerable: false, | ||
// not mentioned in the spec, but we must always coerce to a string | ||
value: jest.fn((key, val = '') => { | ||
this[key] = val + ''; | ||
}) | ||
}); | ||
Object.defineProperty(this, 'removeItem', { | ||
enumerable: false, | ||
value: jest.fn(key => { | ||
delete this[key]; | ||
}) | ||
}); | ||
Object.defineProperty(this, 'clear', { | ||
enumerable: false, | ||
value: jest.fn(() => { | ||
Object.keys(this).map(key => delete this[key]); | ||
}) | ||
}); | ||
Object.defineProperty(this, 'toString', { | ||
enumerable: false, | ||
value: jest.fn(() => { | ||
return '[object Storage]'; | ||
}) | ||
}); | ||
Object.defineProperty(this, 'key', { | ||
enumerable: false, | ||
value: jest.fn(idx => Object.keys(this)[idx] || null) | ||
}); | ||
} // end constructor | ||
clear() { | ||
this.store = {}; | ||
} | ||
getItem(key) { | ||
return this.store[key] || null; | ||
} | ||
setItem(key, value = '') { | ||
// not mentioned in the spec, but we must always coerce to a string | ||
this.store[key] = value + ''; | ||
} | ||
removeItem(key) { | ||
delete this.store[key]; | ||
} | ||
key(index) { | ||
const keys = Object.keys(this.store); | ||
return keys[index] || null; | ||
} | ||
get length() { | ||
return Object.keys(this.store).length; | ||
return Object.keys(this).length; | ||
} | ||
toString() { | ||
return '[object Storage]'; | ||
// for backwards compatibility | ||
get __STORE__() { | ||
return this; | ||
} | ||
} | ||
const local = new LocalStorage(); | ||
const session = new LocalStorage(); | ||
global.localStorage = { | ||
clear: jest.fn(() => local.clear()), | ||
getItem: jest.fn(key => local.getItem(key)), | ||
setItem: jest.fn((key, value) => local.setItem(key, value)), | ||
removeItem: jest.fn(key => local.removeItem(key)), | ||
key: jest.fn(index => local.key(index)), | ||
toString: jest.fn(() => local.toString()), | ||
get __STORE__() { | ||
return local.store; | ||
}, | ||
set __STORE__(store) { | ||
local.store = store; | ||
}, | ||
get length() { | ||
return local.length; | ||
} | ||
}; | ||
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; | ||
} | ||
}; | ||
global.localStorage = new LocalStorage(jest); | ||
global.sessionStorage = new LocalStorage(jest); | ||
//# sourceMappingURL=setup.js.map |
{ | ||
"name": "jest-localstorage-mock", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Auto mock all localstorage and sessionstorage APIs for your Jest tests", | ||
@@ -59,3 +59,3 @@ "main": "dist/setup.js", | ||
"babel-core": "^6.26.0", | ||
"babel-jest": "^21.2.0", | ||
"babel-jest": "^22.0.0", | ||
"babel-plugin-external-helpers": "^6.22.0", | ||
@@ -66,5 +66,5 @@ "babel-preset-env": "^1.6.1", | ||
"eslint-plugin-prettier": "^2.3.1", | ||
"jest": "^21.2.1", | ||
"jest": "^22.0.0", | ||
"prettier": "^1.8.2", | ||
"rollup": "^0.51.0", | ||
"rollup": "^0.54.0", | ||
"rollup-plugin-babel": "^3.0.0", | ||
@@ -71,0 +71,0 @@ "rollup-plugin-node-resolve": "^3.0.0" |
@@ -155,6 +155,6 @@ Use this module with [Jest](https://facebook.github.io/jest/) to run web tests | ||
localStorage.__STORE__ = {}; | ||
// you may also reset all mocks, which could impact your other mocks | ||
// you could also reset all mocks, but this could impact your other mocks | ||
jest.resetAllMocks(); | ||
// or individually reset a mock used | ||
localStorage.setItem.mockReset(); | ||
localStorage.setItem.mockClear(); | ||
}); | ||
@@ -161,0 +161,0 @@ |
export class LocalStorage { | ||
constructor() { | ||
this.store = {}; | ||
} | ||
constructor(jest) { | ||
Object.defineProperty(this, 'getItem', { | ||
enumerable: false, | ||
value: jest.fn(key => this[key] || null), | ||
}); | ||
Object.defineProperty(this, 'setItem', { | ||
enumerable: false, | ||
// not mentioned in the spec, but we must always coerce to a string | ||
value: jest.fn((key, val = '') => { | ||
this[key] = val + ''; | ||
}), | ||
}); | ||
Object.defineProperty(this, 'removeItem', { | ||
enumerable: false, | ||
value: jest.fn(key => { | ||
delete this[key]; | ||
}), | ||
}); | ||
Object.defineProperty(this, 'clear', { | ||
enumerable: false, | ||
value: jest.fn(() => { | ||
Object.keys(this).map(key => delete this[key]); | ||
}), | ||
}); | ||
Object.defineProperty(this, 'toString', { | ||
enumerable: false, | ||
value: jest.fn(() => { | ||
return '[object Storage]'; | ||
}), | ||
}); | ||
Object.defineProperty(this, 'key', { | ||
enumerable: false, | ||
value: jest.fn(idx => Object.keys(this)[idx] || null), | ||
}); | ||
} // end constructor | ||
clear() { | ||
this.store = {}; | ||
} | ||
getItem(key) { | ||
return this.store[key] || null; | ||
} | ||
setItem(key, value = '') { | ||
// not mentioned in the spec, but we must always coerce to a string | ||
this.store[key] = value + ''; | ||
} | ||
removeItem(key) { | ||
delete this.store[key]; | ||
} | ||
key(index) { | ||
const keys = Object.keys(this.store); | ||
return keys[index] || null; | ||
} | ||
get length() { | ||
return Object.keys(this.store).length; | ||
return Object.keys(this).length; | ||
} | ||
toString() { | ||
return '[object Storage]'; | ||
// for backwards compatibility | ||
get __STORE__() { | ||
return this; | ||
} | ||
} |
import { LocalStorage } from './localstorage'; | ||
const local = new LocalStorage(); | ||
const session = new LocalStorage(); | ||
global.localStorage = { | ||
clear: jest.fn(() => local.clear()), | ||
getItem: jest.fn(key => local.getItem(key)), | ||
setItem: jest.fn((key, value) => local.setItem(key, value)), | ||
removeItem: jest.fn(key => local.removeItem(key)), | ||
key: jest.fn(index => local.key(index)), | ||
toString: jest.fn(() => local.toString()), | ||
get __STORE__() { | ||
return local.store; | ||
}, | ||
set __STORE__(store) { | ||
local.store = store; | ||
}, | ||
get length() { | ||
return local.length; | ||
}, | ||
}; | ||
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; | ||
}, | ||
}; | ||
global.localStorage = new LocalStorage(jest); | ||
global.sessionStorage = new LocalStorage(jest); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
148367
247