Compare commits

...
30 changed files with 152 additions and 148 deletions
+1 -1
View File
@@ -21,7 +21,7 @@ type NavStore = {
hide: () => void; hide: () => void;
}; };
export const useNavStore = create<NavStore>((set, get) => ({ export const useNavStore = create<NavStore>()((set, get) => ({
open: false, open: false,
toggle: () => set({ open: !get().open }), toggle: () => set({ open: !get().open }),
hide: () => set({ open: false }), hide: () => set({ open: false }),
@@ -9,26 +9,28 @@ export type PendingTxsStore = {
resetPendingTxs: () => void; resetPendingTxs: () => void;
}; };
export const usePendingBalancesStore = create<PendingTxsStore>((set, get) => ({ export const usePendingBalancesStore = create<PendingTxsStore>()(
pendingBalances: [], (set, get) => ({
addPendingTxs: (event: Event[]) => { pendingBalances: [],
set({ addPendingTxs: (event: Event[]) => {
pendingBalances: uniqBy( set({
[...get().pendingBalances, ...event], pendingBalances: uniqBy(
'transactionHash' [...get().pendingBalances, ...event],
), 'transactionHash'
});
},
removePendingTx: (event: Event) => {
set({
pendingBalances: [
...get().pendingBalances.filter(
({ transactionHash }) => transactionHash !== event.transactionHash
), ),
], });
}); },
}, removePendingTx: (event: Event) => {
resetPendingTxs: () => { set({
set({ pendingBalances: [] }); pendingBalances: [
}, ...get().pendingBalances.filter(
})); ({ transactionHash }) => transactionHash !== event.transactionHash
),
],
});
},
resetPendingTxs: () => {
set({ pendingBalances: [] });
},
})
);
@@ -38,7 +38,7 @@ export interface RefreshBalances {
vestingAssociatedBalance: BigNumber; vestingAssociatedBalance: BigNumber;
} }
export const useBalances = create<BalancesStore>((set) => ({ export const useBalances = create<BalancesStore>()((set) => ({
associationBreakdown: { associationBreakdown: {
stakingAssociations: {}, stakingAssociations: {},
vestingAssociations: {}, vestingAssociations: {},
@@ -1,7 +1,7 @@
import { toBigNum } from '@vegaprotocol/utils'; import { toBigNum } from '@vegaprotocol/utils';
import type { TrancheServiceResponse } from '@vegaprotocol/smart-contracts'; import type { TrancheServiceResponse } from '@vegaprotocol/smart-contracts';
import type BigNumber from 'bignumber.js'; import type BigNumber from 'bignumber.js';
import create from 'zustand'; import { create } from 'zustand';
import { ENV } from '../../config'; import { ENV } from '../../config';
export interface Tranche { export interface Tranche {
@@ -36,7 +36,7 @@ export type TranchesStore = {
const secondsToDate = (seconds: number) => new Date(seconds * 1000); const secondsToDate = (seconds: number) => new Date(seconds * 1000);
export const useTranches = create<TranchesStore>((set) => ({ export const useTranches = create<TranchesStore>()((set) => ({
tranches: null, tranches: null,
loading: false, loading: false,
error: null, error: null,
+22 -25
View File
@@ -1,5 +1,4 @@
import type ethers from 'ethers'; import type ethers from 'ethers';
import type { GetState, SetState } from 'zustand';
import { create } from 'zustand'; import { create } from 'zustand';
export interface TxData { export interface TxData {
@@ -16,27 +15,25 @@ interface TransactionStore {
remove: (tx: TxData) => void; remove: (tx: TxData) => void;
} }
export const useTransactionStore = create( export const useTransactionStore = create<TransactionStore>()((set, get) => ({
(set: SetState<TransactionStore>, get: GetState<TransactionStore>) => ({ transactions: [],
transactions: [], add: (tx) => {
add: (tx) => { const { transactions } = get();
const { transactions } = get(); set({ transactions: [...transactions, tx] });
set({ transactions: [...transactions, tx] }); },
}, update: (tx) => {
update: (tx) => { const { transactions } = get();
const { transactions } = get(); set({
set({ transactions: [
transactions: [ ...transactions.filter((t) => t.tx.hash !== tx.tx.hash),
...transactions.filter((t) => t.tx.hash !== tx.tx.hash), tx,
tx, ],
], });
}); },
}, remove: (tx) => {
remove: (tx) => { const { transactions } = get();
const { transactions } = get(); set({
set({ transactions: transactions.filter((t) => t.tx.hash !== tx.tx.hash),
transactions: transactions.filter((t) => t.tx.hash !== tx.tx.hash), });
}); },
}, }));
})
);
+1 -1
View File
@@ -15,7 +15,7 @@ interface PageTitleStore {
updateTitle: (title: string) => void; updateTitle: (title: string) => void;
} }
export const useGlobalStore = create<GlobalStore>((set) => ({ export const useGlobalStore = create<GlobalStore>()((set) => ({
nodeSwitcherDialog: false, nodeSwitcherDialog: false,
marketId: LocalStorage.getItem('marketId') || null, marketId: LocalStorage.getItem('marketId') || null,
shouldDisplayWelcomeDialog: false, shouldDisplayWelcomeDialog: false,
+1 -1
View File
@@ -11,7 +11,7 @@ interface Actions {
open: (open?: boolean) => void; open: (open?: boolean) => void;
} }
export const useTransferDialog = create<State & Actions>((set) => ({ export const useTransferDialog = create<State & Actions>()((set) => ({
isOpen: false, isOpen: false,
open: (open = true) => { open: (open = true) => {
set(() => ({ isOpen: open })); set(() => ({ isOpen: open }));
+1 -1
View File
@@ -9,4 +9,4 @@ type HeaderStore = {
[url: string]: HeaderEntry | undefined; [url: string]: HeaderEntry | undefined;
}; };
export const useHeaderStore = create<HeaderStore>(() => ({})); export const useHeaderStore = create<HeaderStore>()(() => ({}));
+1 -1
View File
@@ -20,7 +20,7 @@ export type AssetDetailsDialogStore = {
open: (id: string, trigger?: HTMLElement | null, asJson?: boolean) => void; open: (id: string, trigger?: HTMLElement | null, asJson?: boolean) => void;
}; };
export const useAssetDetailsDialogStore = create<AssetDetailsDialogStore>( export const useAssetDetailsDialogStore = create<AssetDetailsDialogStore>()(
(set) => ({ (set) => ({
isOpen: false, isOpen: false,
id: '', id: '',
+1 -1
View File
@@ -15,7 +15,7 @@ interface Actions {
close: () => void; close: () => void;
} }
export const useDepositDialog = create<State & Actions>((set) => ({ export const useDepositDialog = create<State & Actions>()((set) => ({
isOpen: false, isOpen: false,
assetId: undefined, assetId: undefined,
open: (assetId) => set(() => ({ assetId, isOpen: true })), open: (assetId) => set(() => ({ assetId, isOpen: true })),
-18
View File
@@ -1,18 +0,0 @@
import { act } from 'react-dom/test-utils';
const zu = jest.requireActual('zustand'); // if using jest
// a variable to hold reset functions for all stores declared in the app
const storeResetFns = new Set();
// when creating a store, we get its initial state, create a reset function and add it in the set
export const create = (createState) => {
const store = zu.create(createState);
const initialState = store.getState();
storeResetFns.add(() => store.setState(initialState, true));
return store;
};
// Reset all stores after each test run
beforeEach(() => {
act(() => storeResetFns.forEach((resetFn) => resetFn()));
});
+21
View File
@@ -0,0 +1,21 @@
import type { StateCreator } from 'zustand';
import { act } from 'react-dom/test-utils';
const { create: actualCreate } = jest.requireActual('zustand'); // if using jest
// a variable to hold reset functions for all stores declared in the app
const storeResetFns = new Set<() => void>();
// when creating a store, we get its initial state, create a reset function and add it in the set
export const create =
() =>
<S>(createState: StateCreator<S>) => {
const store = actualCreate(createState);
const initialState = store.getState();
storeResetFns.add(() => store.setState(initialState, true));
return store;
};
// Reset all stores after each test run
beforeEach(() => {
act(() => storeResetFns.forEach((resetFn) => resetFn()));
});
@@ -34,7 +34,7 @@ export type EnvStore = Env & Actions;
export const STORAGE_KEY = 'vega_url'; export const STORAGE_KEY = 'vega_url';
const SUBSCRIPTION_TIMEOUT = 3000; const SUBSCRIPTION_TIMEOUT = 3000;
export const useEnvironment = create<EnvStore>((set, get) => ({ export const useEnvironment = create<EnvStore>()((set, get) => ({
...compileEnvVars(), ...compileEnvVars(),
nodes: [], nodes: [],
status: 'default', status: 'default',
+2 -1
View File
@@ -17,7 +17,8 @@
"**/*.test.js", "**/*.test.js",
"**/*.spec.jsx", "**/*.spec.jsx",
"**/*.test.jsx", "**/*.test.jsx",
"jest.config.ts" "jest.config.ts",
"__mocks__"
], ],
"include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"] "include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"]
} }
@@ -77,7 +77,7 @@ const randomToast = (): Toast => {
}; };
type PriceStore = { price: number; setPrice: (p: number) => void }; type PriceStore = { price: number; setPrice: (p: number) => void };
const usePrice = create<PriceStore>((set) => ({ const usePrice = create<PriceStore>()((set) => ({
price: 0, price: 0,
setPrice: (p) => set((state) => ({ price: p })), setPrice: (p) => set((state) => ({ price: p })),
})); }));
@@ -47,8 +47,8 @@ type Actions = {
type ToastsStore = State & Actions; type ToastsStore = State & Actions;
export const useToasts = create( export const useToasts = create<ToastsStore>()(
immer<ToastsStore>((set, get) => ({ immer((set) => ({
toasts: {}, toasts: {},
count: 0, count: 0,
add: (toast) => add: (toast) =>
-18
View File
@@ -1,18 +0,0 @@
import { act } from 'react-dom/test-utils';
const zu = jest.requireActual('zustand'); // if using jest
// a variable to hold reset functions for all stores declared in the app
const storeResetFns = new Set();
// when creating a store, we get its initial state, create a reset function and add it in the set
export const create = (createState) => {
const store = zu.create(createState);
const initialState = store.getState();
storeResetFns.add(() => store.setState(initialState, true));
return store;
};
// Reset all stores after each test run
beforeEach(() => {
act(() => storeResetFns.forEach((resetFn) => resetFn()));
});
+21
View File
@@ -0,0 +1,21 @@
import type { StateCreator } from 'zustand';
import { act } from 'react-dom/test-utils';
const { create: actualCreate } = jest.requireActual('zustand'); // if using jest
// a variable to hold reset functions for all stores declared in the app
const storeResetFns = new Set<() => void>();
// when creating a store, we get its initial state, create a reset function and add it in the set
export const create =
() =>
<S>(createState: StateCreator<S>) => {
const store = actualCreate(createState);
const initialState = store.getState();
storeResetFns.add(() => store.setState(initialState, true));
return store;
};
// Reset all stores after each test run
beforeEach(() => {
act(() => storeResetFns.forEach((resetFn) => resetFn()));
});
@@ -8,8 +8,11 @@ import {
import type { MockedResponse } from '@apollo/client/testing'; import type { MockedResponse } from '@apollo/client/testing';
import { MockedProvider } from '@apollo/client/testing'; import { MockedProvider } from '@apollo/client/testing';
import { VegaWalletProvider } from '../provider'; import { VegaWalletProvider } from '../provider';
import { VegaConnectDialog, CLOSE_DELAY } from './connect-dialog'; import {
import type { VegaWalletDialogStore } from './connect-dialog'; VegaConnectDialog,
CLOSE_DELAY,
useVegaWalletDialogStore,
} from './connect-dialog';
import type { VegaConnectDialogProps } from '..'; import type { VegaConnectDialogProps } from '..';
import { import {
ClientErrors, ClientErrors,
@@ -24,11 +27,6 @@ import { ChainIdDocument } from '@vegaprotocol/react-helpers';
const mockUpdateDialogOpen = jest.fn(); const mockUpdateDialogOpen = jest.fn();
const mockCloseVegaDialog = jest.fn(); const mockCloseVegaDialog = jest.fn();
const mockStoreObj: Partial<VegaWalletDialogStore> = {
updateVegaWalletDialog: mockUpdateDialogOpen,
closeVegaWalletDialog: mockCloseVegaDialog,
vegaWalletDialogOpen: true,
};
jest.mock('@vegaprotocol/environment'); jest.mock('@vegaprotocol/environment');
@@ -44,11 +42,6 @@ useEnvironment.mockImplementation(() => ({
HOSTED_WALLET_URL: mockHostedWalletUrl, HOSTED_WALLET_URL: mockHostedWalletUrl,
})); }));
jest.mock('zustand', () => ({
create: () => (storeGetter: (store: VegaWalletDialogStore) => unknown) =>
storeGetter(mockStoreObj as VegaWalletDialogStore),
}));
let defaultProps: VegaConnectDialogProps; let defaultProps: VegaConnectDialogProps;
const INITIAL_KEY = 'some-key'; const INITIAL_KEY = 'some-key';
@@ -66,6 +59,12 @@ beforeEach(() => {
defaultProps = { defaultProps = {
connectors, connectors,
}; };
useVegaWalletDialogStore.setState({
updateVegaWalletDialog: mockUpdateDialogOpen,
closeVegaWalletDialog: mockCloseVegaDialog,
vegaWalletDialogOpen: true,
});
}); });
const mockVegaWalletUrl = 'http://mock.wallet.com'; const mockVegaWalletUrl = 'http://mock.wallet.com';
@@ -37,7 +37,7 @@ export interface VegaConnectDialogProps {
onChangeOpen?: (open: boolean) => void; onChangeOpen?: (open: boolean) => void;
} }
export const useVegaWalletDialogStore = create<VegaWalletDialogStore>( export const useVegaWalletDialogStore = create<VegaWalletDialogStore>()(
(set) => ({ (set) => ({
vegaWalletDialogOpen: false, vegaWalletDialogOpen: false,
updateVegaWalletDialog: (open: boolean) => updateVegaWalletDialog: (open: boolean) =>
@@ -52,8 +52,8 @@ export interface VegaTransactionStore {
) => void; ) => void;
} }
export const useVegaTransactionStore = create( export const useVegaTransactionStore = create<VegaTransactionStore>()(
subscribeWithSelector<VegaTransactionStore>((set, get) => ({ subscribeWithSelector((set, get) => ({
transactions: [] as VegaStoredTxState[], transactions: [] as VegaStoredTxState[],
create: (body: Transaction, order?: OrderTxUpdateFieldsFragment) => { create: (body: Transaction, order?: OrderTxUpdateFieldsFragment) => {
const transactions = get().transactions; const transactions = get().transactions;
+2 -1
View File
@@ -17,7 +17,8 @@
"**/*.test.js", "**/*.test.js",
"**/*.spec.jsx", "**/*.spec.jsx",
"**/*.test.jsx", "**/*.test.jsx",
"jest.config.ts" "jest.config.ts",
"__mocks__"
], ],
"include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"] "include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"]
} }
-27
View File
@@ -1,27 +0,0 @@
import { act } from '@testing-library/react';
const zu = jest.requireActual('zustand'); // if using jest
// a variable to hold reset functions for all stores declared in the app
const storeResetFns = new Set();
// when creating a store, we get its initial state, create a reset function and add it in the set
export const create = (createState) => {
let store;
if (typeof createState === 'function') {
store = zu.create(createState);
} else {
store = (selector, equalityFn) =>
zu.useStore(createState, selector, equalityFn);
Object.assign(store, createState);
}
const initialState = store.getState();
storeResetFns.add(() => store.setState(initialState, true));
return store;
};
// Reset all stores after each test run
beforeEach(() => {
act(() => storeResetFns.forEach((resetFn) => resetFn()));
});
export default create;
+24
View File
@@ -0,0 +1,24 @@
import type { StateCreator } from 'zustand';
import { act } from 'react-dom/test-utils';
const { create: actualCreate } = jest.requireActual('zustand'); // if using jest
// a variable to hold reset functions for all stores declared in the app
const storeResetFns = new Set<() => void>();
// when creating a store, we get its initial state, create a reset function and add it in the set
export const create =
() =>
<S>(createState: StateCreator<S>) => {
const store = actualCreate(createState);
const initialState = store.getState();
storeResetFns.add(() => store.setState(initialState, true));
return store;
};
// Reset all stores after each test run
beforeEach(() => {
act(() => storeResetFns.forEach((resetFn) => resetFn()));
});
// also export default as web3-react internals import and use zustand as the default import
export default create;
@@ -55,8 +55,8 @@ export interface EthTransactionStore {
delete: (index: number) => void; delete: (index: number) => void;
} }
export const useEthTransactionStore = create( export const useEthTransactionStore = create<EthTransactionStore>()(
subscribeWithSelector<EthTransactionStore>((set, get) => ({ subscribeWithSelector((set, get) => ({
transactions: [] as EthStoredTxState[], transactions: [] as EthStoredTxState[],
create: ( create: (
contract: Contract | null, contract: Contract | null,
@@ -49,8 +49,8 @@ export interface EthWithdrawApprovalStore {
delete: (index: number) => void; delete: (index: number) => void;
} }
export const useEthWithdrawApprovalsStore = create( export const useEthWithdrawApprovalsStore = create<EthWithdrawApprovalStore>()(
subscribeWithSelector<EthWithdrawApprovalStore>((set, get) => ({ subscribeWithSelector((set, get) => ({
transactions: [] as EthWithdrawalApprovalState[], transactions: [] as EthWithdrawalApprovalState[],
create: ( create: (
withdrawal: EthWithdrawalApprovalState['withdrawal'], withdrawal: EthWithdrawalApprovalState['withdrawal'],
+1 -1
View File
@@ -13,7 +13,7 @@ interface Actions {
close: () => void; close: () => void;
} }
export const useWeb3ConnectStore = create<State & Actions>((set) => ({ export const useWeb3ConnectStore = create<State & Actions>()((set) => ({
isOpen: false, isOpen: false,
connectors: [], connectors: [],
initialize: (connectors, desiredChainId) => { initialize: (connectors, desiredChainId) => {
+2 -1
View File
@@ -17,7 +17,8 @@
"**/*.test.js", "**/*.test.js",
"**/*.spec.jsx", "**/*.spec.jsx",
"**/*.test.jsx", "**/*.test.jsx",
"jest.config.ts" "jest.config.ts",
"__mocks__"
], ],
"include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"] "include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"]
} }
+1 -1
View File
@@ -11,7 +11,7 @@ export interface WithdrawStore {
update: (state: Partial<WithdrawStore>) => void; update: (state: Partial<WithdrawStore>) => void;
} }
export const useWithdrawStore = create<WithdrawStore>((set) => ({ export const useWithdrawStore = create<WithdrawStore>()((set) => ({
asset: undefined, asset: undefined,
balance: new BigNumber(0), balance: new BigNumber(0),
min: new BigNumber(0), min: new BigNumber(0),
+1 -1
View File
@@ -17,7 +17,7 @@ interface Actions {
close: () => void; close: () => void;
} }
export const useWithdrawalDialog = create<State & Actions>((set) => ({ export const useWithdrawalDialog = create<State & Actions>()((set) => ({
isOpen: false, isOpen: false,
assetId: undefined, assetId: undefined,
open: (assetId) => set(() => ({ assetId, isOpen: true })), open: (assetId) => set(() => ({ assetId, isOpen: true })),