Skip to content

Commit

Permalink
test: should add imported tokens to the icrcCanistersStore
Browse files Browse the repository at this point in the history
  • Loading branch information
mstrasinskis committed Aug 30, 2024
1 parent f3e8fae commit 8515ad4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
9 changes: 7 additions & 2 deletions frontend/src/lib/services/imported-tokens.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ export const loadImportedTokens = async ({
certified,
});

const icrcCanistersStoreData = get(icrcCanistersStore);
if (!certified && notForceCallStrategy()) {
return;
}

// Populate icrcCanistersStore with the imported tokens.
for (const { ledgerCanisterId, indexCanisterId } of importedTokens) {
if (isNullish(icrcCanistersStoreData[ledgerCanisterId.toText()])) {
// If the imported token is not already in the store, add it.
if (isNullish(get(icrcCanistersStore)[ledgerCanisterId.toText()])) {
icrcCanistersStore.setCanisters({
ledgerCanisterId,
indexCanisterId,
Expand Down
35 changes: 35 additions & 0 deletions frontend/src/tests/lib/services/imported-tokens.services.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import {
loadImportedTokens,
removeImportedTokens,
} from "$lib/services/imported-tokens.services";
import { icrcCanistersStore } from "$lib/stores/icrc-canisters.store";
import { importedTokensStore } from "$lib/stores/imported-tokens.store";
import * as toastsStore from "$lib/stores/toasts.store";
import type { ImportedTokenData } from "$lib/types/imported-tokens";
import { mockIdentity, resetIdentity } from "$tests/mocks/auth.store.mock";
import { principal } from "$tests/mocks/sns-projects.mock";
import { get } from "svelte/store";
import { runResolvedPromises } from "../../utils/timers.test-utils";

describe("imported-tokens-services", () => {
const importedTokenA: ImportedToken = {
Expand All @@ -39,6 +41,7 @@ describe("imported-tokens-services", () => {
vi.clearAllMocks();
resetIdentity();
importedTokensStore.reset();
icrcCanistersStore.reset();
vi.spyOn(console, "error").mockReturnValue();
});

Expand Down Expand Up @@ -74,6 +77,38 @@ describe("imported-tokens-services", () => {
});
});

it("should add imported tokens to the icrcCanistersStore", async () => {
const spyGetImportedTokens = vi
.spyOn(importedTokensApi, "getImportedTokens")
.mockResolvedValueOnce({
imported_tokens: [importedTokenA, importedTokenB],
});

expect(spyGetImportedTokens).toBeCalledTimes(0);
expect(get(icrcCanistersStore)).toEqual({});

expect(get(importedTokensStore)).toEqual({
importedTokens: undefined,
certified: undefined,
});

await loadImportedTokens();
// Wait for the store to update.
await runResolvedPromises();

expect(spyGetImportedTokens).toBeCalledTimes(2);
expect(get(icrcCanistersStore)).toEqual({
[importedTokenDataA.ledgerCanisterId.toText()]: {
ledgerCanisterId: importedTokenDataA.ledgerCanisterId,
indexCanisterId: importedTokenDataA.indexCanisterId,
},
[importedTokenDataB.ledgerCanisterId.toText()]: {
ledgerCanisterId: importedTokenDataB.ledgerCanisterId,
indexCanisterId: importedTokenDataB.indexCanisterId,
},
});
});

it("should display toast on error", async () => {
const spyToastError = vi.spyOn(toastsStore, "toastsError");
const spyGetImportedTokens = vi
Expand Down

0 comments on commit 8515ad4

Please sign in to comment.