Skip to content
Permalink
Browse files

feat(upgrade-settings): add utility to upgrade settings

  • Loading branch information...
devinus committed Jan 28, 2019
1 parent 19d70ab commit e3bb9f52e94a8ecddac9c2c85c606a6e75cc0770
Showing with 80 additions and 0 deletions.
  1. +44 −0 app/utils/upgrade-settings.js
  2. +36 −0 tests/unit/utils/upgrade-settings-test.js
@@ -0,0 +1,44 @@
import { Promise } from 'rsvp';
import { run, next } from '@ember/runloop';
import { setProperties } from '@ember/object';

import window from 'ember-window-mock';

const { localStorage } = window;

export const CURRENT_VERSION = 1;

export default async function upgradeSettings(settings) {
const entries = Object.entries(localStorage).map(([key, data]) => {
const value = JSON.parse(data, (k, v) => {
if (typeof v === 'object') {
delete v.seed; // eslint-disable-line no-param-reassign
delete v.acceptedTerms; // eslint-disable-line no-param-reassign
}

return v;
});

return [key, value];
});

try {
await new Promise((resolve) => {
run(localStorage, 'clear', []);
next(() => resolve());
});
} finally {
await new Promise((resolve) => {
run(() => {
entries.forEach(([key, value]) => {
localStorage.setItem(key, JSON.stringify(value));
});
});

next(() => resolve());
});
}

const version = CURRENT_VERSION;
return setProperties(settings, { version });
}
@@ -0,0 +1,36 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import window, { reset } from 'ember-window-mock';

import upgradeSettings from '@nano-wallet-company/nano-wallet-desktop/utils/upgrade-settings';

const { localStorage } = window;

describe('Unit | Utility | upgrade-settings', () => {
beforeEach(() => reset());

it('upgrades from v0 to v1', async () => {
const settings = await upgradeSettings({ version: 0 });
expect(settings).to.be.an('object');
expect(settings.version).to.equal(1);
});

it('overwrites localStorage with upgraded settings', async () => {
localStorage.setItem('wallet', JSON.stringify({
key: 'somevalue',
seed: 'someseed',
subkey: {
seed: 'someseed',
},
}));

await upgradeSettings({ version: 0 });

const wallet = JSON.parse(localStorage.getItem('wallet'));
expect(wallet).to.be.an('object');
expect(wallet.key).to.equal('somevalue');
expect(wallet.seed).to.not.exist;
expect(wallet.subkey.seed).to.not.exist;
});
});

0 comments on commit e3bb9f5

Please sign in to comment.
You can’t perform that action at this time.