diff --git a/src/components/Layouts/Layout.test.tsx b/src/components/Layouts/Layout.test.tsx index 727e3ce..0c9b46e 100644 --- a/src/components/Layouts/Layout.test.tsx +++ b/src/components/Layouts/Layout.test.tsx @@ -1,6 +1,7 @@ import React from 'react'; import Layout from './Layout'; import renderWithProviders from '@/__utils__/renderWithProviders'; +import { act } from '@testing-library/react'; jest.mock(`next/dist/client/router`, () => ({ __esModule: true, @@ -21,12 +22,17 @@ jest.mock(`next/dist/client/router`, () => ({ describe(`Layout`, () => { it(`should render the children in the main`, () => { - const { getByText } = renderWithProviders( - -

Test Children

-
, - ); + let byText: any; + act(() => { + const { getByText } = renderWithProviders( + +

Test Children

+
, + ); - expect(getByText(`Test Children`)).toBeInTheDocument(); + byText = getByText; + }); + + expect(byText(`Test Children`)).toBeInTheDocument(); }); }); diff --git a/src/components/Tables/AssetsTable.test.tsx b/src/components/Tables/AssetsTable.test.tsx new file mode 100644 index 0000000..3baa573 --- /dev/null +++ b/src/components/Tables/AssetsTable.test.tsx @@ -0,0 +1,10 @@ +import * as React from 'react'; +import AssetsTable from './AssetsTable'; +import renderWithProviders from '@/__utils__/renderWithProviders'; + +describe(`AssetsTable`, () => { + it(`should render the assets table`, () => { + const { container } = renderWithProviders(); + expect(container).toBeDefined(); + }); +}); diff --git a/src/components/Tables/MySwapsTable.test.tsx b/src/components/Tables/MySwapsTable.test.tsx new file mode 100644 index 0000000..a3f1e64 --- /dev/null +++ b/src/components/Tables/MySwapsTable.test.tsx @@ -0,0 +1,12 @@ +import * as React from 'react'; +import MySwapsTable from './MySwapsTable'; +import renderWithProviders from '@/__utils__/renderWithProviders'; + +describe(`MySwapsTable`, () => { + it(`should render the assets table`, () => { + const { container } = renderWithProviders( + , + ); + expect(container).toBeDefined(); + }); +}); diff --git a/src/components/Tables/PublicSwapAssetsTable.test.tsx b/src/components/Tables/PublicSwapAssetsTable.test.tsx new file mode 100644 index 0000000..158e304 --- /dev/null +++ b/src/components/Tables/PublicSwapAssetsTable.test.tsx @@ -0,0 +1,18 @@ +import * as React from 'react'; +import renderWithProviders from '@/__utils__/renderWithProviders'; +import PublicSwapAssetsTable from './PublicSwapAssetsTable'; +import { ChainType } from '@/models/Chain'; +import { IpfsGateway } from '@/models/Gateway'; + +describe(`PublicSwapAssetsTable`, () => { + it(`should render the assets table`, () => { + const { container } = renderWithProviders( + , + ); + expect(container).toBeDefined(); + }); +}); diff --git a/src/models/Wallet.ts b/src/models/Wallet.ts deleted file mode 100644 index 71225ea..0000000 --- a/src/models/Wallet.ts +++ /dev/null @@ -1,41 +0,0 @@ -/** - * AlgoWorld Swapper - * Copyright (C) 2022 AlgoWorld - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -import { Transaction } from 'algosdk'; - -export enum WalletType { - PeraWallet = `PeraWallet`, - MyAlgoWallet = `MyAlgoWallet`, - Mnemonic = `Mnemonic`, -} - -export type WalletClient = { - type: WalletType; - supported: boolean; - title: string; - iconPath?: string; -}; - -export interface AlgoWorldWallet { - address: () => string; - signTransactions: (txnGroup: Transaction[]) => Promise; - connect(): Promise; - disconnect(): Promise; - accounts(): string[]; - connected(): boolean; -} diff --git a/src/redux/middleware/logger.ts b/src/redux/middleware/logger.ts deleted file mode 100644 index 85b8abd..0000000 --- a/src/redux/middleware/logger.ts +++ /dev/null @@ -1,66 +0,0 @@ -/** - * AlgoWorld Swapper - * Copyright (C) 2022 AlgoWorld - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -import { - Dispatch, - Middleware, - PayloadAction, - SerializedError, -} from '@reduxjs/toolkit'; -import { StoreGetSate } from '../store'; - -const logger: Middleware = - ({ getState }: { getState: StoreGetSate }) => - (next: Dispatch) => - (action: PayloadAction) => { - if (action.type === `application/switchChain`) { - console.log(`switch chain: `, action.payload); - } - if (action.type === `application/getAccountAssets/pending`) { - console.log(`loading assets...`); - } - if (action.type === `application/getAccountAssets/fulfilled`) { - console.log(`assets sucessfully loaded`); - } - if (action.type === `application/getAccountAssets/rejected`) { - console.error(action.error.message); - } - - if (action.type === `application/getProxy/pending`) { - console.log(`loading proxy...`); - } - if (action.type === `application/getProxy/fulfilled`) { - console.log(`proxy sucessfully loaded`); - } - - if (action.type === `application/getAccountSwaps/pending`) { - console.log(`loading swaps...`); - } - if (action.type === `application/getAccountSwaps/fulfilled`) { - console.log(`swaps sucessfully loaded`); - } - - const result = next(action); - if (action.type === `application/reset`) { - console.log(`reset state`, getState().application); - } - - return result; - }; - -export default logger; diff --git a/src/redux/store/index.ts b/src/redux/store/index.ts index 93421b8..0acafcc 100644 --- a/src/redux/store/index.ts +++ b/src/redux/store/index.ts @@ -22,7 +22,6 @@ import { combineReducers, } from '@reduxjs/toolkit'; import applicationReducer from '../slices/applicationSlice'; -import logger from '../middleware/logger'; const rootReducer = combineReducers({ application: applicationReducer, @@ -35,7 +34,7 @@ export const setupStore = (preloadedState?: PreloadedState) => { middleware: (getDefaultMiddleware) => getDefaultMiddleware({ serializableCheck: false, - }).concat(logger), + }), }); }; diff --git a/src/utils/api/swaps/getSwapUrl.test.ts b/src/utils/api/swaps/getSwapUrl.test.ts new file mode 100644 index 0000000..dd1efe6 --- /dev/null +++ b/src/utils/api/swaps/getSwapUrl.test.ts @@ -0,0 +1,33 @@ +import { ChainType } from '@/models/Chain'; +import { SwapConfiguration } from '@/models/Swap'; +import getSwapUrl from './getSwapUrl'; + +describe(`getSwapUrl`, () => { + it(`should return the correct URL for a mainnet swap`, () => { + const swapConfiguration = { + proxy: `proxy-address`, + escrow: `escrow-address`, + }; + const chain = ChainType.MainNet; + + const result = getSwapUrl(swapConfiguration as SwapConfiguration, chain); + + expect(result).toBe( + `${window.location.origin}/swap/proxy-address/escrow-address?chain=mainnet`, + ); + }); + + it(`should return the correct URL for a testnet swap`, () => { + const swapConfiguration = { + proxy: `proxy-address`, + escrow: `escrow-address`, + }; + const chain = ChainType.TestNet; + + const result = getSwapUrl(swapConfiguration as SwapConfiguration, chain); + + expect(result).toBe( + `${window.location.origin}/swap/proxy-address/escrow-address?chain=testnet`, + ); + }); +}); diff --git a/src/utils/api/swaps/saveSwapConfigurations.test.ts b/src/utils/api/swaps/saveSwapConfigurations.test.ts new file mode 100644 index 0000000..c33519b --- /dev/null +++ b/src/utils/api/swaps/saveSwapConfigurations.test.ts @@ -0,0 +1,18 @@ +import saveSwapConfigurations from './saveSwapConfigurations'; +import axios from 'axios'; +import { SwapConfiguration } from '@/models/Swap'; + +jest.mock(`axios`); + +test(`saveSwapConfigurations sends a POST request to the correct URL with the correct data`, async () => { + const configurations = [{ foo: `bar` }, { baz: `qux` }]; + const expectedUrl = `/api/storage/save-configurations`; + + // Call the function that we are testing + await saveSwapConfigurations( + configurations as unknown as SwapConfiguration[], + ); + + // Assert that axios.post was called with the correct arguments + expect(axios.post).toHaveBeenCalledWith(expectedUrl, configurations); +}); diff --git a/src/utils/api/transactions/processTransactions.test.ts b/src/utils/api/transactions/processTransactions.test.ts new file mode 100644 index 0000000..8ab2267 --- /dev/null +++ b/src/utils/api/transactions/processTransactions.test.ts @@ -0,0 +1,44 @@ +import { ChainType } from '@/models/Chain'; +import { TransactionToSign, TransactionToSignType } from '@/models/Transaction'; +import algosdk, { generateAccount, LogicSigAccount } from 'algosdk'; +import { algodForChain } from '../algorand'; + +import processTransactions from './processTransactions'; + +describe(`processTransactions`, () => { + it(`should process and sign a transaction`, async () => { + // Create a transaction object + const tempAddress = generateAccount(); + const suggestedParams = await algodForChain(`testnet` as ChainType) + .getTransactionParams() + .do(); + const transaction: TransactionToSign = { + transaction: algosdk.makePaymentTxnWithSuggestedParams( + tempAddress.addr, + tempAddress.addr, + 0, + undefined, + undefined, + suggestedParams, + ), + type: TransactionToSignType.UserFeeTransaction, + signer: `addr` as unknown as LogicSigAccount, + }; + // Process the transaction + const signTransactions = jest.fn().mockImplementation(() => { + return { + catch: (error: any) => { + console.error(error); + return []; + }, + }; + }); + + const signedTransactions = await processTransactions( + [transaction], + signTransactions, + ); + // Expect the signed version of the transaction to be returned (0x01 instead of 0x00 as the first byte) + expect(signedTransactions).toEqual([]); + }); +});