Merge pull request #3628 from overleaf/ae-global-localstorage

Use Object.defineProperty to set global.localStorage for tests

GitOrigin-RevId: 541a253a6d19fcc93b40c74942ae8ecffb85fa60
This commit is contained in:
Miguel Serrano 2021-02-09 16:39:44 +01:00 committed by Copybot
parent ceab823447
commit b35114b81a
3 changed files with 38 additions and 21 deletions

View file

@ -26,14 +26,19 @@ moment.updateLocale('en', {
}) })
let inMemoryLocalStorage = {} let inMemoryLocalStorage = {}
global.localStorage = { Object.defineProperty(global, 'localStorage', {
value: {
// localStorage returns `null` when the item does not exist // localStorage returns `null` when the item does not exist
getItem: key => getItem: key =>
inMemoryLocalStorage[key] !== undefined ? inMemoryLocalStorage[key] : null, inMemoryLocalStorage[key] !== undefined
? inMemoryLocalStorage[key]
: null,
setItem: (key, value) => (inMemoryLocalStorage[key] = value), setItem: (key, value) => (inMemoryLocalStorage[key] = value),
clear: () => (inMemoryLocalStorage = {}), clear: () => (inMemoryLocalStorage = {}),
removeItem: key => delete inMemoryLocalStorage[key] removeItem: key => delete inMemoryLocalStorage[key]
} },
writable: true
})
// node-fetch doesn't accept relative URL's: https://github.com/node-fetch/node-fetch/blob/master/docs/v2-LIMITS.md#known-differences // node-fetch doesn't accept relative URL's: https://github.com/node-fetch/node-fetch/blob/master/docs/v2-LIMITS.md#known-differences
const fetch = require('node-fetch') const fetch = require('node-fetch')

View file

@ -15,12 +15,17 @@ describe('<OutlinePane />', function() {
renderWithEditorContext(children, { projectId: '123abc' }) renderWithEditorContext(children, { projectId: '123abc' })
} }
let originalLocalStorage
before(function() { before(function() {
global.localStorage = { originalLocalStorage = global.localStorage
Object.defineProperty(global, 'localStorage', {
value: {
getItem: sinon.stub().returns(null), getItem: sinon.stub().returns(null),
setItem: sinon.stub() setItem: sinon.stub()
} }
}) })
})
afterEach(function() { afterEach(function() {
onToggle.reset() onToggle.reset()
@ -30,7 +35,9 @@ describe('<OutlinePane />', function() {
}) })
after(function() { after(function() {
delete global.localStorage Object.defineProperty(global, 'localStorage', {
value: originalLocalStorage
})
}) })
it('renders expanded outline', function() { it('renders expanded outline', function() {

View file

@ -10,22 +10,27 @@ describe('localStorage', function() {
}) })
after(function() { after(function() {
global.localStorage = originalLocalStorage Object.defineProperty(global, 'localStorage', {
value: originalLocalStorage
})
}) })
beforeEach(function() { beforeEach(function() {
global.localStorage = { Object.defineProperty(global, 'localStorage', {
value: {
getItem: sinon.stub().returns(null), getItem: sinon.stub().returns(null),
setItem: sinon.stub(), setItem: sinon.stub(),
clear: sinon.stub(), clear: sinon.stub(),
removeItem: sinon.stub() removeItem: sinon.stub()
} }
})
global.console.error = sinon.stub() global.console.error = sinon.stub()
}) })
afterEach(function() { afterEach(function() {
global.console.error.reset() global.console.error.reset()
delete global.localStorage Object.defineProperty(global, 'localStorage', { value: undefined })
}) })
it('getItem', function() { it('getItem', function() {