Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
deuch13 committed Feb 26, 2024
1 parent 2ca0de6 commit b0460ae
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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} */
Expand Down
12 changes: 7 additions & 5 deletions web-wallet/src/lib/components/Send/Send.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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} */
Expand All @@ -63,6 +63,7 @@
let isValidGas = false;
let { gasLimit, gasPrice } = gasSettings;
const { limitLower, limitUpper, priceLower } = $gasStore;
const minAmount = 0.000000001;
Expand Down Expand Up @@ -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;
Expand Down
19 changes: 11 additions & 8 deletions web-wallet/src/lib/components/Stake/Stake.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -78,6 +78,7 @@
let isValidGas = false;
let { gasLimit, gasPrice } = gasSettings;
const { limitLower, limitUpper, priceLower } = $gasStore;
/** @type {Record<StakeType, string>} */
const confirmLabels = {
Expand All @@ -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;
Expand All @@ -112,6 +114,7 @@
if (flow === "stake") {
stakeAmount = Math.min(minStake, stakeAmount);
}
isValidGas = gasStore.areValidSettings(gasPrice, gasLimit);
});
Expand Down Expand Up @@ -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}
/>
</WizardStep>
Expand Down Expand Up @@ -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}
Expand Down
33 changes: 1 addition & 32 deletions web-wallet/src/lib/components/__tests__/GasControls.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -68,7 +68,6 @@ describe("GasControls", () => {

expect(eventHandler).toHaveBeenCalledTimes(1);
expect(eventHandler.mock.lastCall[0].detail).toStrictEqual({
isValidGas: true,
limit: baseProps.limit,
price: 15
});
Expand All @@ -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");
});
});
7 changes: 3 additions & 4 deletions web-wallet/src/lib/components/__tests__/GasSettings.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -23,7 +23,8 @@ describe("GasSettings", () => {
limitLower: 10000000,
limitUpper: 1000000000,
price: 1,
priceLower: 1
priceLower: 1,
gasStore: gasStore
};

const baseOptions = {
Expand Down Expand Up @@ -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
});
Expand All @@ -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
});
Expand Down
8 changes: 3 additions & 5 deletions web-wallet/src/lib/components/__tests__/Send.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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",
Expand Down
8 changes: 3 additions & 5 deletions web-wallet/src/lib/components/__tests__/Stake.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
gasStore,
operationsStore,
settingsStore,
walletStore,
walletStore
} from "$lib/stores";
import {
ContractOperations,
Expand Down
3 changes: 0 additions & 3 deletions web-wallet/src/lib/contracts/contracts.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ type ContractDescriptor = {

type ContractGasSettings = {
gasLimit: number;
gasLimitLower: number;
gasLimitUpper: number;
gasPrice: number;
gasPriceLower: number;
};

type ContractOperation = {
Expand Down
32 changes: 15 additions & 17 deletions web-wallet/src/lib/stores/gasStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
12 changes: 12 additions & 0 deletions web-wallet/src/lib/stores/stores.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<GasStoreContent> & GasStoreService;
5 changes: 3 additions & 2 deletions web-wallet/src/routes/(app)/settings/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -137,7 +137,8 @@
<div class="settings-group__multi-control-content">
<GasControls
on:gasSettings={(event) => {
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;
Expand Down
8 changes: 0 additions & 8 deletions web-wallet/src/routes/(welcome)/login/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b0460ae

Please sign in to comment.