-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from decentraland/feat/storage-migration
BREAKING CHANGE: storage migration
- Loading branch information
Showing
8 changed files
with
175 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { expect } from 'chai' | ||
import { Migrations } from './types' | ||
import { | ||
hasLocalStorage, | ||
migrateStorage, | ||
getLocalStorage | ||
} from './localStorage' | ||
declare var global: any | ||
let fakeStore = {} | ||
global.window = {} | ||
|
||
describe('localStorage', function() { | ||
const migrations: Migrations<any> = { | ||
2: (data: any) => ({ ...data, data: 'new version' }) | ||
} | ||
|
||
beforeEach(function() { | ||
fakeStore = {} | ||
global.window['localStorage'] = { | ||
getItem: (key: string) => fakeStore[key], | ||
setItem: (key: string, value: string) => (fakeStore[key] = value), | ||
removeItem: (key: string) => delete fakeStore[key] | ||
} | ||
}) | ||
|
||
describe('hasLocalStorage', function() { | ||
it('should return false if localStorage is not available', function() { | ||
delete global.window['localStorage'] | ||
expect(hasLocalStorage()).to.equal(false) | ||
}) | ||
it('should return true if localStorage is available', function() { | ||
expect(hasLocalStorage()).to.equal(true) | ||
}) | ||
}) | ||
|
||
describe('migrateStorage', function() { | ||
it('should migrate', function() { | ||
const key = 'key' | ||
const localStorage = getLocalStorage() | ||
localStorage.setItem(key, JSON.stringify('{}')) | ||
let data = JSON.parse(localStorage.getItem(key) as string) | ||
expect(data.storage).to.equal(undefined) | ||
migrateStorage(key, migrations) | ||
data = JSON.parse(localStorage.getItem(key) as string) | ||
expect(data.storage.version).to.equal(2) | ||
expect(data.data).to.equal('new version') | ||
}) | ||
|
||
it('should set corrent version', function() { | ||
const key = 'key' | ||
const localStorage = getLocalStorage() | ||
|
||
localStorage.setItem(key, JSON.stringify('{ storage: { version: null }}')) | ||
migrateStorage(key, migrations) | ||
let data = JSON.parse(localStorage.getItem(key) as string) | ||
expect(data.storage.version).to.equal(2) | ||
}) | ||
|
||
it('should not migrate if there is no migrations left', function() { | ||
const key = 'key' | ||
const localStorage = getLocalStorage() | ||
localStorage.setItem(key, JSON.stringify('{}')) | ||
let data = JSON.parse(localStorage.getItem(key) as string) | ||
expect(data.storage).to.equal(undefined) | ||
migrateStorage(key, migrations) | ||
data = JSON.parse(localStorage.getItem(key) as string) | ||
expect(data.storage.version).to.equal(2) | ||
|
||
migrateStorage(key, migrations) | ||
expect(data.storage.version).to.equal(2) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Migrations } from '../../lib/types' | ||
|
||
export interface StorageMiddleware<T> { | ||
storageKey: string | ||
paths: string[] | string[][] | ||
actions: string[] | ||
migrations: Migrations<T> | ||
} |