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

Commit

Permalink
Merge pull request #471 from juan-cortes/LL-2136
Browse files Browse the repository at this point in the history
LL-2136 Account ordering prioritize starred accounts + tests
  • Loading branch information
gre authored Jan 24, 2020
2 parents 97693f1 + 6c7c86d commit fa904d9
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 10 deletions.
167 changes: 167 additions & 0 deletions src/__tests__/accounts/ordering.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// @flow
import { fromAccountRaw, sortAccountsComparatorFromOrder } from "../../account";

const accounts = [
{
id: "ethereumjs:2:ethereum:0x01:",
seedIdentifier: "0x01",
name: "A",
derivationMode: "",
index: 0,
freshAddress: "0x01",
freshAddressPath: "44'/60'/0'/0/0",
freshAddresses: [],
blockHeight: 8168983,
operations: [],
pendingOperations: [],
currencyId: "ethereum",
unitMagnitude: 18,
lastSyncDate: "2019-07-17T15:13:30.318Z",
balance: "1000000000000000000"
},
{
id: "ethereumjs:2:ethereum:0x02:",
seedIdentifier: "0x02",
name: "B",
derivationMode: "",
index: 1,
freshAddress: "0x02",
freshAddressPath: "44'/60'/1'/0/0",
freshAddresses: [],
blockHeight: 8168983,
operations: [],
pendingOperations: [],
currencyId: "ethereum",
unitMagnitude: 18,
lastSyncDate: "2019-07-17T15:13:29.306Z",
balance: "2000000000000000000"
},
{
id: "libcore:1:ethereum:xpub3:",
seedIdentifier: "seed",
name: "C",
derivationMode: "",
index: 2,
freshAddress: "0x03",
freshAddressPath: "44'/60'/2'/0/0",
freshAddresses: [],
blockHeight: 8168983,
operations: [],
pendingOperations: [],
currencyId: "ethereum",
unitMagnitude: 18,
lastSyncDate: "2019-07-17T15:13:29.306Z",
balance: "3000000000000000000"
},
{
id: "libcore:1:ethereum:xpub3B:",
seedIdentifier: "seed",
name: "CA",
derivationMode: "",
index: 2,
freshAddress: "0x03",
freshAddressPath: "44'/60'/2'/0/0",
freshAddresses: [],
blockHeight: 8168983,
operations: [],
pendingOperations: [],
currencyId: "ethereum",
unitMagnitude: 18,
lastSyncDate: "2019-07-17T15:13:29.306Z",
balance: "3000000000000000000"
},
{
id: "libcore:1:ethereum:xpub1B:",
seedIdentifier: "seed",
name: "AA",
derivationMode: "",
index: 2,
freshAddress: "0x03",
freshAddressPath: "44'/60'/2'/0/0",
freshAddresses: [],
blockHeight: 8168983,
operations: [],
pendingOperations: [],
currencyId: "ethereum",
unitMagnitude: 18,
lastSyncDate: "2019-07-17T15:13:29.306Z",
balance: "4000000000000000000"
}
].map(fromAccountRaw);

const mockedCalculateCountervalue = (_, balance) => balance;

test("Accounts ordering | name asc", () => {
const compareFn = sortAccountsComparatorFromOrder(
"name|asc",
mockedCalculateCountervalue,
[]
);
const sortedAccounts = accounts.sort(compareFn);
expect(sortedAccounts.map(a => a.name)).toEqual(["A", "AA", "B", "C", "CA"]);
});

test("Accounts ordering | name desc", () => {
const compareFn = sortAccountsComparatorFromOrder(
"name|desc",
mockedCalculateCountervalue,
[]
);
const sortedAccounts = accounts.sort(compareFn);
expect(sortedAccounts.map(a => a.name)).toEqual(["CA", "C", "B", "AA", "A"]);
});

test("Accounts ordering | balance asc", () => {
const compareFn = sortAccountsComparatorFromOrder(
"balance|asc",
mockedCalculateCountervalue,
[]
);
const sortedAccounts = accounts.sort(compareFn);
expect(sortedAccounts.map(a => a.name)).toEqual(["A", "B", "C", "CA", "AA"]);
});

test("Accounts ordering | balance desc", () => {
const compareFn = sortAccountsComparatorFromOrder(
"balance|desc",
mockedCalculateCountervalue,
[]
);
const sortedAccounts = accounts.sort(compareFn);
expect(sortedAccounts.map(a => a.name)).toEqual(["AA", "C", "CA", "B", "A"]);
});

test("Accounts ordering | starred + name", () => {
const compareFn = sortAccountsComparatorFromOrder(
"name|desc",
mockedCalculateCountervalue,
["ethereumjs:2:ethereum:0x01:"]
);
const sortedAccounts = accounts.sort(compareFn);
expect(sortedAccounts.map(a => a.name)).toEqual(["A", "CA", "C", "B", "AA"]);
});

test("Accounts ordering | all accounts starred, then balance, then name", () => {
const compareFn = sortAccountsComparatorFromOrder(
"balance|desc",
mockedCalculateCountervalue,
[
"ethereumjs:2:ethereum:0x01:",
"ethereumjs:2:ethereum:0x02:",
"libcore:1:ethereum:xpub3:",
"libcore:1:ethereum:xpub3B:",
"libcore:1:ethereum:xpub1B:"
]
);
const sortedAccounts = accounts.sort(compareFn);
expect(sortedAccounts.map(a => a.name)).toEqual(["AA", "C", "CA", "B", "A"]);
});

test("Accounts ordering | backwards compatible when no starred accounts are passed", () => {
const compareFn = sortAccountsComparatorFromOrder(
"balance|desc",
mockedCalculateCountervalue
);
const sortedAccounts = accounts.sort(compareFn);
expect(sortedAccounts.map(a => a.name)).toEqual(["AA", "C", "CA", "B", "A"]);
});
33 changes: 23 additions & 10 deletions src/account/ordering.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,20 @@ export const sortAccountsComparatorFromOrder = (
calculateCountervalue: (
currency: TokenCurrency | CryptoCurrency,
value: BigNumber
) => ?BigNumber
) => ?BigNumber,
starredAccountIds: string[] = []
): AccountComparator => {
const [order, sort] = orderAccounts.split("|");
const ascValue = sort === "desc" ? -1 : 1;
if (order === "name") {
return (a, b) =>
ascValue * sortNameLense(a).localeCompare(sortNameLense(b));
return (a, b) => {
const starDiff =
Number(starredAccountIds.includes(b.id)) -
Number(starredAccountIds.includes(a.id));
if (starDiff === 0)
return ascValue * sortNameLense(a).localeCompare(sortNameLense(b));
return starDiff;
};
}
const cvCaches = {};
const lazyCalcCV = a => {
Expand All @@ -46,13 +53,19 @@ export const sortAccountsComparatorFromOrder = (
return v;
};
return (a, b) => {
const diff =
ascValue *
lazyCalcCV(a)
.minus(lazyCalcCV(b))
.toNumber();
if (diff === 0) return sortNameLense(a).localeCompare(sortNameLense(b));
return diff;
const starDiff =
Number(starredAccountIds.includes(b.id)) -
Number(starredAccountIds.includes(a.id));
if (starDiff === 0) {
const diff =
ascValue *
lazyCalcCV(a)
.minus(lazyCalcCV(b))
.toNumber();
if (diff === 0) return sortNameLense(a).localeCompare(sortNameLense(b));
return diff;
}
return starDiff;
};
};

Expand Down

0 comments on commit fa904d9

Please sign in to comment.