Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
Add unit tests for savedAccounts utils
Browse files Browse the repository at this point in the history
  • Loading branch information
slaweet committed Nov 10, 2017
1 parent d30aa0c commit 7099e47
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/utils/savedAccounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ export const setSavedAccount = ({ publicKey, network, address }) => {
return savedAccounts;
};

export const removeSavedAccount = ({ network, publicKey }) => {
export const removeSavedAccount = ({ publicKey, network, address }) => {
const accounts = getSavedAccounts().filter(account =>
!(account.publicKey === publicKey && account.network === network));
!(account.publicKey === publicKey &&
account.network === network &&
account.address === address));
localStorage.setItem('accounts', JSON.stringify(accounts));
return accounts;
};
111 changes: 111 additions & 0 deletions src/utils/savedAccounts.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { expect } from 'chai';
import { mock } from 'sinon';
import {
getSavedAccounts,
getLastActiveAccount,
setLastActiveAccount,
setSavedAccount,
removeSavedAccount,
} from './savedAccounts';

describe('savedAccounts', () => {
let localStorageMock;
const publicKey = 'fab9d261ea050b9e326d7e11587eccc343a20e64e29d8781b50fd06683cacc88';
const accounts = [
{
publicKey: 'hab9d261ea050b9e326d7e11587eccc343a20e64e29d8781b50fd06683cacc88',
network: 0,
},
{
publicKey,
network: 0,
},
{
publicKey,
network: 2,
address: 'http://localhost:4000',
},
];

beforeEach(() => {
localStorageMock = mock(localStorage);
});

afterEach(() => {
localStorageMock.restore();
});

describe('getSavedAccounts', () => {
it('returns [] if if localStorage.getItem(\'accounts\') returns undefined', () => {
localStorageMock.expects('getItem').withExactArgs('accounts').returns(undefined);
expect(getSavedAccounts()).to.deep.equal([]);
});

it('returns [] if if localStorage.getItem(\'accounts\') returns invalid JSON string', () => {
localStorageMock.expects('getItem').withExactArgs('accounts').returns('{]');
expect(getSavedAccounts()).to.deep.equal([]);
});


it('returns [] if if localStorage.getItem(\'accounts\') returns JSON encoded array with invalid data', () => {
const invalidAccounts = [
{
publicKey: 'invalid',
},
];
localStorageMock.expects('getItem').withExactArgs('accounts').returns(JSON.stringify(invalidAccounts));
expect(getSavedAccounts()).to.deep.equal([]);
});

it('returns array parsed from json in localStorage.getItem(\'accounts\')', () => {
localStorageMock.expects('getItem').withExactArgs('accounts').returns(JSON.stringify(accounts));
expect(getSavedAccounts()).to.deep.equal(accounts);
});
});

describe('getLastActiveAccount', () => {
it('returns first account if localStorage.getItem(\'lastActiveAccountIndex\') returns undefined', () => {
localStorageMock.expects('getItem').withExactArgs('accounts').returns(JSON.stringify(accounts));
localStorageMock.expects('getItem').withExactArgs('lastActiveAccountIndex').returns(undefined);
expect(getLastActiveAccount()).to.deep.equal(accounts[0]);
});

it('returns nth account if localStorage.getItem(\'lastActiveAccountIndex\') returns n', () => {
const n = 2;
localStorageMock.expects('getItem').withExactArgs('accounts').returns(JSON.stringify(accounts));
localStorageMock.expects('getItem').withExactArgs('lastActiveAccountIndex').returns(n);
expect(getLastActiveAccount()).to.deep.equal(accounts[n]);
});
});

describe('setLastActiveAccount', () => {
it('sets nothing in localStorage if passed account is not in localStorageMock.accounts and returns -1', () => {
localStorageMock.expects('getItem').withExactArgs('accounts').returns(JSON.stringify(accounts.slice(0, 1)));
expect(setLastActiveAccount(accounts[2])).to.equal(-1);
});

it('sets index of passed account in localStorage.acocunts into localStorage and returns n', () => {
const n = 2;
localStorageMock.expects('getItem').withExactArgs('accounts').returns(JSON.stringify(accounts));
localStorageMock.expects('setItem').withExactArgs('lastActiveAccountIndex', n);
expect(setLastActiveAccount(accounts[n])).to.equal(n);
});
});

describe('setSavedAccount', () => {
it('sets accounts in localStorage with appended passed account and also returns it', () => {
localStorageMock.expects('getItem').withExactArgs('accounts').returns(JSON.stringify(accounts.slice(0, 2)));
localStorageMock.expects('setItem').withExactArgs('accounts', JSON.stringify(accounts));
expect(setSavedAccount(accounts[2])).to.deep.equal(accounts);
});
});

describe('removeSavedAccount', () => {
it('sets accounts in localStorage with appended passed account and also returns it', () => {
localStorageMock.expects('getItem').withExactArgs('accounts').returns(JSON.stringify(accounts));
localStorageMock.expects('setItem').withExactArgs('accounts', JSON.stringify(accounts.slice(0, 2)));
expect(removeSavedAccount(accounts[2])).to.deep.equal(accounts.slice(0, 2));
});
});
});

0 comments on commit 7099e47

Please sign in to comment.