mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Migration to remove legacy local storage keys from localStorage (#9986)
* Migration to remove legacy local storage keys from localStorage * Update app/scripts/migrations/050.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> * Update app/scripts/migrations/050.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> * Fix unit tests for migration 50 * Fixing stubbing and localstorage reference in migration 50 * Update test/helper.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> Co-authored-by: Mark Stacey <markjstacey@gmail.com>
This commit is contained in:
parent
c515591e7b
commit
1661953e23
32
app/scripts/migrations/050.js
Normal file
32
app/scripts/migrations/050.js
Normal file
@ -0,0 +1,32 @@
|
||||
import { cloneDeep } from 'lodash'
|
||||
|
||||
const version = 50
|
||||
|
||||
const LEGACY_LOCAL_STORAGE_KEYS = [
|
||||
'METASWAP_GAS_PRICE_ESTIMATES_LAST_RETRIEVED',
|
||||
'METASWAP_GAS_PRICE_ESTIMATES',
|
||||
'cachedFetch',
|
||||
'BASIC_PRICE_ESTIMATES_LAST_RETRIEVED',
|
||||
'BASIC_PRICE_ESTIMATES',
|
||||
'BASIC_GAS_AND_TIME_API_ESTIMATES',
|
||||
'BASIC_GAS_AND_TIME_API_ESTIMATES_LAST_RETRIEVED',
|
||||
'GAS_API_ESTIMATES_LAST_RETRIEVED',
|
||||
'GAS_API_ESTIMATES',
|
||||
]
|
||||
|
||||
/**
|
||||
* Migrate metaMetrics state to the new MetaMetrics controller
|
||||
*/
|
||||
export default {
|
||||
version,
|
||||
async migrate(originalVersionedData) {
|
||||
const versionedData = cloneDeep(originalVersionedData)
|
||||
versionedData.meta.version = version
|
||||
|
||||
LEGACY_LOCAL_STORAGE_KEYS.forEach((key) =>
|
||||
window.localStorage.removeItem(key),
|
||||
)
|
||||
|
||||
return versionedData
|
||||
},
|
||||
}
|
@ -54,6 +54,7 @@ const migrations = [
|
||||
require('./047').default,
|
||||
require('./048').default,
|
||||
require('./049').default,
|
||||
require('./050').default,
|
||||
]
|
||||
|
||||
export default migrations
|
||||
|
@ -76,8 +76,9 @@ Object.assign(window, { fetch, Headers, Request, Response })
|
||||
require('abortcontroller-polyfill/dist/polyfill-patch-fetch')
|
||||
|
||||
// localStorage
|
||||
window.localStorage = {}
|
||||
|
||||
window.localStorage = {
|
||||
removeItem: () => null,
|
||||
}
|
||||
// override @metamask/logo
|
||||
window.requestAnimationFrame = () => undefined
|
||||
|
||||
|
89
test/unit/migrations/050-test.js
Normal file
89
test/unit/migrations/050-test.js
Normal file
@ -0,0 +1,89 @@
|
||||
import { strict as assert } from 'assert'
|
||||
import sinon from 'sinon'
|
||||
import migration50 from '../../../app/scripts/migrations/050'
|
||||
|
||||
const LEGACY_LOCAL_STORAGE_KEYS = [
|
||||
'METASWAP_GAS_PRICE_ESTIMATES_LAST_RETRIEVED',
|
||||
'METASWAP_GAS_PRICE_ESTIMATES',
|
||||
'cachedFetch',
|
||||
'BASIC_PRICE_ESTIMATES_LAST_RETRIEVED',
|
||||
'BASIC_PRICE_ESTIMATES',
|
||||
'BASIC_GAS_AND_TIME_API_ESTIMATES',
|
||||
'BASIC_GAS_AND_TIME_API_ESTIMATES_LAST_RETRIEVED',
|
||||
'GAS_API_ESTIMATES_LAST_RETRIEVED',
|
||||
'GAS_API_ESTIMATES',
|
||||
]
|
||||
|
||||
describe('migration #50', function () {
|
||||
let mockLocalStorageRemoveItem
|
||||
|
||||
beforeEach(function () {
|
||||
mockLocalStorageRemoveItem = sinon.stub(window.localStorage, 'removeItem')
|
||||
})
|
||||
|
||||
afterEach(function () {
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
it('should update the version metadata', async function () {
|
||||
const oldStorage = {
|
||||
meta: {
|
||||
version: 49,
|
||||
},
|
||||
data: {},
|
||||
}
|
||||
|
||||
const newStorage = await migration50.migrate(oldStorage)
|
||||
assert.deepEqual(newStorage.meta, {
|
||||
version: 50,
|
||||
})
|
||||
})
|
||||
|
||||
it('should call window.localStorage.removeItem for each legacy key', async function () {
|
||||
const oldStorage = {
|
||||
meta: {
|
||||
version: 49,
|
||||
},
|
||||
data: {},
|
||||
}
|
||||
|
||||
await migration50.migrate(oldStorage)
|
||||
assert.equal(mockLocalStorageRemoveItem.callCount, 9)
|
||||
assert.equal(
|
||||
mockLocalStorageRemoveItem.getCall(0).args[0],
|
||||
LEGACY_LOCAL_STORAGE_KEYS[0],
|
||||
)
|
||||
assert.equal(
|
||||
mockLocalStorageRemoveItem.getCall(1).args[0],
|
||||
LEGACY_LOCAL_STORAGE_KEYS[1],
|
||||
)
|
||||
assert.equal(
|
||||
mockLocalStorageRemoveItem.getCall(2).args[0],
|
||||
LEGACY_LOCAL_STORAGE_KEYS[2],
|
||||
)
|
||||
assert.equal(
|
||||
mockLocalStorageRemoveItem.getCall(3).args[0],
|
||||
LEGACY_LOCAL_STORAGE_KEYS[3],
|
||||
)
|
||||
assert.equal(
|
||||
mockLocalStorageRemoveItem.getCall(4).args[0],
|
||||
LEGACY_LOCAL_STORAGE_KEYS[4],
|
||||
)
|
||||
assert.equal(
|
||||
mockLocalStorageRemoveItem.getCall(5).args[0],
|
||||
LEGACY_LOCAL_STORAGE_KEYS[5],
|
||||
)
|
||||
assert.equal(
|
||||
mockLocalStorageRemoveItem.getCall(6).args[0],
|
||||
LEGACY_LOCAL_STORAGE_KEYS[6],
|
||||
)
|
||||
assert.equal(
|
||||
mockLocalStorageRemoveItem.getCall(7).args[0],
|
||||
LEGACY_LOCAL_STORAGE_KEYS[7],
|
||||
)
|
||||
assert.equal(
|
||||
mockLocalStorageRemoveItem.getCall(8).args[0],
|
||||
LEGACY_LOCAL_STORAGE_KEYS[8],
|
||||
)
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue
Block a user