Skip to content

Commit

Permalink
feat(frontend): add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
loki344 committed Nov 15, 2024
1 parent 8f08dee commit 4c5ceaf
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 21 deletions.
25 changes: 25 additions & 0 deletions src/frontend/src/env/map-icrc-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { EnvAdditionalIcrcTokens } from '$env/types/env-icrc-additional-token';
import type { EnvTokenSymbol } from '$env/types/env-token-common';
import type { IcInterface } from '$icp/types/ic-token';
import { BETA, PROD, STAGING } from '$lib/constants/app.constants';
import { nonNullish } from '@dfinity/utils';

/**
* Additional ICRC tokens from JSON file
*/
export const mapIcrcData = (
icrcTokens: EnvAdditionalIcrcTokens
): Record<EnvTokenSymbol, Omit<IcInterface, 'position'>> =>
Object.entries(icrcTokens).reduce(
(acc, [key, value]) => ({
...acc,
...((STAGING || BETA || PROD) &&
nonNullish(value) && {
[key]: {
...value,
exchangeCoinId: 'internet-computer'
}
})
}),
{}
);
22 changes: 1 addition & 21 deletions src/frontend/src/env/networks.icrc.env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
CKETH_EXPLORER_URL,
CKETH_SEPOLIA_EXPLORER_URL
} from '$env/explorers.env';
import { mapIcrcData } from '$env/map-icrc-data';
import { EURC_TOKEN } from '$env/tokens-erc20/tokens.eurc.env';
import { LINK_TOKEN, SEPOLIA_LINK_TOKEN } from '$env/tokens-erc20/tokens.link.env';
import { OCT_TOKEN } from '$env/tokens-erc20/tokens.oct.env';
Expand All @@ -19,7 +20,6 @@ import { BTC_MAINNET_TOKEN, BTC_TESTNET_TOKEN } from '$env/tokens.btc.env';
import { ckErc20Production, ckErc20Staging } from '$env/tokens.ckerc20.env';
import { ETHEREUM_TOKEN, SEPOLIA_TOKEN } from '$env/tokens.env';
import { additionalIcrcTokensProduction } from '$env/tokens.icrc.env';
import type { EnvAdditionalIcrcTokens } from '$env/types/env-icrc-additional-token';
import type { EnvCkErc20Tokens } from '$env/types/env-token-ckerc20';
import type { EnvTokenSymbol } from '$env/types/env-token-common';
import type { LedgerCanisterIdText } from '$icp/types/canister';
Expand Down Expand Up @@ -387,26 +387,6 @@ const CKXAUT_IC_DATA: IcCkInterface | undefined = nonNullish(CKERC20_PRODUCTION_
}
: undefined;

/**
* Additional ICRC tokens from JSON file
*/
const mapIcrcData = (
icrcTokens: EnvAdditionalIcrcTokens
): Record<EnvTokenSymbol, Omit<IcInterface, 'position'>> =>
Object.entries(icrcTokens).reduce(
(acc, [key, value]) => ({
...acc,
...((STAGING || BETA || PROD) &&
nonNullish(value) && {
[key]: {
...value,
exchangeCoinId: 'internet-computer'
}
})
}),
{}
);

const ADDITIONAL_ICRC_PRODUCTION_DATA = mapIcrcData(additionalIcrcTokensProduction);

const BURN_IC_DATA: IcInterface | undefined = nonNullish(ADDITIONAL_ICRC_PRODUCTION_DATA?.BURN)
Expand Down
38 changes: 38 additions & 0 deletions src/frontend/src/tests/env/map-icrc-data.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { describe, expect, it } from 'vitest';

import { mapIcrcData } from '$env/map-icrc-data';

describe('mapIcrcData', () => {
beforeEach(() => {
vi.mock('$lib/constants/app.constants', () => ({
PROD: true,
STAGING: false,
BETA: false
}));
});

afterEach(() => vi.clearAllMocks());

it('should map ICRC tokens with exchangeCoinId', () => {
const token = {
TESTTOKEN: {
ledgerCanisterId: 'dummy-ledger-id',
indexCanisterId: 'dummy-canister-id'
}
};

const result = mapIcrcData(token);

expect(result).toEqual({
TESTTOKEN: {
...token.TESTTOKEN,
exchangeCoinId: 'internet-computer'
}
});
});

it('should handle empty input', () => {
const result = mapIcrcData({});
expect(result).toEqual({});
});
});

0 comments on commit 4c5ceaf

Please sign in to comment.