Skip to content

Commit

Permalink
changes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabib committed Dec 6, 2024
1 parent 6dd67bd commit b241623
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
import { i18n } from "$lib/stores/i18n";
import { IconDown } from "@dfinity/gix-components";
import { createEventDispatcher } from "svelte";
import { ICPToken, isNullish, TokenAmountV2 } from "@dfinity/utils";
import {
ICPToken,
isNullish,
nonNullish,
TokenAmountV2,
} from "@dfinity/utils";
import {
CsvGenerationError,
FileSystemAccessError,
Expand Down Expand Up @@ -34,9 +39,13 @@
let isDisabled = true;
$: identity = $authStore.identity;
$: isDisabled = isNullish(identity);
const swapCanisterAccountsStore = createSwapCanisterAccountsStore(
identity?.getPrincipal()
);
$: swapCanisterAccounts = $swapCanisterAccountsStore ?? new Set();
$: neuronAccounts = $neuronAccountsStore;
let swapCanisterAccountsStore;
$: nnsAccounts = Object.values($nnsAccountsListStore).flat();
$: isDisabled = isNullish(identity) || nnsAccounts.length === 0;
const buildDatasets = (
data: TransactionsAndAccounts
Expand All @@ -57,9 +66,6 @@
}[];
}[] => {
return data.map(({ account, transactions }) => {
swapCanisterAccountsStore = createSwapCanisterAccountsStore(
identity?.getPrincipal()
);
const amount = TokenAmountV2.fromUlps({
amount: account.balanceUlps,
token: ICPToken,
Expand Down Expand Up @@ -110,10 +116,14 @@
accountIdentifier: account.identifier,
transaction,
neuronAccounts,
swapCanisterAccounts: $swapCanisterAccountsStore ?? new Set(),
swapCanisterAccounts,
});
const sign = transactionDirection === "credit" ? "+" : "-";
const amount = formatTokenV2({ value: tokenAmount, detailed: true });
const timestamp = nonNullish(timestampNanos)
? nanoSecondsToDateTime(timestampNanos)
: $i18n.core.not_applicable;
return {
id: transaction.id.toString(),
Expand All @@ -123,10 +133,7 @@
from,
type: transactionName({ type, i18n: $i18n }),
amount: `${sign}${amount}`,
timestamp:
timestampNanos !== undefined
? nanoSecondsToDateTime(timestampNanos)
: $i18n.core.not_applicable,
timestamp,
};
}),
};
Expand All @@ -136,8 +143,6 @@
const exportIcpTransactions = async () => {
// Button will only be shown if logged in
try {
const nnsAccounts = Object.values($nnsAccountsListStore).flat();
const data = await getAccountTransactionsConcurrently({
accounts: nnsAccounts,
identity: identity as SignIdentity,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import * as icpIndexApi from "$lib/api/icp-index.api";
import ExportIcpTransactionsButton from "$lib/components/header/ExportIcpTransactionsButton.svelte";
import { authStore } from "$lib/stores/auth.store";
import * as toastsStore from "$lib/stores/toasts.store";
import * as exportToCsv from "$lib/utils/export-to-csv.utils";
import {
mockSignInIdentity,
resetSignInIdentity,
setNoIdentity,
} from "$tests/mocks/auth.store.mock";
import { mockAccountsStoreData } from "$tests/mocks/icp-accounts.store.mock";
import { createTransactionWithId } from "$tests/mocks/icp-transactions.mock";
import { ExportIcpTransactionsButtonPo } from "$tests/page-objects/ExportIcpTransactionsButton.page-object";
import { JestPageObjectElement } from "$tests/page-objects/jest.page-object";
import { setAccountsForTesting } from "$tests/utils/accounts.test-utils";
import { runResolvedPromises } from "$tests/utils/timers.test-utils";
import { render } from "@testing-library/svelte";
import { tick } from "svelte";

vi.mock("$lib/api/icp-ledger.api");

Expand All @@ -34,7 +33,7 @@ describe("ExportIcpTransactionsButton", () => {
vi.useFakeTimers();
vi.setSystemTime(mockDate);

authStore.setForTesting(mockSignInIdentity);
resetSignInIdentity();

setAccountsForTesting({
...mockAccountsStoreData,
Expand Down Expand Up @@ -80,8 +79,7 @@ describe("ExportIcpTransactionsButton", () => {
expect(spyGenerateCsvFileToSave).toHaveBeenCalledTimes(0);

await po.click();
// Wait for getAccountTransactionsConcurrently to complete
await tick();
await runResolvedPromises();

const expectedFileName = `icp_transactions_export_20231014`;
expect(spyGenerateCsvFileToSave).toHaveBeenCalledWith(
Expand All @@ -97,8 +95,7 @@ describe("ExportIcpTransactionsButton", () => {

expect(spyGenerateCsvFileToSave).toBeCalledTimes(0);
await po.click();
// Wait for getAccountTransactionsConcurrently to complete
await tick();
await runResolvedPromises();

expect(spyGenerateCsvFileToSave).toBeCalledWith(
expect.objectContaining({
Expand Down Expand Up @@ -158,10 +155,7 @@ describe("ExportIcpTransactionsButton", () => {
expect(onTrigger).toHaveBeenCalledTimes(0);

await po.click();
// Wait for getAccountTransactionsConcurrently to complete
await tick();
// Wait for the csv generation to complete
await tick();
await runResolvedPromises();
expect(onTrigger).toHaveBeenCalledTimes(1);
});

Expand All @@ -175,10 +169,7 @@ describe("ExportIcpTransactionsButton", () => {
expect(spyToastError).toBeCalledTimes(0);

await po.click();
// Wait for getAccountTransactionsConcurrently to complete
await tick();
// Wait for the csv generation to complete
await tick();
await runResolvedPromises();

expect(spyToastError).toBeCalledWith({
labelKey: "export_error.file_system_access",
Expand All @@ -196,10 +187,7 @@ describe("ExportIcpTransactionsButton", () => {
expect(spyToastError).toBeCalledTimes(0);

await po.click();
// Wait for getAccountTransactionsConcurrently to complete
await tick();
// Wait for the csv generation to complete
await tick();
await runResolvedPromises();

expect(spyToastError).toBeCalledWith({
labelKey: "export_error.csv_generation",
Expand All @@ -216,11 +204,8 @@ describe("ExportIcpTransactionsButton", () => {

expect(spyToastError).toBeCalledTimes(0);
await po.click();
await runResolvedPromises();

// Wait for getAccountTransactionsConcurrently to complete
await tick();
// Wait for the csv generation to complete
await tick();
expect(spyToastError).toBeCalledWith({
labelKey: "export_error.neurons",
});
Expand Down

0 comments on commit b241623

Please sign in to comment.