From b0460aed3fd76f4836a7a557dcd11dbddc2ec46c Mon Sep 17 00:00:00 2001 From: Alex Panturu Date: Mon, 26 Feb 2024 13:43:09 +0200 Subject: [PATCH] update tests --- .../components/GasSettings/GasSettings.svelte | 2 +- .../src/lib/components/Send/Send.svelte | 12 ++++--- .../src/lib/components/Stake/Stake.svelte | 19 ++++++----- .../components/__tests__/GasControls.spec.js | 33 +------------------ .../components/__tests__/GasSettings.spec.js | 7 ++-- .../src/lib/components/__tests__/Send.spec.js | 8 ++--- .../lib/components/__tests__/Stake.spec.js | 8 ++--- .../TransferContract/TransferContract.svelte | 2 +- web-wallet/src/lib/contracts/contracts.d.ts | 3 -- web-wallet/src/lib/stores/gasStore.js | 32 +++++++++--------- web-wallet/src/lib/stores/stores.d.ts | 12 +++++++ .../src/routes/(app)/settings/+page.svelte | 5 +-- .../src/routes/(welcome)/login/+page.svelte | 8 ----- 13 files changed, 60 insertions(+), 91 deletions(-) diff --git a/web-wallet/src/lib/components/GasSettings/GasSettings.svelte b/web-wallet/src/lib/components/GasSettings/GasSettings.svelte index f71586d08..5d4ad2fbe 100644 --- a/web-wallet/src/lib/components/GasSettings/GasSettings.svelte +++ b/web-wallet/src/lib/components/GasSettings/GasSettings.svelte @@ -22,7 +22,7 @@ /** @type {string} */ export let fee; - /** @type {import("svelte/store").Readable<*>}*/ + /** @type {import("$lib/stores/stores").GasStore} */ export let gasStore; /** @type {boolean} */ diff --git a/web-wallet/src/lib/components/Send/Send.svelte b/web-wallet/src/lib/components/Send/Send.svelte index 41fe8d6a7..70b35c302 100644 --- a/web-wallet/src/lib/components/Send/Send.svelte +++ b/web-wallet/src/lib/components/Send/Send.svelte @@ -40,7 +40,7 @@ /** @type {ContractStatus[]} */ export let statuses; - /** @type {import("svelte/store").Readable<*>}*/ + /** @type {import("$lib/stores/stores").GasStore} */ export let gasStore; /** @type {number} */ @@ -63,6 +63,7 @@ let isValidGas = false; let { gasLimit, gasPrice } = gasSettings; + const { limitLower, limitUpper, priceLower } = $gasStore; const minAmount = 0.000000001; @@ -133,12 +134,13 @@ {fee} {gasStore} limit={gasSettings.gasLimit} - limitLower={$gasStore.limitLower} - limitUpper={$gasStore.limitUpper} + limitLower={limitLower} + limitUpper={limitUpper} price={gasSettings.gasPrice} - priceLower={$gasStore.priceLower} + priceLower={priceLower} on:gasSettings={(event) => { - isValidGas = gasStore.areValidSettings(event.detail.price, event.detail.limit) + isValidGas = gasStore.areValidSettings(event.detail.price, event.detail.limit); + if (isValidGas) { gasPrice = event.detail.price; gasLimit = event.detail.limit; diff --git a/web-wallet/src/lib/components/Stake/Stake.svelte b/web-wallet/src/lib/components/Stake/Stake.svelte index 200a4668b..5d8729bb9 100644 --- a/web-wallet/src/lib/components/Stake/Stake.svelte +++ b/web-wallet/src/lib/components/Stake/Stake.svelte @@ -58,7 +58,7 @@ /** @type {boolean} */ export let hideStakingNotice; - /** @type {import("svelte/store").Readable<*>}*/ + /** @type {import("$lib/stores/stores").GasStore} */ export let gasStore; const defaultMinStake = 1000; @@ -78,6 +78,7 @@ let isValidGas = false; let { gasLimit, gasPrice } = gasSettings; + const { limitLower, limitUpper, priceLower } = $gasStore; /** @type {Record} */ const confirmLabels = { @@ -101,7 +102,8 @@ * @param {{detail:{price:number, limit:number, isValidGas:boolean}}} event */ const setGasValues = (event) => { - isValidGas = gasStore.areValidSettings(event.detail.price, event.detail.limit) + isValidGas = gasStore.areValidSettings(event.detail.price, event.detail.limit); + if (isValidGas) { gasPrice = event.detail.price; gasLimit = event.detail.limit; @@ -112,6 +114,7 @@ if (flow === "stake") { stakeAmount = Math.min(minStake, stakeAmount); } + isValidGas = gasStore.areValidSettings(gasPrice, gasLimit); }); @@ -225,10 +228,10 @@ {fee} {gasStore} limit={gasSettings.gasLimit} - limitLower={gasSettings.gasLimitLower} - limitUpper={gasSettings.gasLimitUpper} + limitLower={limitLower} + limitUpper={limitUpper} price={gasSettings.gasPrice} - priceLower={gasSettings.gasPriceLower} + priceLower={priceLower} on:gasSettings={setGasValues} /> @@ -270,10 +273,10 @@ {fee} {gasStore} limit={gasSettings.gasLimit} - limitLower={gasSettings.gasLimitLower} - limitUpper={gasSettings.gasLimitUpper} + limitLower={limitLower} + limitUpper={limitUpper} price={gasSettings.gasPrice} - priceLower={gasSettings.gasPriceLower} + priceLower={priceLower} on:gasSettings={setGasValues} /> {/if} diff --git a/web-wallet/src/lib/components/__tests__/GasControls.spec.js b/web-wallet/src/lib/components/__tests__/GasControls.spec.js index 803459922..3668358c7 100644 --- a/web-wallet/src/lib/components/__tests__/GasControls.spec.js +++ b/web-wallet/src/lib/components/__tests__/GasControls.spec.js @@ -57,7 +57,7 @@ describe("GasControls", () => { expect(container).toMatchSnapshot(); }); - it("should dispatch a \"gasSettings\" event when the price or the limit are changed with valid gas settings", async () => { + it("should dispatch a \"gasSettings\" event when the price or the limit are changed", async () => { const { component, getByLabelText } = render(GasControls, baseOptions); const priceInput = asInput(getByLabelText(/price/i)); const limitInput = asInput(getByLabelText(/limit/i)); @@ -68,7 +68,6 @@ describe("GasControls", () => { expect(eventHandler).toHaveBeenCalledTimes(1); expect(eventHandler.mock.lastCall[0].detail).toStrictEqual({ - isValidGas: true, limit: baseProps.limit, price: 15 }); @@ -78,40 +77,10 @@ describe("GasControls", () => { expect(eventHandler).toHaveBeenCalledTimes(2); expect(eventHandler.mock.lastCall[0].detail).toStrictEqual({ - isValidGas: true, limit: 25, price: 15 }); expect(limitInput.valueAsNumber).toBe(25); expect(priceInput.max).toBe("25"); }); - - it("should dispatch a \"gasSettings\" event when the price or the limit are changed with invalid gas settings", async () => { - const { component, getByLabelText } = render(GasControls, baseOptions); - const priceInput = asInput(getByLabelText(/price/i)); - const limitInput = asInput(getByLabelText(/limit/i)); - - component.$on("gasSettings", eventHandler); - - await fireInput(priceInput, 25); - - expect(eventHandler).toHaveBeenCalledTimes(1); - expect(eventHandler.mock.lastCall[0].detail).toStrictEqual({ - isValidGas: false, - limit: baseProps.limit, - price: 25 - }); - expect(priceInput.valueAsNumber).toBe(25); - - await fireInput(limitInput, 105); - - expect(eventHandler).toHaveBeenCalledTimes(2); - expect(eventHandler.mock.lastCall[0].detail).toStrictEqual({ - isValidGas: false, - limit: 105, - price: 25 - }); - expect(limitInput.valueAsNumber).toBe(105); - expect(priceInput.max).toBe("105"); - }); }); diff --git a/web-wallet/src/lib/components/__tests__/GasSettings.spec.js b/web-wallet/src/lib/components/__tests__/GasSettings.spec.js index 040d647ae..363458a6f 100644 --- a/web-wallet/src/lib/components/__tests__/GasSettings.spec.js +++ b/web-wallet/src/lib/components/__tests__/GasSettings.spec.js @@ -9,7 +9,7 @@ import { cleanup, fireEvent, render } from "@testing-library/svelte"; import { GasSettings } from ".."; import { get } from "svelte/store"; -import { settingsStore } from "$lib/stores"; +import { gasStore, settingsStore } from "$lib/stores"; import { createCurrencyFormatter } from "$lib/dusk/currency"; describe("GasSettings", () => { @@ -23,7 +23,8 @@ describe("GasSettings", () => { limitLower: 10000000, limitUpper: 1000000000, price: 1, - priceLower: 1 + priceLower: 1, + gasStore: gasStore }; const baseOptions = { @@ -66,7 +67,6 @@ describe("GasSettings", () => { expect(eventHandler).toHaveBeenCalledTimes(1); expect(eventHandler.mock.lastCall[0].detail).toStrictEqual({ - isValidGas: true, limit: baseProps.limitLower, price: baseProps.price }); @@ -75,7 +75,6 @@ describe("GasSettings", () => { expect(eventHandler).toHaveBeenCalledTimes(2); expect(eventHandler.mock.lastCall[0].detail).toStrictEqual({ - isValidGas: true, limit: baseProps.limitLower, price: baseProps.price * 2 }); diff --git a/web-wallet/src/lib/components/__tests__/Send.spec.js b/web-wallet/src/lib/components/__tests__/Send.spec.js index 6c3a22f1d..2e296c7e3 100644 --- a/web-wallet/src/lib/components/__tests__/Send.spec.js +++ b/web-wallet/src/lib/components/__tests__/Send.spec.js @@ -7,7 +7,7 @@ import { vi } from "vitest"; import { cleanup, fireEvent, render } from "@testing-library/svelte"; - +import { gasStore } from "$lib/stores"; import { deductLuxFeeFrom } from "$lib/contracts"; import { createCurrencyFormatter } from "$lib/dusk/currency"; import { getAsHTMLElement } from "$lib/dusk/test-helpers"; @@ -23,11 +23,9 @@ describe("Send", () => { formatter, gasSettings: { gasLimit: 20000000, - gasLimitLower: 10000000, - gasLimitUpper: 1000000000, - gasPrice: 1, - gasPriceLower: 1 + gasPrice: 1 }, + gasStore: gasStore, spendable: 1000, statuses: [{ label: "Spendable", diff --git a/web-wallet/src/lib/components/__tests__/Stake.spec.js b/web-wallet/src/lib/components/__tests__/Stake.spec.js index 2bcda32f8..72e5edf52 100644 --- a/web-wallet/src/lib/components/__tests__/Stake.spec.js +++ b/web-wallet/src/lib/components/__tests__/Stake.spec.js @@ -7,7 +7,7 @@ import { vi } from "vitest"; import { cleanup, fireEvent, render } from "@testing-library/svelte"; - +import { gasStore } from "$lib/stores"; import { deductLuxFeeFrom } from "$lib/contracts"; import { createCurrencyFormatter } from "$lib/dusk/currency"; @@ -32,11 +32,9 @@ describe("Stake", () => { formatter, gasSettings: { gasLimit: 20000000, - gasLimitLower: 10000000, - gasLimitUpper: 1000000000, - gasPrice: 1, - gasPriceLower: 1 + gasPrice: 1 }, + gasStore: gasStore, hideStakingNotice: true, rewards: 345, spendable: 10000, diff --git a/web-wallet/src/lib/containers/TransferContract/TransferContract.svelte b/web-wallet/src/lib/containers/TransferContract/TransferContract.svelte index 7a6f64734..e499f3e3d 100644 --- a/web-wallet/src/lib/containers/TransferContract/TransferContract.svelte +++ b/web-wallet/src/lib/containers/TransferContract/TransferContract.svelte @@ -18,7 +18,7 @@ gasStore, operationsStore, settingsStore, - walletStore, + walletStore } from "$lib/stores"; import { ContractOperations, diff --git a/web-wallet/src/lib/contracts/contracts.d.ts b/web-wallet/src/lib/contracts/contracts.d.ts index e7034a6f8..17997f397 100644 --- a/web-wallet/src/lib/contracts/contracts.d.ts +++ b/web-wallet/src/lib/contracts/contracts.d.ts @@ -7,10 +7,7 @@ type ContractDescriptor = { type ContractGasSettings = { gasLimit: number; - gasLimitLower: number; - gasLimitUpper: number; gasPrice: number; - gasPriceLower: number; }; type ContractOperation = { diff --git a/web-wallet/src/lib/stores/gasStore.js b/web-wallet/src/lib/stores/gasStore.js index c1d1e71a2..31e77df17 100644 --- a/web-wallet/src/lib/stores/gasStore.js +++ b/web-wallet/src/lib/stores/gasStore.js @@ -4,32 +4,30 @@ import { isType } from "lamb"; const gasSettings = { limitLower: Number(parseInt(import.meta.env.VITE_GAS_LIMIT_LOWER, 10)), limitUpper: Number(parseInt(import.meta.env.VITE_GAS_LIMIT_UPPER, 10)), - priceLower: Number(parseInt(import.meta.env.VITE_GAS_PRICE_LOWER, 10)), + priceLower: Number(parseInt(import.meta.env.VITE_GAS_PRICE_LOWER, 10)) }; const gasStore = readable(gasSettings); const { subscribe } = gasStore; /** - * - * @param {Number} price - * @param {Number} limit + * + * @param {Number} price + * @param {Number} limit * @returns {Boolean} */ const areValidSettings = (price, limit) => { - let isValidPrice = false; - let isValidLimit = false; - let isValidGas = false; + let isValidPrice = false; + let isValidLimit = false; + let isValidGas = false; - if ([price, limit].every(isType("Number"))) { - isValidPrice = price >= gasSettings.priceLower && price <= limit; - isValidLimit = limit >= gasSettings.limitLower && limit <= gasSettings.limitUpper; + if ([price, limit].every(isType("Number"))) { + isValidPrice = price >= gasSettings.priceLower && price <= limit; + isValidLimit = limit >= gasSettings.limitLower && limit <= gasSettings.limitUpper; + isValidGas = isValidPrice && isValidLimit; + } - console.log(price, limit, isValidPrice, isValidPrice) - isValidGas = isValidPrice && isValidLimit; - } - - return isValidGas; -} + return isValidGas; +}; -export default {areValidSettings, subscribe}; +export default { areValidSettings, subscribe }; diff --git a/web-wallet/src/lib/stores/stores.d.ts b/web-wallet/src/lib/stores/stores.d.ts index db6964132..c46ae68ce 100644 --- a/web-wallet/src/lib/stores/stores.d.ts +++ b/web-wallet/src/lib/stores/stores.d.ts @@ -62,3 +62,15 @@ type SettingsStore = { network: string; userId: string; }; + +type GasStoreContent = { + limitLower: number, + limitUpper: number, + priceLower: number +} + +type GasStoreService = { + areValidSettings: (gasPrice:number, gasLimit:number) => boolean; +} + +type GasStore = Readable & GasStoreService; diff --git a/web-wallet/src/routes/(app)/settings/+page.svelte b/web-wallet/src/routes/(app)/settings/+page.svelte index a1650293f..f39d1d6c2 100644 --- a/web-wallet/src/routes/(app)/settings/+page.svelte +++ b/web-wallet/src/routes/(app)/settings/+page.svelte @@ -23,7 +23,7 @@ } from "$lib/dusk/components"; import { GasControls } from "$lib/components"; import { currencies } from "$lib/dusk/currency"; - import { settingsStore, gasStore, walletStore } from "$lib/stores"; + import { gasStore, settingsStore, walletStore } from "$lib/stores"; const resetWallet = () => walletStore.clearLocalData().then(() => { settingsStore.reset(); @@ -137,7 +137,8 @@
{ - isValidGas = gasStore.areValidSettings(event.detail.price, event.detail.limit) + isValidGas = gasStore.areValidSettings(event.detail.price, event.detail.limit); + if (isValidGas) { settingsStore.update(store => { store.gasLimit = event.detail.limit; diff --git a/web-wallet/src/routes/(welcome)/login/+page.svelte b/web-wallet/src/routes/(welcome)/login/+page.svelte index d35475445..84924895e 100644 --- a/web-wallet/src/routes/(welcome)/login/+page.svelte +++ b/web-wallet/src/routes/(welcome)/login/+page.svelte @@ -40,14 +40,6 @@ await wallet.reset(); settingsStore.reset(); settingsStore.update(setKey("userId", defaultAddress)); - } else { - settingsStore.update(store => { - store.gasLimitLower = parseInt(import.meta.env.VITE_GAS_LIMIT_LOWER, 10); - store.gasLimitUpper = parseInt(import.meta.env.VITE_GAS_LIMIT_UPPER, 10); - store.gasPriceLower = parseInt(import.meta.env.VITE_GAS_PRICE_LOWER, 10); - - return store; - }); } return wallet;