@ledgerhq/live-common 21.5.1 → 21.7.1-patch.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/env.js +1 -1
- package/lib/env.js.map +1 -1
- package/lib/exchange/swap/getExchangeRates.d.ts.map +1 -1
- package/lib/exchange/swap/getExchangeRates.js +9 -8
- package/lib/exchange/swap/getExchangeRates.js.map +1 -1
- package/lib/exchange/swap/hooks/index.d.ts +11 -0
- package/lib/exchange/swap/hooks/index.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/index.js +23 -0
- package/lib/exchange/swap/hooks/index.js.map +1 -0
- package/lib/exchange/swap/hooks/useFromState.d.ts +15 -0
- package/lib/exchange/swap/hooks/useFromState.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useFromState.js +67 -0
- package/lib/exchange/swap/hooks/useFromState.js.map +1 -0
- package/lib/exchange/swap/hooks/useFromState.test.d.ts +2 -0
- package/lib/exchange/swap/hooks/useFromState.test.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useFromState.test.js +141 -0
- package/lib/exchange/swap/hooks/useFromState.test.js.map +1 -0
- package/lib/exchange/swap/hooks/usePickDefaultAccount.d.ts +5 -0
- package/lib/exchange/swap/hooks/usePickDefaultAccount.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/usePickDefaultAccount.js +37 -0
- package/lib/exchange/swap/hooks/usePickDefaultAccount.js.map +1 -0
- package/lib/exchange/swap/hooks/usePickDefaultAccount.test.d.ts +2 -0
- package/lib/exchange/swap/hooks/usePickDefaultAccount.test.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/usePickDefaultAccount.test.js +185 -0
- package/lib/exchange/swap/hooks/usePickDefaultAccount.test.js.map +1 -0
- package/lib/exchange/swap/hooks/usePickDefaultCurrency.d.ts +3 -0
- package/lib/exchange/swap/hooks/usePickDefaultCurrency.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/usePickDefaultCurrency.js +17 -0
- package/lib/exchange/swap/hooks/usePickDefaultCurrency.js.map +1 -0
- package/lib/exchange/swap/hooks/usePickDefaultCurrency.test.d.ts +2 -0
- package/lib/exchange/swap/hooks/usePickDefaultCurrency.test.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/usePickDefaultCurrency.test.js +73 -0
- package/lib/exchange/swap/hooks/usePickDefaultCurrency.test.js.map +1 -0
- package/lib/exchange/swap/hooks/usePollKYCStatus.d.ts +12 -0
- package/lib/exchange/swap/hooks/usePollKYCStatus.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/usePollKYCStatus.js +105 -0
- package/lib/exchange/swap/hooks/usePollKYCStatus.js.map +1 -0
- package/lib/exchange/swap/hooks/usePollKYCStatus.test.d.ts +2 -0
- package/lib/exchange/swap/hooks/usePollKYCStatus.test.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/usePollKYCStatus.test.js +78 -0
- package/lib/exchange/swap/hooks/usePollKYCStatus.test.js.map +1 -0
- package/lib/exchange/swap/hooks/useProviderRates.d.ts +20 -0
- package/lib/exchange/swap/hooks/useProviderRates.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useProviderRates.js +174 -0
- package/lib/exchange/swap/hooks/useProviderRates.js.map +1 -0
- package/lib/exchange/swap/hooks/useProviderRates.test.d.ts +2 -0
- package/lib/exchange/swap/hooks/useProviderRates.test.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useProviderRates.test.js +261 -0
- package/lib/exchange/swap/hooks/useProviderRates.test.js.map +1 -0
- package/lib/exchange/swap/hooks/useReverseAccounts.d.ts +15 -0
- package/lib/exchange/swap/hooks/useReverseAccounts.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useReverseAccounts.js +35 -0
- package/lib/exchange/swap/hooks/useReverseAccounts.js.map +1 -0
- package/lib/exchange/swap/hooks/useReverseAccounts.test.d.ts +2 -0
- package/lib/exchange/swap/hooks/useReverseAccounts.test.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useReverseAccounts.test.js +79 -0
- package/lib/exchange/swap/hooks/useReverseAccounts.test.js.map +1 -0
- package/lib/exchange/swap/hooks/useSelectableCurrencies.d.ts +5 -0
- package/lib/exchange/swap/hooks/useSelectableCurrencies.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useSelectableCurrencies.js +43 -0
- package/lib/exchange/swap/hooks/useSelectableCurrencies.js.map +1 -0
- package/lib/exchange/swap/hooks/useSelectableCurrencies.test.d.ts +2 -0
- package/lib/exchange/swap/hooks/useSelectableCurrencies.test.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useSelectableCurrencies.test.js +32 -0
- package/lib/exchange/swap/hooks/useSelectableCurrencies.test.js.map +1 -0
- package/lib/exchange/swap/hooks/useSwapProviders.d.ts +14 -0
- package/lib/exchange/swap/hooks/useSwapProviders.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useSwapProviders.js +104 -0
- package/lib/exchange/swap/hooks/useSwapProviders.js.map +1 -0
- package/lib/exchange/swap/hooks/useSwapProviders.test.d.ts +2 -0
- package/lib/exchange/swap/hooks/useSwapProviders.test.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useSwapProviders.test.js +174 -0
- package/lib/exchange/swap/hooks/useSwapProviders.test.js.map +1 -0
- package/lib/exchange/swap/hooks/useSwapTransaction.d.ts +53 -0
- package/lib/exchange/swap/hooks/useSwapTransaction.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useSwapTransaction.js +109 -0
- package/lib/exchange/swap/hooks/useSwapTransaction.js.map +1 -0
- package/lib/exchange/swap/hooks/useSwapTransaction.test.d.ts +2 -0
- package/lib/exchange/swap/hooks/useSwapTransaction.test.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useSwapTransaction.test.js +50 -0
- package/lib/exchange/swap/hooks/useSwapTransaction.test.js.map +1 -0
- package/lib/exchange/swap/hooks/useToState.d.ts +12 -0
- package/lib/exchange/swap/hooks/useToState.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useToState.js +73 -0
- package/lib/exchange/swap/hooks/useToState.js.map +1 -0
- package/lib/exchange/swap/hooks/useToState.test.d.ts +2 -0
- package/lib/exchange/swap/hooks/useToState.test.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useToState.test.js +107 -0
- package/lib/exchange/swap/hooks/useToState.test.js.map +1 -0
- package/lib/exchange/swap/hooks/useUpdateMaxAmount.d.ts +15 -0
- package/lib/exchange/swap/hooks/useUpdateMaxAmount.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useUpdateMaxAmount.js +106 -0
- package/lib/exchange/swap/hooks/useUpdateMaxAmount.js.map +1 -0
- package/lib/exchange/swap/hooks/useUpdateMaxAmount.test.d.ts +2 -0
- package/lib/exchange/swap/hooks/useUpdateMaxAmount.test.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useUpdateMaxAmount.test.js +217 -0
- package/lib/exchange/swap/hooks/useUpdateMaxAmount.test.js.map +1 -0
- package/lib/exchange/swap/mock.d.ts +6 -2
- package/lib/exchange/swap/mock.d.ts.map +1 -1
- package/lib/exchange/swap/mock.js +21 -6
- package/lib/exchange/swap/mock.js.map +1 -1
- package/lib/exchange/swap/types.d.ts +1 -1
- package/lib/exchange/swap/types.d.ts.map +1 -1
- package/lib/exchange/swap/utils/index.d.ts +16 -0
- package/lib/exchange/swap/utils/index.d.ts.map +1 -0
- package/lib/exchange/swap/utils/index.js +49 -0
- package/lib/exchange/swap/utils/index.js.map +1 -0
- package/lib/exchange/swap/utils/index.test.d.ts +2 -0
- package/lib/exchange/swap/utils/index.test.d.ts.map +1 -0
- package/lib/exchange/swap/utils/index.test.js +238 -0
- package/lib/exchange/swap/utils/index.test.js.map +1 -0
- package/lib/families/crypto_org/datasets/croeseid.d.ts.map +1 -1
- package/lib/families/crypto_org/datasets/croeseid.js +2 -6
- package/lib/families/crypto_org/datasets/croeseid.js.map +1 -1
- package/lib/families/elrond/api/apiCalls.d.ts +0 -4
- package/lib/families/elrond/api/apiCalls.d.ts.map +1 -1
- package/lib/families/elrond/api/apiCalls.js +3 -37
- package/lib/families/elrond/api/apiCalls.js.map +1 -1
- package/lib/families/elrond/api/sdk.js +2 -2
- package/lib/families/elrond/api/sdk.js.map +1 -1
- package/lib/families/elrond/js-transaction.d.ts +2 -0
- package/lib/families/elrond/js-transaction.d.ts.map +1 -1
- package/lib/families/elrond/types.d.ts +2 -0
- package/lib/families/elrond/types.d.ts.map +1 -1
- package/lib/families/elrond/types.js.map +1 -1
- package/lib/families/tezos/bridge/js.d.ts.map +1 -1
- package/lib/families/tezos/bridge/js.js +11 -1
- package/lib/families/tezos/bridge/js.js.map +1 -1
- package/lib/mock/account.d.ts +2 -1
- package/lib/mock/account.d.ts.map +1 -1
- package/lib/mock/account.js +4 -2
- package/lib/mock/account.js.map +1 -1
- package/package.json +15 -15
- package/src/__tests__/__snapshots__/all.libcore.ts.snap +10 -10
- package/src/currencies/__snapshots__/sortByMarketcap.test.ts.snap +30 -15
- package/src/env.ts +1 -1
- package/src/exchange/swap/getExchangeRates.ts +8 -5
- package/src/exchange/swap/hooks/index.ts +10 -0
- package/src/exchange/swap/hooks/useFromState.test.ts +134 -0
- package/src/exchange/swap/hooks/useFromState.ts +84 -0
- package/src/exchange/swap/hooks/usePickDefaultAccount.test.ts +167 -0
- package/src/exchange/swap/hooks/usePickDefaultAccount.ts +47 -0
- package/src/exchange/swap/hooks/usePickDefaultCurrency.test.ts +107 -0
- package/src/exchange/swap/hooks/usePickDefaultCurrency.ts +20 -0
- package/src/exchange/swap/hooks/usePollKYCStatus.test.ts +89 -0
- package/src/exchange/swap/hooks/usePollKYCStatus.ts +43 -0
- package/src/exchange/swap/hooks/useProviderRates.test.ts +200 -0
- package/src/exchange/swap/hooks/useProviderRates.ts +130 -0
- package/src/exchange/swap/hooks/useReverseAccounts.test.ts +83 -0
- package/src/exchange/swap/hooks/useReverseAccounts.ts +56 -0
- package/src/exchange/swap/hooks/useSelectableCurrencies.test.ts +39 -0
- package/src/exchange/swap/hooks/useSelectableCurrencies.ts +26 -0
- package/src/exchange/swap/hooks/useSwapProviders.test.ts +113 -0
- package/src/exchange/swap/hooks/useSwapProviders.ts +57 -0
- package/src/exchange/swap/hooks/useSwapTransaction.test.ts +54 -0
- package/src/exchange/swap/hooks/useSwapTransaction.ts +170 -0
- package/src/exchange/swap/hooks/useToState.test.ts +53 -0
- package/src/exchange/swap/hooks/useToState.ts +86 -0
- package/src/exchange/swap/hooks/useUpdateMaxAmount.test.ts +132 -0
- package/src/exchange/swap/hooks/useUpdateMaxAmount.ts +67 -0
- package/src/exchange/swap/mock.ts +22 -3
- package/src/exchange/swap/types.ts +2 -1
- package/src/exchange/swap/utils/index.test.ts +224 -0
- package/src/exchange/swap/utils/index.ts +67 -0
- package/src/families/crypto_org/datasets/croeseid.ts +7 -9
- package/src/families/elrond/api/apiCalls.ts +3 -31
- package/src/families/elrond/api/sdk.ts +2 -2
- package/src/families/elrond/types.ts +2 -0
- package/src/families/tezos/bridge/js.ts +13 -0
- package/src/mock/account.ts +2 -2
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { renderHook } from "@testing-library/react-hooks";
|
|
2
|
+
import BigNumber from "bignumber.js";
|
|
3
|
+
|
|
4
|
+
import { usePickDefaultAccount } from "./usePickDefaultAccount";
|
|
5
|
+
import { genAccount } from "../../../mock/account";
|
|
6
|
+
import type { Account, CryptoCurrency } from "../../../types";
|
|
7
|
+
import { getCryptoCurrencyById } from "../../../currencies";
|
|
8
|
+
|
|
9
|
+
function* accountGenerator(currency: CryptoCurrency): Generator<Account> {
|
|
10
|
+
let id = 0;
|
|
11
|
+
while (true) {
|
|
12
|
+
id += 1;
|
|
13
|
+
// operations are not taken into account by the usePickDefaultAccount hooks
|
|
14
|
+
yield genAccount(`mocked-account-${id}`, { currency, operationsSize: 0 });
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const getAccountCreator = (currencyId: string) => {
|
|
19
|
+
const generator = accountGenerator(getCryptoCurrencyById(currencyId));
|
|
20
|
+
return () => generator.next().value;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
describe("usePickDefaultAccount", () => {
|
|
24
|
+
const getEthAccount = getAccountCreator("ethereum");
|
|
25
|
+
const getBtcAccount = getAccountCreator("bitcoin");
|
|
26
|
+
const getPolkadotAccount = getAccountCreator("polkadot");
|
|
27
|
+
const getCosmosAccount = getAccountCreator("cosmos");
|
|
28
|
+
const setFromAccount = jest.fn();
|
|
29
|
+
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
setFromAccount.mockClear();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("do nothing when fromAccount is not null/undefined", () => {
|
|
35
|
+
const accounts: Account[] = [getEthAccount(), getBtcAccount()];
|
|
36
|
+
|
|
37
|
+
renderHook(() =>
|
|
38
|
+
usePickDefaultAccount(accounts, getEthAccount(), setFromAccount)
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
expect(setFromAccount).toHaveBeenCalledTimes(0);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("do nothing when all passed accounts are disabled", () => {
|
|
45
|
+
const accounts: Account[] = [
|
|
46
|
+
{ ...getEthAccount(), disabled: true },
|
|
47
|
+
{ ...getBtcAccount(), disabled: true },
|
|
48
|
+
{ ...getEthAccount(), disabled: true },
|
|
49
|
+
{ ...getEthAccount(), disabled: true },
|
|
50
|
+
];
|
|
51
|
+
|
|
52
|
+
renderHook(() =>
|
|
53
|
+
usePickDefaultAccount(accounts, undefined, setFromAccount)
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
expect(setFromAccount).toHaveBeenCalledTimes(0);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("returns an ethereum account when it's available", () => {
|
|
60
|
+
const ethAccount = getEthAccount();
|
|
61
|
+
|
|
62
|
+
const accounts: Account[] = [ethAccount, getBtcAccount()];
|
|
63
|
+
|
|
64
|
+
renderHook(() =>
|
|
65
|
+
usePickDefaultAccount(accounts, undefined, setFromAccount)
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
expect(setFromAccount).toHaveBeenCalledTimes(1);
|
|
69
|
+
expect(setFromAccount).toHaveBeenCalledWith(ethAccount);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("returns the first ethereum account when all of the enabled accounts have the same balance", () => {
|
|
73
|
+
const ethAccount = getEthAccount();
|
|
74
|
+
|
|
75
|
+
const accounts: Account[] = [
|
|
76
|
+
ethAccount,
|
|
77
|
+
getBtcAccount(),
|
|
78
|
+
getEthAccount(),
|
|
79
|
+
{ ...getEthAccount(), disabled: true },
|
|
80
|
+
];
|
|
81
|
+
|
|
82
|
+
renderHook(() =>
|
|
83
|
+
usePickDefaultAccount(accounts, undefined, setFromAccount)
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
expect(setFromAccount).toHaveBeenCalledTimes(1);
|
|
87
|
+
expect(setFromAccount).toHaveBeenCalledWith(ethAccount);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("returns the ethereum enabled account with the highest balance", () => {
|
|
91
|
+
const ethAccount = getEthAccount();
|
|
92
|
+
const ethAccount2 = getEthAccount();
|
|
93
|
+
|
|
94
|
+
ethAccount.amount = new BigNumber(1000);
|
|
95
|
+
ethAccount2.amount = new BigNumber(0);
|
|
96
|
+
|
|
97
|
+
const accounts: Account[] = [
|
|
98
|
+
{ ...getEthAccount(), disabled: true },
|
|
99
|
+
ethAccount,
|
|
100
|
+
getBtcAccount(),
|
|
101
|
+
ethAccount2,
|
|
102
|
+
];
|
|
103
|
+
|
|
104
|
+
renderHook(() =>
|
|
105
|
+
usePickDefaultAccount(accounts, undefined, setFromAccount)
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
expect(setFromAccount).toHaveBeenCalledTimes(1);
|
|
109
|
+
expect(setFromAccount).toHaveBeenCalledWith(ethAccount);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("returns the first bitcoin account when no ethereum accounts are passed/enabled and all btc accounts have the same balance", () => {
|
|
113
|
+
const BtcAccount = getBtcAccount();
|
|
114
|
+
|
|
115
|
+
const accounts: Account[] = [
|
|
116
|
+
{ ...getEthAccount(), disabled: true },
|
|
117
|
+
BtcAccount,
|
|
118
|
+
getBtcAccount(),
|
|
119
|
+
];
|
|
120
|
+
|
|
121
|
+
renderHook(() =>
|
|
122
|
+
usePickDefaultAccount(accounts, undefined, setFromAccount)
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
expect(setFromAccount).toHaveBeenCalledTimes(1);
|
|
126
|
+
expect(setFromAccount).toHaveBeenCalledWith(BtcAccount);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test("returns the bitcoin enabled account with the highest balance when no ETH accounts are passed", () => {
|
|
130
|
+
const btcAccount = getBtcAccount();
|
|
131
|
+
const btcAccount2 = getBtcAccount();
|
|
132
|
+
|
|
133
|
+
btcAccount.amount = new BigNumber(1000);
|
|
134
|
+
btcAccount2.amount = new BigNumber(0);
|
|
135
|
+
|
|
136
|
+
const accounts: Account[] = [
|
|
137
|
+
{ ...getEthAccount(), disabled: true },
|
|
138
|
+
btcAccount,
|
|
139
|
+
btcAccount2,
|
|
140
|
+
];
|
|
141
|
+
|
|
142
|
+
renderHook(() =>
|
|
143
|
+
usePickDefaultAccount(accounts, undefined, setFromAccount)
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
expect(setFromAccount).toHaveBeenCalledTimes(1);
|
|
147
|
+
expect(setFromAccount).toHaveBeenCalledWith(btcAccount);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
test("returns the first enabled account when no ethereum/bitcoin accounts are passed", () => {
|
|
151
|
+
const dotAccount = getPolkadotAccount();
|
|
152
|
+
|
|
153
|
+
const accounts: Account[] = [
|
|
154
|
+
{ ...getCosmosAccount(), disabled: true },
|
|
155
|
+
dotAccount,
|
|
156
|
+
getPolkadotAccount(),
|
|
157
|
+
getCosmosAccount(),
|
|
158
|
+
];
|
|
159
|
+
|
|
160
|
+
renderHook(() =>
|
|
161
|
+
usePickDefaultAccount(accounts, undefined, setFromAccount)
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
expect(setFromAccount).toHaveBeenCalledTimes(1);
|
|
165
|
+
expect(setFromAccount).toHaveBeenCalledWith(dotAccount);
|
|
166
|
+
});
|
|
167
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
import { Account, TokenAccount } from "../../../types";
|
|
3
|
+
|
|
4
|
+
// Pick a default source account if none are selected.
|
|
5
|
+
export const usePickDefaultAccount = (
|
|
6
|
+
accounts: ((Account | TokenAccount) & { disabled?: boolean })[],
|
|
7
|
+
fromAccount: Account | TokenAccount | null | undefined,
|
|
8
|
+
setFromAccount: (account: Account | TokenAccount) => void
|
|
9
|
+
): void => {
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
if (!fromAccount) {
|
|
12
|
+
const possibleDefaults = accounts.reduce<
|
|
13
|
+
[
|
|
14
|
+
Account | TokenAccount | undefined | null,
|
|
15
|
+
Account | TokenAccount | undefined | null,
|
|
16
|
+
Account | TokenAccount | undefined | null
|
|
17
|
+
]
|
|
18
|
+
>(
|
|
19
|
+
(acc, account) => {
|
|
20
|
+
if (account.disabled) return acc;
|
|
21
|
+
if (
|
|
22
|
+
// eventually needs a type guard because the "currency" property does not exist in TokenAccount
|
|
23
|
+
account["currency"]?.id === "ethereum" &&
|
|
24
|
+
(acc[0]?.balance ?? -1) < account.balance
|
|
25
|
+
) {
|
|
26
|
+
acc[0] = account;
|
|
27
|
+
}
|
|
28
|
+
if (
|
|
29
|
+
// eventually needs a type guard because the "currency" property does not exist in TokenAccount
|
|
30
|
+
account["currency"]?.id === "bitcoin" &&
|
|
31
|
+
(acc[1]?.balance ?? -1) < account.balance
|
|
32
|
+
) {
|
|
33
|
+
acc[1] = account;
|
|
34
|
+
}
|
|
35
|
+
const maxFundsAccount = acc[2];
|
|
36
|
+
if (!maxFundsAccount || maxFundsAccount.balance < account.balance) {
|
|
37
|
+
acc[2] = account;
|
|
38
|
+
}
|
|
39
|
+
return acc;
|
|
40
|
+
},
|
|
41
|
+
[null, null, null]
|
|
42
|
+
);
|
|
43
|
+
const defaultAccount = possibleDefaults.find(Boolean);
|
|
44
|
+
defaultAccount && setFromAccount(defaultAccount);
|
|
45
|
+
}
|
|
46
|
+
}, [accounts, fromAccount, setFromAccount]);
|
|
47
|
+
};
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { renderHook } from "@testing-library/react-hooks";
|
|
2
|
+
|
|
3
|
+
import { usePickDefaultCurrency } from "./usePickDefaultCurrency";
|
|
4
|
+
import { getCryptoCurrencyById } from "../../../currencies";
|
|
5
|
+
import { CryptoCurrency, TokenCurrency } from "../../../types";
|
|
6
|
+
|
|
7
|
+
describe("usePickDefaultCurrency", () => {
|
|
8
|
+
const setCurrency = jest.fn();
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
setCurrency.mockClear();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test("do nothing when the passed currency is valid", () => {
|
|
15
|
+
const currencies: (CryptoCurrency | TokenCurrency)[] = [
|
|
16
|
+
getCryptoCurrencyById("ethereum"),
|
|
17
|
+
getCryptoCurrencyById("bitcoin"),
|
|
18
|
+
getCryptoCurrencyById("bsc"),
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
renderHook(() =>
|
|
22
|
+
usePickDefaultCurrency(
|
|
23
|
+
currencies,
|
|
24
|
+
getCryptoCurrencyById("ethereum"),
|
|
25
|
+
setCurrency
|
|
26
|
+
)
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
expect(setCurrency).toHaveBeenCalledTimes(0);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("do nothing if the currency is undefined/null and the currencies list don't include eth/btc", () => {
|
|
33
|
+
const currencies: (CryptoCurrency | TokenCurrency)[] = [
|
|
34
|
+
getCryptoCurrencyById("solana"),
|
|
35
|
+
getCryptoCurrencyById("polkadot"),
|
|
36
|
+
getCryptoCurrencyById("bsc"),
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
renderHook(() =>
|
|
40
|
+
usePickDefaultCurrency(currencies, undefined, setCurrency)
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
expect(setCurrency).toHaveBeenCalledTimes(0);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("do nothing if the currency passed isn't valid in the list and the currencies list don't include eth/btc", () => {
|
|
47
|
+
const currencies: (CryptoCurrency | TokenCurrency)[] = [
|
|
48
|
+
getCryptoCurrencyById("solana"),
|
|
49
|
+
getCryptoCurrencyById("polkadot"),
|
|
50
|
+
getCryptoCurrencyById("bsc"),
|
|
51
|
+
];
|
|
52
|
+
|
|
53
|
+
renderHook(() =>
|
|
54
|
+
usePickDefaultCurrency(
|
|
55
|
+
currencies,
|
|
56
|
+
getCryptoCurrencyById("stellar"),
|
|
57
|
+
setCurrency
|
|
58
|
+
)
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
expect(setCurrency).toHaveBeenCalledTimes(0);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test("returns the ethereum currency if the passed currency isn't valid and ethereum comes before bitcoin in the list", () => {
|
|
65
|
+
const ethereumCurrency = getCryptoCurrencyById("ethereum");
|
|
66
|
+
|
|
67
|
+
const currencies: (CryptoCurrency | TokenCurrency)[] = [
|
|
68
|
+
getCryptoCurrencyById("bsc"),
|
|
69
|
+
ethereumCurrency,
|
|
70
|
+
getCryptoCurrencyById("bitcoin"),
|
|
71
|
+
getCryptoCurrencyById("polkadot"),
|
|
72
|
+
];
|
|
73
|
+
|
|
74
|
+
renderHook(() =>
|
|
75
|
+
usePickDefaultCurrency(
|
|
76
|
+
currencies,
|
|
77
|
+
getCryptoCurrencyById("stellar"),
|
|
78
|
+
setCurrency
|
|
79
|
+
)
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
expect(setCurrency).toHaveBeenCalledTimes(1);
|
|
83
|
+
expect(setCurrency).toHaveBeenCalledWith(ethereumCurrency);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test("returns the bitcoin currency if the passed currency isn't valid and bitcoin comes before ethereum in the list", () => {
|
|
87
|
+
const bitcoinCurrency = getCryptoCurrencyById("bitcoin");
|
|
88
|
+
|
|
89
|
+
const currencies: (CryptoCurrency | TokenCurrency)[] = [
|
|
90
|
+
getCryptoCurrencyById("bsc"),
|
|
91
|
+
bitcoinCurrency,
|
|
92
|
+
getCryptoCurrencyById("ethereum"),
|
|
93
|
+
getCryptoCurrencyById("polkadot"),
|
|
94
|
+
];
|
|
95
|
+
|
|
96
|
+
renderHook(() =>
|
|
97
|
+
usePickDefaultCurrency(
|
|
98
|
+
currencies,
|
|
99
|
+
getCryptoCurrencyById("stellar"),
|
|
100
|
+
setCurrency
|
|
101
|
+
)
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
expect(setCurrency).toHaveBeenCalledTimes(1);
|
|
105
|
+
expect(setCurrency).toHaveBeenCalledWith(bitcoinCurrency);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
import { CryptoCurrency, TokenCurrency } from "../../../types";
|
|
3
|
+
|
|
4
|
+
// Pick a default currency target if none are selected.
|
|
5
|
+
export const usePickDefaultCurrency = (
|
|
6
|
+
currencies: (CryptoCurrency | TokenCurrency)[],
|
|
7
|
+
currency: (CryptoCurrency | TokenCurrency) | null | undefined,
|
|
8
|
+
setCurrency: (currency: CryptoCurrency | TokenCurrency) => void
|
|
9
|
+
): void => {
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
// Keep the same currency target if it is still valid.
|
|
12
|
+
const isCurrencyValid = currency && currencies.indexOf(currency) >= 0;
|
|
13
|
+
if (!currency || !isCurrencyValid) {
|
|
14
|
+
const defaultCurrency = currencies.find(
|
|
15
|
+
(currency) => currency.id === "ethereum" || currency.id === "bitcoin"
|
|
16
|
+
);
|
|
17
|
+
defaultCurrency && setCurrency(defaultCurrency);
|
|
18
|
+
}
|
|
19
|
+
}, [currency, currencies, setCurrency]);
|
|
20
|
+
};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { renderHook } from "@testing-library/react-hooks";
|
|
2
|
+
import { mocked } from "ts-jest/utils";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
usePollKYCStatus,
|
|
6
|
+
KYC_STATUS_POLLING_INTERVAL,
|
|
7
|
+
} from "./usePollKYCStatus";
|
|
8
|
+
import type { UsePollKYCStatusProps } from "./usePollKYCStatus";
|
|
9
|
+
import { KYC_STATUS } from "../utils";
|
|
10
|
+
import { getKYCStatus } from "..";
|
|
11
|
+
import { KYCStatus } from "../types";
|
|
12
|
+
|
|
13
|
+
// mock getKYCStatus
|
|
14
|
+
jest.mock("..");
|
|
15
|
+
const mockedGetKYCStatus = mocked(getKYCStatus, true);
|
|
16
|
+
|
|
17
|
+
describe("usePollKYCStatus", () => {
|
|
18
|
+
const defaultInput: UsePollKYCStatusProps = {
|
|
19
|
+
provider: "changelly",
|
|
20
|
+
kyc: { id: "1", status: KYC_STATUS.pending },
|
|
21
|
+
onChange: () => {},
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
mockedGetKYCStatus.mockReset();
|
|
26
|
+
jest.useFakeTimers();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
afterEach(() => {
|
|
30
|
+
jest.runOnlyPendingTimers();
|
|
31
|
+
jest.useRealTimers();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("KYC status isn't fetched when the local status isn't PENDING", () => {
|
|
35
|
+
Object.values(KYC_STATUS)
|
|
36
|
+
.filter((status) => status !== "pending")
|
|
37
|
+
.forEach((status) => {
|
|
38
|
+
const input = { ...defaultInput, kyc: { id: "1", status } };
|
|
39
|
+
renderHook(() => usePollKYCStatus(input));
|
|
40
|
+
expect(mockedGetKYCStatus).toHaveBeenCalledTimes(0);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("onChange isn't triggered when the local/remote status are equals", () => {
|
|
45
|
+
const onChange = jest.fn();
|
|
46
|
+
const input = { ...defaultInput, onChange };
|
|
47
|
+
|
|
48
|
+
const fetchedValue: KYCStatus = { status: "pending", id: "1" };
|
|
49
|
+
mockedGetKYCStatus.mockResolvedValueOnce(fetchedValue);
|
|
50
|
+
|
|
51
|
+
renderHook(() => usePollKYCStatus(input));
|
|
52
|
+
expect(mockedGetKYCStatus).toHaveBeenCalledTimes(1);
|
|
53
|
+
expect(mockedGetKYCStatus).toHaveBeenCalledWith(
|
|
54
|
+
input.provider,
|
|
55
|
+
input.kyc.id
|
|
56
|
+
);
|
|
57
|
+
expect(onChange).toHaveBeenCalledTimes(0);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test("onChange is triggered when the local/remote status are different", () => {
|
|
61
|
+
const onChange = jest.fn();
|
|
62
|
+
const input = { ...defaultInput, onChange };
|
|
63
|
+
|
|
64
|
+
const fetchedValue: KYCStatus = { status: "approved", id: "1" };
|
|
65
|
+
mockedGetKYCStatus.mockResolvedValueOnce(fetchedValue);
|
|
66
|
+
|
|
67
|
+
const { waitFor } = renderHook(() => usePollKYCStatus(input));
|
|
68
|
+
expect(mockedGetKYCStatus).toHaveBeenCalledTimes(1);
|
|
69
|
+
waitFor(() => expect(onChange).toHaveBeenCalledTimes(1));
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("setInterval retriggers the updateKYCStatus function", () => {
|
|
73
|
+
const onChange = jest.fn();
|
|
74
|
+
const input = { ...defaultInput, onChange };
|
|
75
|
+
|
|
76
|
+
const fetchedValue: KYCStatus = { status: "approved", id: "1" };
|
|
77
|
+
mockedGetKYCStatus.mockResolvedValue(fetchedValue);
|
|
78
|
+
|
|
79
|
+
const { waitFor } = renderHook(() => usePollKYCStatus(input));
|
|
80
|
+
waitFor(() => expect(onChange).toHaveBeenCalledTimes(1));
|
|
81
|
+
waitFor(() => expect(mockedGetKYCStatus).toHaveBeenCalledTimes(1));
|
|
82
|
+
|
|
83
|
+
// manually trigger the setInterval
|
|
84
|
+
jest.advanceTimersByTime(KYC_STATUS_POLLING_INTERVAL);
|
|
85
|
+
|
|
86
|
+
waitFor(() => expect(onChange).toHaveBeenCalledTimes(2));
|
|
87
|
+
waitFor(() => expect(mockedGetKYCStatus).toHaveBeenCalledTimes(2));
|
|
88
|
+
});
|
|
89
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
import { KYC_STATUS } from "../utils";
|
|
3
|
+
import { getKYCStatus } from "..";
|
|
4
|
+
|
|
5
|
+
// Poll the server to update the KYC status of a given provider.
|
|
6
|
+
export const KYC_STATUS_POLLING_INTERVAL = 10000;
|
|
7
|
+
export type KYCItem = {
|
|
8
|
+
id: string;
|
|
9
|
+
status: string;
|
|
10
|
+
};
|
|
11
|
+
export type UsePollKYCStatusProps = {
|
|
12
|
+
provider: string;
|
|
13
|
+
kyc: KYCItem;
|
|
14
|
+
onChange: (item: KYCItem) => void;
|
|
15
|
+
};
|
|
16
|
+
export const usePollKYCStatus = (
|
|
17
|
+
{ provider, kyc, onChange }: UsePollKYCStatusProps,
|
|
18
|
+
dependencies = []
|
|
19
|
+
): void => {
|
|
20
|
+
useEffect(
|
|
21
|
+
() => {
|
|
22
|
+
if (kyc?.status !== KYC_STATUS.pending) return;
|
|
23
|
+
let cancelled = false;
|
|
24
|
+
async function updateKYCStatus() {
|
|
25
|
+
if (!kyc?.id) return;
|
|
26
|
+
const res = await getKYCStatus(provider, kyc.id);
|
|
27
|
+
if (cancelled || res?.status === kyc?.status) return;
|
|
28
|
+
onChange(res);
|
|
29
|
+
}
|
|
30
|
+
const intervalId = setInterval(
|
|
31
|
+
updateKYCStatus,
|
|
32
|
+
KYC_STATUS_POLLING_INTERVAL
|
|
33
|
+
);
|
|
34
|
+
updateKYCStatus();
|
|
35
|
+
return () => {
|
|
36
|
+
cancelled = true;
|
|
37
|
+
clearInterval(intervalId);
|
|
38
|
+
};
|
|
39
|
+
},
|
|
40
|
+
// eslint-disable-next-line
|
|
41
|
+
[provider, kyc, ...dependencies]
|
|
42
|
+
);
|
|
43
|
+
};
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { renderHook, act } from "@testing-library/react-hooks";
|
|
2
|
+
import { getCryptoCurrencyById } from "@ledgerhq/cryptoassets";
|
|
3
|
+
import BigNumber from "bignumber.js";
|
|
4
|
+
import { mocked } from "ts-jest/utils";
|
|
5
|
+
import { useProviderRates } from ".";
|
|
6
|
+
import { getExchangeRates } from "..";
|
|
7
|
+
import { genAccount } from "../../../mock/account";
|
|
8
|
+
import { mockGetExchangeRates } from "../mock";
|
|
9
|
+
|
|
10
|
+
jest.mock("..");
|
|
11
|
+
|
|
12
|
+
const BTC = getCryptoCurrencyById("bitcoin");
|
|
13
|
+
const ETH = getCryptoCurrencyById("ethereum");
|
|
14
|
+
const fromAccount = genAccount("from-account", {
|
|
15
|
+
currency: ETH,
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const mockedGetExchangeRates = mocked(getExchangeRates, true);
|
|
19
|
+
const mockedRatesPromise = mockGetExchangeRates(
|
|
20
|
+
{ fromAccount } as any,
|
|
21
|
+
{ amount: new BigNumber(1) } as any,
|
|
22
|
+
BTC
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
describe("useProviderRates", () => {
|
|
26
|
+
let mockedRates;
|
|
27
|
+
const baseInitalProps = {
|
|
28
|
+
fromState: { account: fromAccount } as any,
|
|
29
|
+
toState: { currency: ETH } as any,
|
|
30
|
+
transaction: { amount: new BigNumber(1) } as any,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
beforeAll(async () => {
|
|
34
|
+
mockedRates = await mockedRatesPromise;
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
beforeEach(() => {
|
|
38
|
+
mockedGetExchangeRates.mockReset();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("should fetch and store the rates", async () => {
|
|
42
|
+
mockedGetExchangeRates.mockResolvedValue(mockedRatesPromise);
|
|
43
|
+
const setExchangeRate = jest.fn();
|
|
44
|
+
|
|
45
|
+
const { result, waitForNextUpdate } = renderHook(useProviderRates, {
|
|
46
|
+
initialProps: {
|
|
47
|
+
...baseInitalProps,
|
|
48
|
+
setExchangeRate,
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
expect(result.current.rates.status).toBe("loading");
|
|
53
|
+
|
|
54
|
+
await waitForNextUpdate({ timeout: 1000 });
|
|
55
|
+
|
|
56
|
+
// Check mock
|
|
57
|
+
expect(mockedGetExchangeRates).toHaveBeenCalledTimes(1);
|
|
58
|
+
// Check callback
|
|
59
|
+
expect(setExchangeRate).toHaveBeenCalledTimes(1);
|
|
60
|
+
expect(setExchangeRate.mock.calls[0][0]).toBe(mockedRates[0]);
|
|
61
|
+
// Check result
|
|
62
|
+
const { rates } = result.current;
|
|
63
|
+
expect(rates.error).toBeUndefined();
|
|
64
|
+
expect(rates.status).toBeNull();
|
|
65
|
+
expect(rates.value).toMatchObject(mockedRates);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("should not fetch the rates if the conditions are not met", async () => {
|
|
69
|
+
const testProps = [
|
|
70
|
+
// No account
|
|
71
|
+
{
|
|
72
|
+
...baseInitalProps,
|
|
73
|
+
fromState: { account: null } as any,
|
|
74
|
+
},
|
|
75
|
+
// No currency
|
|
76
|
+
{
|
|
77
|
+
...baseInitalProps,
|
|
78
|
+
toState: { currency: null } as any,
|
|
79
|
+
},
|
|
80
|
+
// No transaction
|
|
81
|
+
{
|
|
82
|
+
...baseInitalProps,
|
|
83
|
+
transaction: null,
|
|
84
|
+
},
|
|
85
|
+
// Amount <= 0
|
|
86
|
+
{
|
|
87
|
+
...baseInitalProps,
|
|
88
|
+
transaction: { amount: new BigNumber(0) } as any,
|
|
89
|
+
},
|
|
90
|
+
// Nothing
|
|
91
|
+
{} as any,
|
|
92
|
+
];
|
|
93
|
+
|
|
94
|
+
testProps.forEach((initialProps) => {
|
|
95
|
+
renderHook(useProviderRates, {
|
|
96
|
+
initialProps,
|
|
97
|
+
});
|
|
98
|
+
expect(mockedGetExchangeRates).toHaveBeenCalledTimes(0);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("should refetch the rates if a dependency has been updated", async () => {
|
|
103
|
+
mockedGetExchangeRates.mockResolvedValue(mockedRatesPromise);
|
|
104
|
+
|
|
105
|
+
const { waitForNextUpdate, rerender } = renderHook(useProviderRates, {
|
|
106
|
+
initialProps: baseInitalProps,
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
await waitForNextUpdate({ timeout: 1000 });
|
|
110
|
+
expect(mockedGetExchangeRates).toHaveBeenCalledTimes(1);
|
|
111
|
+
mockedGetExchangeRates.mockClear();
|
|
112
|
+
|
|
113
|
+
rerender();
|
|
114
|
+
expect(mockedGetExchangeRates).toHaveBeenCalledTimes(0);
|
|
115
|
+
|
|
116
|
+
rerender({
|
|
117
|
+
...baseInitalProps,
|
|
118
|
+
fromState: { account: { ...fromAccount } } as any,
|
|
119
|
+
});
|
|
120
|
+
await waitForNextUpdate({ timeout: 1000 });
|
|
121
|
+
expect(mockedGetExchangeRates).toHaveBeenCalledTimes(1);
|
|
122
|
+
mockedGetExchangeRates.mockClear();
|
|
123
|
+
|
|
124
|
+
rerender({ ...baseInitalProps, toState: { currency: { ...ETH } } as any });
|
|
125
|
+
await waitForNextUpdate({ timeout: 1000 });
|
|
126
|
+
expect(mockedGetExchangeRates).toHaveBeenCalledTimes(1);
|
|
127
|
+
mockedGetExchangeRates.mockClear();
|
|
128
|
+
|
|
129
|
+
rerender({
|
|
130
|
+
...baseInitalProps,
|
|
131
|
+
transaction: { amount: new BigNumber(2) } as any,
|
|
132
|
+
});
|
|
133
|
+
await waitForNextUpdate({ timeout: 1000 });
|
|
134
|
+
expect(mockedGetExchangeRates).toHaveBeenCalledTimes(1);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("should refetch the rates if refetchRates is called", async () => {
|
|
138
|
+
const { result, waitForNextUpdate } = renderHook(useProviderRates, {
|
|
139
|
+
initialProps: baseInitalProps,
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
await waitForNextUpdate({ timeout: 1000 });
|
|
143
|
+
expect(mockedGetExchangeRates).toHaveBeenCalledTimes(1);
|
|
144
|
+
mockedGetExchangeRates.mockClear();
|
|
145
|
+
|
|
146
|
+
act(() => {
|
|
147
|
+
result.current.refetchRates();
|
|
148
|
+
});
|
|
149
|
+
await waitForNextUpdate({ timeout: 1000 });
|
|
150
|
+
expect(mockedGetExchangeRates).toHaveBeenCalledTimes(1);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("should filter out errored rates", async () => {
|
|
154
|
+
const mockedRatesWithSingleError = mockedRates.map((rate, index) =>
|
|
155
|
+
index === 0
|
|
156
|
+
? {
|
|
157
|
+
...rate,
|
|
158
|
+
error: new Error("kaboom"),
|
|
159
|
+
}
|
|
160
|
+
: rate
|
|
161
|
+
);
|
|
162
|
+
mockedGetExchangeRates.mockResolvedValue(mockedRatesWithSingleError);
|
|
163
|
+
|
|
164
|
+
const { result, waitForNextUpdate } = renderHook(useProviderRates, {
|
|
165
|
+
initialProps: baseInitalProps,
|
|
166
|
+
});
|
|
167
|
+
await waitForNextUpdate({ timeout: 1000 });
|
|
168
|
+
expect(result.current.rates.value).toMatchObject([mockedRates[1]]);
|
|
169
|
+
expect(result.current.rates.status).toBeNull();
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("should dispatch an error if all the rates contain an error", async () => {
|
|
173
|
+
const error = new Error("kaboom");
|
|
174
|
+
const mockedRatesWithError = mockedRates.map((rate) => ({
|
|
175
|
+
...rate,
|
|
176
|
+
error,
|
|
177
|
+
}));
|
|
178
|
+
mockedGetExchangeRates.mockResolvedValue(mockedRatesWithError);
|
|
179
|
+
const { result, waitForNextUpdate } = renderHook(useProviderRates, {
|
|
180
|
+
initialProps: baseInitalProps,
|
|
181
|
+
});
|
|
182
|
+
await waitForNextUpdate({ timeout: 1000 });
|
|
183
|
+
expect(result.current.rates.status).toBe("error");
|
|
184
|
+
expect(result.current.rates.error).toBe(error);
|
|
185
|
+
expect(result.current.rates.value).toBeUndefined();
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("should call 'onNoRates' if there are no rates returned by the server", async () => {
|
|
189
|
+
const onNoRates = jest.fn(() => {});
|
|
190
|
+
mockedGetExchangeRates.mockResolvedValue([]);
|
|
191
|
+
|
|
192
|
+
const { result, waitForNextUpdate } = renderHook(useProviderRates, {
|
|
193
|
+
initialProps: { ...baseInitalProps, onNoRates },
|
|
194
|
+
});
|
|
195
|
+
await waitForNextUpdate({ timeout: 1000 });
|
|
196
|
+
expect(result.current.rates.value).toMatchObject([]);
|
|
197
|
+
expect(result.current.rates.status).toBeNull();
|
|
198
|
+
expect(onNoRates).toHaveBeenCalledTimes(1);
|
|
199
|
+
});
|
|
200
|
+
});
|