Skip to content

Commit

Permalink
Merge pull request #464 from trilitech/remove-nonmnemonic-accounts-slice
Browse files Browse the repository at this point in the history
Remove nonmnemonic accounts slice
  • Loading branch information
ryutamago authored Sep 28, 2023
2 parents 887e147 + ca7dcfb commit c0eb26f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/utils/redux/slices/accountsSlice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,44 @@ describe("Accounts reducer", () => {
});
});

describe("removeNonMnemonicAccounts", () => {
const mnemonic = mockImplicitAccount(0);
const social1 = mockImplicitAccount(1, AccountType.SOCIAL);
const social2 = mockImplicitAccount(2, AccountType.SOCIAL);
const ledger = mockImplicitAccount(2, AccountType.LEDGER);

beforeEach(() => {
store.dispatch(addAccount([mnemonic, social1, social2, ledger]));
});

it("does nothing for mnemonic account", async () => {
store.dispatch(
accountsSlice.actions.removeNonMnemonicAccounts({
accountType: AccountType.MNEMONIC,
})
);
expect(store.getState().accounts.items).toHaveLength(4);
});

it("should remove ledger account", async () => {
store.dispatch(
accountsSlice.actions.removeNonMnemonicAccounts({
accountType: AccountType.LEDGER,
})
);
expect(store.getState().accounts.items).toEqual([mnemonic, social1, social2]);
});

it("should remove multiple social accounts", async () => {
store.dispatch(
accountsSlice.actions.removeNonMnemonicAccounts({
accountType: AccountType.SOCIAL,
})
);
expect(store.getState().accounts.items).toEqual([mnemonic, ledger]);
});
});

describe("restoreFromMnemonic thunk", () => {
it("should restore accounts from seedphrase, encrypt seedphrase and store result in state", async () => {
const fingerPrint = await getFingerPrint(mnemonic1);
Expand Down
9 changes: 9 additions & 0 deletions src/utils/redux/slices/accountsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AccountType, ImplicitAccount } from "../../../types/Account";
import { EncryptedData } from "../../crypto/types";
import changeMnemonicPassword from "../thunks/changeMnemonicPassword";
import { deriveAccount, restoreFromMnemonic } from "../thunks/restoreMnemonicAccounts";
import { remove } from "lodash";

export type State = {
items: ImplicitAccount[];
Expand Down Expand Up @@ -49,6 +50,14 @@ const accountsSlice = createSlice({
state.items = newAccounts;
delete state.seedPhrases[fingerPrint];
},
removeNonMnemonicAccounts: (
state,
{ payload }: { type: string; payload: { accountType: AccountType } }
) => {
state.items = remove(state.items, account => {
return account.type === AccountType.MNEMONIC || account.type !== payload.accountType;
});
},
addAccount: (state, { payload }: { type: string; payload: ImplicitAccount[] }) => {
state.items = concatUnique(state.items, payload);
},
Expand Down

0 comments on commit c0eb26f

Please sign in to comment.