Skip to content

Commit

Permalink
feat(frontend): add sol txn initialized like for btc/eth tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
loki344 committed Jan 8, 2025
1 parent c360dc2 commit 3be1297
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 19 deletions.
11 changes: 11 additions & 0 deletions src/frontend/src/sol/derived/sol-transactions.derived.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { tokenWithFallback } from '$lib/derived/token.derived';
import { solTransactionsStore } from '$sol/stores/sol-transactions.store';
import type { SolTransactionUi } from '$sol/types/sol-transaction';
import { nonNullish } from '@dfinity/utils';
import { derived, type Readable } from 'svelte/store';

export const solTransactions: Readable<SolTransactionUi[]> = derived(
[tokenWithFallback, solTransactionsStore],
([$token, $solTransactionsStore]) =>
($solTransactionsStore?.[$token.id] ?? []).map(({ data: transaction }) => transaction)
);

export const solTransactionsInitialized: Readable<boolean> = derived(
[solTransactionsStore, tokenWithFallback],
([solTransactionsStore, { id: $tokenId }]) => nonNullish(solTransactionsStore?.[$tokenId])
);

export const solTransactionsNotInitialized: Readable<boolean> = derived(
[solTransactionsInitialized],
([solTransactionsInitialized]) => !solTransactionsInitialized
);
101 changes: 82 additions & 19 deletions src/frontend/src/tests/sol/derived/sol-transactions.derived.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SOLANA_TOKEN, SOLANA_TOKEN_ID } from '$env/tokens/tokens.sol.env';
import { token } from '$lib/stores/token.store';
import { solTransactions } from '$sol/derived/sol-transactions.derived';
import { solTransactions, solTransactionsInitialized, solTransactionsNotInitialized } from '$sol/derived/sol-transactions.derived';
import { solTransactionsStore } from '$sol/stores/sol-transactions.store';
import { createMockSolTransactionUi } from '$tests/mocks/sol-transactions.mock';
import { get } from 'svelte/store';
Expand All @@ -21,31 +21,94 @@ describe('sol-transactions.derived', () => {
token.set(SOLANA_TOKEN);
});

it('should return an empty array when transactions store is empty', () => {
const result = get(solTransactions);
expect(result).toEqual([]);
});
describe('solTransactions', () => {

it('should return an empty array when transactions store is empty', () => {
const result = get(solTransactions);
expect(result).toEqual([]);
});

it('should return empty array when transactions is nullish', () => {
solTransactionsStore.append({
tokenId: SOLANA_TOKEN_ID,
transactions
it('should return empty array when transactions is nullish', () => {
solTransactionsStore.append({
tokenId: SOLANA_TOKEN_ID,
transactions
});

solTransactionsStore.nullify(SOLANA_TOKEN_ID);

const result = get(solTransactions);
expect(result).toHaveLength(0);
expect(result).toEqual([]);
});

solTransactionsStore.nullify(SOLANA_TOKEN_ID);
it('should return transactions for the current token', () => {
solTransactionsStore.append({
tokenId: SOLANA_TOKEN_ID,
transactions
});

const result = get(solTransactions);
expect(result).toEqual(transactions.map(({ data }) => data));
});
})

describe('solTransactionsInitialized', () => {
it('should return false when transactions store is empty', () => {
solTransactionsStore.reset(SOLANA_TOKEN_ID);
const result = get(solTransactionsInitialized);
expect(result).toBe(false);
});

it('should return false when transactions are nullish', () => {
solTransactionsStore.append({
tokenId: SOLANA_TOKEN_ID,
transactions
});

solTransactionsStore.nullify(SOLANA_TOKEN_ID);

const result = get(solTransactions);
expect(result).toHaveLength(0);
expect(result).toEqual([]);
const result = get(solTransactionsInitialized);
expect(result).toBe(false);
});

it('should return true when transactions are initialized', () => {
solTransactionsStore.append({
tokenId: SOLANA_TOKEN_ID,
transactions
});

const result = get(solTransactionsInitialized);
expect(result).toBe(true);
});
});

it('should return transactions for the current token', () => {
solTransactionsStore.append({
tokenId: SOLANA_TOKEN_ID,
transactions
describe('solTransactionsNotInitialized', () => {
it('should return true when transactions store is empty', () => {
solTransactionsStore.reset(SOLANA_TOKEN_ID);
const result = get(solTransactionsNotInitialized);
expect(result).toBe(true);
});

it('should return true when transactions are nullish', () => {
solTransactionsStore.append({
tokenId: SOLANA_TOKEN_ID,
transactions
});

solTransactionsStore.nullify(SOLANA_TOKEN_ID);

const result = get(solTransactionsNotInitialized);
expect(result).toBe(true);
});

const result = get(solTransactions);
expect(result).toEqual(transactions.map(({ data }) => data));
it('should return false when transactions are initialized', () => {
solTransactionsStore.append({
tokenId: SOLANA_TOKEN_ID,
transactions
});

const result = get(solTransactionsNotInitialized);
expect(result).toBe(false);
});
});
});

0 comments on commit 3be1297

Please sign in to comment.