From d3b74e16522753bb8571d73ee2ac597a96e6f58e Mon Sep 17 00:00:00 2001 From: DenysKarmazynDFINITY Date: Fri, 22 Nov 2024 15:51:35 +0100 Subject: [PATCH] feat(frontend): update utxos fee store and context --- .../convert/BtcConvertTokenWizard.svelte | 4 +- .../btc/components/fee/UtxosFeeContext.svelte | 15 +++- .../src/btc/stores/utxos-fee.store.ts | 6 +- .../convert/BtcConvertFeeTotal.spec.ts | 24 +++---- .../components/convert/BtcConvertForm.spec.ts | 30 ++++---- .../convert/BtcConvertTokenWizard.spec.ts | 23 ++---- .../components/fee/UtxosFeeContext.spec.ts | 71 ++++++++++++++----- .../tests/btc/stores/utxos-fee.store.spec.ts | 33 ++++----- 8 files changed, 120 insertions(+), 86 deletions(-) diff --git a/src/frontend/src/btc/components/convert/BtcConvertTokenWizard.svelte b/src/frontend/src/btc/components/convert/BtcConvertTokenWizard.svelte index f136ecc033..6e3eea3ba9 100644 --- a/src/frontend/src/btc/components/convert/BtcConvertTokenWizard.svelte +++ b/src/frontend/src/btc/components/convert/BtcConvertTokenWizard.svelte @@ -11,7 +11,7 @@ import { UTXOS_FEE_CONTEXT_KEY, type UtxosFeeContext as UtxosFeeContextType, - initUtxosFeeStore + utxosFeeStore } from '$btc/stores/utxos-fee.store'; import { btcAddressStore } from '$icp/stores/btc.store'; import ButtonBack from '$lib/components/ui/ButtonBack.svelte'; @@ -43,8 +43,6 @@ export let convertProgressStep: string; export let formCancelAction: 'back' | 'close' = 'close'; - const utxosFeeStore = initUtxosFeeStore(); - setContext(UTXOS_FEE_CONTEXT_KEY, { store: utxosFeeStore }); diff --git a/src/frontend/src/btc/components/fee/UtxosFeeContext.svelte b/src/frontend/src/btc/components/fee/UtxosFeeContext.svelte index d57ac9260f..cba6d754d9 100644 --- a/src/frontend/src/btc/components/fee/UtxosFeeContext.svelte +++ b/src/frontend/src/btc/components/fee/UtxosFeeContext.svelte @@ -21,17 +21,25 @@ return; } + const parsedAmount = nonNullish(amount) ? Number(amount) : undefined; + // we need to make the value is not 0 because the utxos call fails if amount = 0 - if (amountError || isNullish(networkId) || isNullish(amount) || Number(amount) === 0) { + if (amountError || isNullish(networkId) || isNullish(parsedAmount) || parsedAmount === 0) { store.reset(); return; } + // WizardModal re-renders content on step change (e.g. when switching between Convert to Review steps) + // To avoid re-fetching the fees, we need to check if amount hasn't changed since the last request + if (nonNullish($store) && $store.amount === parsedAmount) { + return; + } + const network = mapNetworkIdToBitcoinNetwork(networkId); const utxosFee = nonNullish(network) ? await selectUtxosFeeApi({ - amount, + amount: parsedAmount, network, identity: $authIdentity }) @@ -43,7 +51,8 @@ } store.setUtxosFee({ - utxosFee + utxosFee, + amount: parsedAmount }); }; diff --git a/src/frontend/src/btc/stores/utxos-fee.store.ts b/src/frontend/src/btc/stores/utxos-fee.store.ts index 88bf1d0875..8d616fe163 100644 --- a/src/frontend/src/btc/stores/utxos-fee.store.ts +++ b/src/frontend/src/btc/stores/utxos-fee.store.ts @@ -1,9 +1,11 @@ import type { UtxosFee } from '$btc/types/btc-send'; +import type { OptionAmount } from '$lib/types/send'; import type { Option } from '$lib/types/utils'; import { writable, type Readable } from 'svelte/store'; export type UtxosFeeStoreData = Option<{ utxosFee?: UtxosFee; + amount?: OptionAmount; }>; export interface UtxosFeeStore extends Readable { @@ -11,7 +13,7 @@ export interface UtxosFeeStore extends Readable { reset: () => void; } -export const initUtxosFeeStore = (): UtxosFeeStore => { +const initUtxosFeeStore = (): UtxosFeeStore => { const { subscribe, set } = writable(undefined); return { @@ -32,3 +34,5 @@ export interface UtxosFeeContext { } export const UTXOS_FEE_CONTEXT_KEY = Symbol('utxos-fee'); + +export const utxosFeeStore = initUtxosFeeStore(); diff --git a/src/frontend/src/tests/btc/components/convert/BtcConvertFeeTotal.spec.ts b/src/frontend/src/tests/btc/components/convert/BtcConvertFeeTotal.spec.ts index f74cb27dab..a241d4bdad 100644 --- a/src/frontend/src/tests/btc/components/convert/BtcConvertFeeTotal.spec.ts +++ b/src/frontend/src/tests/btc/components/convert/BtcConvertFeeTotal.spec.ts @@ -1,8 +1,8 @@ import BtcConvertFeeTotal from '$btc/components/convert/BtcConvertFeeTotal.svelte'; import { BTC_CONVERT_FEE } from '$btc/constants/btc.constants'; import { - initUtxosFeeStore, UTXOS_FEE_CONTEXT_KEY, + utxosFeeStore, type UtxosFeeStore } from '$btc/stores/utxos-fee.store'; import { BTC_MAINNET_TOKEN } from '$env/tokens/tokens.btc.env'; @@ -17,17 +17,16 @@ import { render } from '@testing-library/svelte'; import { readable } from 'svelte/store'; describe('BtcConvertFeeTotal', () => { - let store: UtxosFeeStore; const exchangeRate = 0.01; const mockContext = ({ - utxosFeeStore, + mockUtxosFeeStore, destinationTokenId = ICP_TOKEN.id }: { - utxosFeeStore: UtxosFeeStore; + mockUtxosFeeStore: UtxosFeeStore; destinationTokenId?: TokenId; }) => new Map([ - [UTXOS_FEE_CONTEXT_KEY, { store: utxosFeeStore }], + [UTXOS_FEE_CONTEXT_KEY, { store: mockUtxosFeeStore }], [ CONVERT_CONTEXT_KEY, { @@ -40,21 +39,20 @@ describe('BtcConvertFeeTotal', () => { beforeEach(() => { mockPage.reset(); - store = initUtxosFeeStore(); - store.reset(); + utxosFeeStore.reset(); }); it('should calculate totalFee correctly if only default fee is available', () => { const { component } = render(BtcConvertFeeTotal, { - context: mockContext({ utxosFeeStore: store }) + context: mockContext({ mockUtxosFeeStore: utxosFeeStore }) }); expect(component.$$.ctx[component.$$.props['totalFee']]).toBe(BTC_CONVERT_FEE); }); it('should calculate totalFee correctly if default and utxos fees are available', () => { - store.setUtxosFee({ utxosFee: mockUtxosFee }); + utxosFeeStore.setUtxosFee({ utxosFee: mockUtxosFee }); const { component } = render(BtcConvertFeeTotal, { - context: mockContext({ utxosFeeStore: store }) + context: mockContext({ mockUtxosFeeStore: utxosFeeStore }) }); expect(component.$$.ctx[component.$$.props['totalFee']]).toBe( BTC_CONVERT_FEE + mockUtxosFee.feeSatoshis @@ -64,7 +62,7 @@ describe('BtcConvertFeeTotal', () => { it('should calculate totalFee correctly if default and ckBTC minter fees are available', () => { const tokenId = setupCkBTCStores(); const { component } = render(BtcConvertFeeTotal, { - context: mockContext({ utxosFeeStore: store, destinationTokenId: tokenId }) + context: mockContext({ mockUtxosFeeStore: utxosFeeStore, destinationTokenId: tokenId }) }); expect(component.$$.ctx[component.$$.props['totalFee']]).toBe( BTC_CONVERT_FEE + mockCkBtcMinterInfo.kyt_fee @@ -72,10 +70,10 @@ describe('BtcConvertFeeTotal', () => { }); it('should calculate totalFee correctly if all fees are available', () => { - store.setUtxosFee({ utxosFee: mockUtxosFee }); + utxosFeeStore.setUtxosFee({ utxosFee: mockUtxosFee }); const tokenId = setupCkBTCStores(); const { component } = render(BtcConvertFeeTotal, { - context: mockContext({ utxosFeeStore: store, destinationTokenId: tokenId }) + context: mockContext({ mockUtxosFeeStore: utxosFeeStore, destinationTokenId: tokenId }) }); expect(component.$$.ctx[component.$$.props['totalFee']]).toBe( BTC_CONVERT_FEE + mockCkBtcMinterInfo.kyt_fee + mockUtxosFee.feeSatoshis diff --git a/src/frontend/src/tests/btc/components/convert/BtcConvertForm.spec.ts b/src/frontend/src/tests/btc/components/convert/BtcConvertForm.spec.ts index 1a65cad23a..49cab21837 100644 --- a/src/frontend/src/tests/btc/components/convert/BtcConvertForm.spec.ts +++ b/src/frontend/src/tests/btc/components/convert/BtcConvertForm.spec.ts @@ -1,8 +1,8 @@ import BtcConvertForm from '$btc/components/convert/BtcConvertForm.svelte'; import * as btcPendingSendTransactionsStatusStore from '$btc/derived/btc-pending-sent-transactions-status.derived'; import { - initUtxosFeeStore, UTXOS_FEE_CONTEXT_KEY, + utxosFeeStore, type UtxosFeeStore } from '$btc/stores/utxos-fee.store'; import { BTC_MAINNET_TOKEN } from '$env/tokens/tokens.btc.env'; @@ -17,7 +17,6 @@ import { BigNumber } from 'alchemy-sdk'; import { readable } from 'svelte/store'; describe('BtcConvertForm', () => { - let store: UtxosFeeStore; const mockContext = ({ utxosFeeStore, sourceTokenBalance = 1000000n @@ -56,24 +55,23 @@ describe('BtcConvertForm', () => { beforeEach(() => { mockPage.reset(); - store = initUtxosFeeStore(); - store.reset(); + utxosFeeStore.reset(); }); it('should keep the next button clickable if all requirements are met', () => { - store.setUtxosFee({ utxosFee: mockUtxosFee }); + utxosFeeStore.setUtxosFee({ utxosFee: mockUtxosFee }); mockBtcPendingSendTransactionsStatusStore(); const { getByTestId } = render(BtcConvertForm, { props, - context: mockContext({ utxosFeeStore: store }) + context: mockContext({ utxosFeeStore }) }); expect(getByTestId(buttonTestId)).not.toHaveAttribute('disabled'); }); it('should keep the next button disabled if amount is undefined', () => { - store.setUtxosFee({ utxosFee: mockUtxosFee }); + utxosFeeStore.setUtxosFee({ utxosFee: mockUtxosFee }); mockBtcPendingSendTransactionsStatusStore(); const { getByTestId } = render(BtcConvertForm, { @@ -81,14 +79,14 @@ describe('BtcConvertForm', () => { ...props, sendAmount: undefined }, - context: mockContext({ utxosFeeStore: store }) + context: mockContext({ utxosFeeStore }) }); expect(getByTestId(buttonTestId)).toHaveAttribute('disabled'); }); it('should keep the next button disabled if amount is invalid', () => { - store.setUtxosFee({ utxosFee: mockUtxosFee }); + utxosFeeStore.setUtxosFee({ utxosFee: mockUtxosFee }); mockBtcPendingSendTransactionsStatusStore(); const { getByTestId } = render(BtcConvertForm, { @@ -96,7 +94,7 @@ describe('BtcConvertForm', () => { ...props, sendAmount: -1 }, - context: mockContext({ utxosFeeStore: store }) + context: mockContext({ utxosFeeStore }) }); expect(getByTestId(buttonTestId)).toHaveAttribute('disabled'); @@ -107,19 +105,19 @@ describe('BtcConvertForm', () => { const { getByTestId } = render(BtcConvertForm, { props, - context: mockContext({ utxosFeeStore: store }) + context: mockContext({ utxosFeeStore }) }); expect(getByTestId(buttonTestId)).toHaveAttribute('disabled'); }); it('should keep the next button disabled if utxos are not available', () => { - store.setUtxosFee({ utxosFee: { ...mockUtxosFee, utxos: [] } }); + utxosFeeStore.setUtxosFee({ utxosFee: { ...mockUtxosFee, utxos: [] } }); mockBtcPendingSendTransactionsStatusStore(); const { getByTestId } = render(BtcConvertForm, { props, - context: mockContext({ utxosFeeStore: store }) + context: mockContext({ utxosFeeStore }) }); expect(getByTestId(buttonTestId)).toHaveAttribute('disabled'); @@ -132,7 +130,7 @@ describe('BtcConvertForm', () => { const { getByTestId } = render(BtcConvertForm, { props, - context: mockContext({ utxosFeeStore: store, sourceTokenBalance: 0n }) + context: mockContext({ utxosFeeStore, sourceTokenBalance: 0n }) }); await waitFor(() => { @@ -149,7 +147,7 @@ describe('BtcConvertForm', () => { const { getByTestId } = render(BtcConvertForm, { props, - context: mockContext({ utxosFeeStore: store }) + context: mockContext({ utxosFeeStore }) }); await waitFor(() => { @@ -168,7 +166,7 @@ describe('BtcConvertForm', () => { const { getByTestId } = render(BtcConvertForm, { props, - context: mockContext({ utxosFeeStore: store }) + context: mockContext({ utxosFeeStore }) }); await waitFor(() => { diff --git a/src/frontend/src/tests/btc/components/convert/BtcConvertTokenWizard.spec.ts b/src/frontend/src/tests/btc/components/convert/BtcConvertTokenWizard.spec.ts index 1fb63e0f5c..4f8207a57c 100644 --- a/src/frontend/src/tests/btc/components/convert/BtcConvertTokenWizard.spec.ts +++ b/src/frontend/src/tests/btc/components/convert/BtcConvertTokenWizard.spec.ts @@ -1,7 +1,6 @@ import BtcConvertTokenWizard from '$btc/components/convert/BtcConvertTokenWizard.svelte'; import * as btcPendingSentTransactionsStore from '$btc/services/btc-pending-sent-transactions.services'; -import * as utxosFeeStore from '$btc/stores/utxos-fee.store'; -import type { UtxosFee } from '$btc/types/btc-send'; +import { utxosFeeStore } from '$btc/stores/utxos-fee.store'; import { convertNumberToSatoshis } from '$btc/utils/btc-send.utils'; import { BTC_MAINNET_TOKEN } from '$env/tokens/tokens.btc.env'; import { ETHEREUM_TOKEN } from '$env/tokens/tokens.eth.env'; @@ -73,14 +72,6 @@ describe('BtcConvertTokenWizard', () => { vi .spyOn(btcPendingSentTransactionsStore, 'loadBtcPendingSentTransactions') .mockResolvedValue({ success: true }); - const mockUtxosFeeStore = (utxosFee?: UtxosFee) => { - const store = utxosFeeStore.initUtxosFeeStore(); - - vi.spyOn(utxosFeeStore, 'initUtxosFeeStore').mockImplementation(() => { - store.setUtxosFee({ utxosFee }); - return store; - }); - }; const clickConvertButton = async (container: HTMLElement) => { const convertButtonSelector = '[data-tid="convert-review-button-next"]'; const button: HTMLButtonElement | null = container.querySelector(convertButtonSelector); @@ -91,6 +82,7 @@ describe('BtcConvertTokenWizard', () => { beforeEach(() => { mockPage.reset(); mockBtcPendingSentTransactionsStore(); + utxosFeeStore.reset(); }); it('should call sendBtc if all requirements are met', async () => { @@ -99,7 +91,7 @@ describe('BtcConvertTokenWizard', () => { mockAuthStore(); mockBtcAddressStore(); mockAddressesStore(); - mockUtxosFeeStore(mockUtxosFee); + utxosFeeStore.setUtxosFee({ utxosFee: mockUtxosFee }); const { container } = render(BtcConvertTokenWizard, { props, @@ -130,7 +122,7 @@ describe('BtcConvertTokenWizard', () => { mockAuthStore(null); mockBtcAddressStore(); mockAddressesStore(); - mockUtxosFeeStore(mockUtxosFee); + utxosFeeStore.setUtxosFee({ utxosFee: mockUtxosFee }); const { container } = render(BtcConvertTokenWizard, { props, @@ -149,7 +141,7 @@ describe('BtcConvertTokenWizard', () => { mockAuthStore(); mockAddressesStore(); mockBtcAddressStore(); - mockUtxosFeeStore(mockUtxosFee); + utxosFeeStore.setUtxosFee({ utxosFee: mockUtxosFee }); const { container } = render(BtcConvertTokenWizard, { props, @@ -168,7 +160,7 @@ describe('BtcConvertTokenWizard', () => { mockAuthStore(); mockAddressesStore(); mockBtcAddressStore(''); - mockUtxosFeeStore(mockUtxosFee); + utxosFeeStore.setUtxosFee({ utxosFee: mockUtxosFee }); const { container } = render(BtcConvertTokenWizard, { props, @@ -187,7 +179,7 @@ describe('BtcConvertTokenWizard', () => { mockAuthStore(); mockAddressesStore(); mockBtcAddressStore(); - mockUtxosFeeStore(mockUtxosFee); + utxosFeeStore.setUtxosFee({ utxosFee: mockUtxosFee }); const { container } = render(BtcConvertTokenWizard, { props: { @@ -209,7 +201,6 @@ describe('BtcConvertTokenWizard', () => { mockAuthStore(); mockAddressesStore(); mockBtcAddressStore(); - mockUtxosFeeStore(undefined); const { container } = render(BtcConvertTokenWizard, { props, diff --git a/src/frontend/src/tests/btc/components/fee/UtxosFeeContext.spec.ts b/src/frontend/src/tests/btc/components/fee/UtxosFeeContext.spec.ts index f6f5634600..0e1fe3a673 100644 --- a/src/frontend/src/tests/btc/components/fee/UtxosFeeContext.spec.ts +++ b/src/frontend/src/tests/btc/components/fee/UtxosFeeContext.spec.ts @@ -1,8 +1,8 @@ import UtxosFeeContext from '$btc/components/fee/UtxosFeeContext.svelte'; import * as btcSendApi from '$btc/services/btc-send.services'; import { - initUtxosFeeStore, UTXOS_FEE_CONTEXT_KEY, + utxosFeeStore, type UtxosFeeStore } from '$btc/stores/utxos-fee.store'; import { BTC_MAINNET_NETWORK_ID, ICP_NETWORK_ID } from '$env/networks.env'; @@ -23,7 +23,6 @@ describe('UtxosFeeContext', () => { vi.spyOn(btcSendApi, 'selectUtxosFee').mockResolvedValue(mockUtxosFee); const mockAuthStore = (value: Identity | null = mockIdentity) => vi.spyOn(authStore, 'authIdentity', 'get').mockImplementation(() => readable(value)); - let store: UtxosFeeStore; const props = { amount, @@ -32,19 +31,18 @@ describe('UtxosFeeContext', () => { beforeEach(() => { mockPage.reset(); - store = initUtxosFeeStore(); - store.reset(); + utxosFeeStore.reset(); }); it('should call selectUtxosFee with proper params', async () => { - const setUtxosFeeSpy = vi.spyOn(store, 'setUtxosFee'); + const setUtxosFeeSpy = vi.spyOn(utxosFeeStore, 'setUtxosFee'); const selectUtxosFeeSpy = mockBtcSendApi(); mockAuthStore(); render(UtxosFeeContext, { props, - context: mockContext(store) + context: mockContext(utxosFeeStore) }); await waitFor(() => { @@ -55,7 +53,7 @@ describe('UtxosFeeContext', () => { identity: mockIdentity }); expect(setUtxosFeeSpy).toHaveBeenCalledOnce(); - expect(setUtxosFeeSpy).toHaveBeenCalledWith({ utxosFee: mockUtxosFee }); + expect(setUtxosFeeSpy).toHaveBeenCalledWith({ utxosFee: mockUtxosFee, amount }); }); }); @@ -66,7 +64,7 @@ describe('UtxosFeeContext', () => { render(UtxosFeeContext, { props, - context: mockContext(store) + context: mockContext(utxosFeeStore) }); await waitFor(() => { @@ -75,7 +73,7 @@ describe('UtxosFeeContext', () => { }); it('should not call selectUtxosFee if no networkId provided', async () => { - const resetSpy = vi.spyOn(store, 'reset'); + const resetSpy = vi.spyOn(utxosFeeStore, 'reset'); const selectUtxosFeeSpy = mockBtcSendApi(); const { networkId: _, ...newProps } = props; @@ -83,7 +81,7 @@ describe('UtxosFeeContext', () => { render(UtxosFeeContext, { props: newProps, - context: mockContext(store) + context: mockContext(utxosFeeStore) }); await waitFor(() => { @@ -93,7 +91,7 @@ describe('UtxosFeeContext', () => { }); it('should not call selectUtxosFee if amountError is true', async () => { - const resetSpy = vi.spyOn(store, 'reset'); + const resetSpy = vi.spyOn(utxosFeeStore, 'reset'); const selectUtxosFeeSpy = mockBtcSendApi(); mockAuthStore(); @@ -103,7 +101,7 @@ describe('UtxosFeeContext', () => { ...props, amountError: true }, - context: mockContext(store) + context: mockContext(utxosFeeStore) }); await waitFor(() => { @@ -113,7 +111,7 @@ describe('UtxosFeeContext', () => { }); it('should not call selectUtxosFee if no amount provided', async () => { - const resetSpy = vi.spyOn(store, 'reset'); + const resetSpy = vi.spyOn(utxosFeeStore, 'reset'); const selectUtxosFeeSpy = mockBtcSendApi(); const { amount: _, ...newProps } = props; @@ -121,7 +119,7 @@ describe('UtxosFeeContext', () => { render(UtxosFeeContext, { props: newProps, - context: mockContext(store) + context: mockContext(utxosFeeStore) }); await waitFor(() => { @@ -131,14 +129,14 @@ describe('UtxosFeeContext', () => { }); it('should not call selectUtxosFee if provided amount is 0', async () => { - const resetSpy = vi.spyOn(store, 'reset'); + const resetSpy = vi.spyOn(utxosFeeStore, 'reset'); const selectUtxosFeeSpy = mockBtcSendApi(); mockAuthStore(); render(UtxosFeeContext, { props: { ...props, amount: 0 }, - context: mockContext(store) + context: mockContext(utxosFeeStore) }); await waitFor(() => { @@ -148,14 +146,14 @@ describe('UtxosFeeContext', () => { }); it('should not call selectUtxosFee if provided networkId is not BTC', async () => { - const resetSpy = vi.spyOn(store, 'reset'); + const resetSpy = vi.spyOn(utxosFeeStore, 'reset'); const selectUtxosFeeSpy = mockBtcSendApi(); mockAuthStore(); render(UtxosFeeContext, { props: { ...props, networkId: ICP_NETWORK_ID }, - context: mockContext(store) + context: mockContext(utxosFeeStore) }); await waitFor(() => { @@ -163,4 +161,41 @@ describe('UtxosFeeContext', () => { expect(selectUtxosFeeSpy).not.toHaveBeenCalled(); }); }); + + it('should not call selectUtxosFee if provided amount has not changed since last request', async () => { + const selectUtxosFeeSpy = mockBtcSendApi(); + + mockAuthStore(); + + utxosFeeStore.setUtxosFee({ utxosFee: mockUtxosFee, amount }); + + render(UtxosFeeContext, { + props, + context: mockContext(utxosFeeStore) + }); + + await waitFor(() => { + expect(selectUtxosFeeSpy).not.toHaveBeenCalled(); + }); + }); + + it('should call selectUtxosFee if provided amount has changed since last request', async () => { + const selectUtxosFeeSpy = mockBtcSendApi(); + + mockAuthStore(); + + utxosFeeStore.setUtxosFee({ utxosFee: mockUtxosFee, amount }); + + render(UtxosFeeContext, { + props: { + ...props, + amount: amount + 1 + }, + context: mockContext(utxosFeeStore) + }); + + await waitFor(() => { + expect(selectUtxosFeeSpy).toHaveBeenCalled(); + }); + }); }); diff --git a/src/frontend/src/tests/btc/stores/utxos-fee.store.spec.ts b/src/frontend/src/tests/btc/stores/utxos-fee.store.spec.ts index 3b74fe02d6..d0adbf88d7 100644 --- a/src/frontend/src/tests/btc/stores/utxos-fee.store.spec.ts +++ b/src/frontend/src/tests/btc/stores/utxos-fee.store.spec.ts @@ -1,4 +1,4 @@ -import { initUtxosFeeStore } from '$btc/stores/utxos-fee.store'; +import { utxosFeeStore } from '$btc/stores/utxos-fee.store'; import { mockUtxosFee } from '$tests/mocks/btc.mock'; import { mockPage } from '$tests/mocks/page.store.mock'; import { testDerivedUpdates } from '$tests/utils/derived.utils'; @@ -9,34 +9,35 @@ describe('utxosFeeStore', () => { mockPage.reset(); }); - it('should ensure derived stores update at most once when the store changes', async () => { - const store = initUtxosFeeStore(); + const amount = 10; + it('should ensure derived stores update at most once when the store changes', async () => { await testDerivedUpdates(() => - store.setUtxosFee({ - utxosFee: mockUtxosFee + utxosFeeStore.setUtxosFee({ + utxosFee: mockUtxosFee, + amount }) ); }); it('should have all expected values', () => { - const store = initUtxosFeeStore(); - - store.setUtxosFee({ - utxosFee: mockUtxosFee + utxosFeeStore.setUtxosFee({ + utxosFee: mockUtxosFee, + amount }); - expect(get(store)?.utxosFee).toStrictEqual(mockUtxosFee); + expect(get(utxosFeeStore)?.utxosFee).toStrictEqual(mockUtxosFee); + expect(get(utxosFeeStore)?.amount).toStrictEqual(amount); }); it('should reset the value', () => { - const store = initUtxosFeeStore(); - - store.setUtxosFee({ - utxosFee: mockUtxosFee + utxosFeeStore.setUtxosFee({ + utxosFee: mockUtxosFee, + amount }); - store.reset(); + utxosFeeStore.reset(); - expect(get(store)?.utxosFee).toBe(undefined); + expect(get(utxosFeeStore)?.utxosFee).toBe(undefined); + expect(get(utxosFeeStore)?.amount).toBe(undefined); }); });