From a7192cbf43aa0ab7fa84a744143086de6c30eb53 Mon Sep 17 00:00:00 2001 From: Paul Cramer Date: Tue, 6 Aug 2024 13:08:36 -0700 Subject: [PATCH 1/2] chore: Add tests for SwapAmountInpu --- src/swap/components/SwapAmountInput.test.tsx | 102 ++++++++++++------- 1 file changed, 63 insertions(+), 39 deletions(-) diff --git a/src/swap/components/SwapAmountInput.test.tsx b/src/swap/components/SwapAmountInput.test.tsx index c247dd8bd1..7f0292bf95 100644 --- a/src/swap/components/SwapAmountInput.test.tsx +++ b/src/swap/components/SwapAmountInput.test.tsx @@ -15,10 +15,6 @@ vi.mock('./SwapProvider', () => ({ useSwapContext: vi.fn(), })); -vi.mock('wagmi', () => ({ - useBalance: vi.fn(), -})); - const useSwapContextMock = useSwapContext as Mock; const mockEthToken: Token = { @@ -65,11 +61,6 @@ const mockContextValue = { handleAmountChange: vi.fn(), } as SwapContextType; -// const mockContextValueWithoutConvertedBalance = { -// ...mockContextValue, -// convertedFromTokenBalance: undefined, -// }; - const mockSwappableTokens: Token[] = [ { name: 'Ethereum', @@ -141,18 +132,23 @@ describe('SwapAmountInput', () => { ); }); - // it('does not update input value with balance amount on max button click when convertedBalance is undefined', () => { - // render( - // - // - // , - // ); - // - // const maxButton = screen.getByTestId('ockSwapAmountInput_MaxButton'); - // fireEvent.click(maxButton); - // - // expect(mockContextValue.setFromAmount).not.toHaveBeenCalled(); - // }); + it('does not update input value with balance amount on max button click when balance is undefined', () => { + const mockContextValueWithNoBalance = { + ...mockContextValue, + from: { + ...mockContextValue.from, + balance: undefined, + }, + }; + + useSwapContextMock.mockReturnValue(mockContextValueWithNoBalance); + render(); + + const maxButton = screen.getByTestId('ockSwapAmountInput_MaxButton'); + fireEvent.click(maxButton); + + expect(mockContextValue.from.setAmount).not.toHaveBeenCalled(); + }); it('displays the correct amount when this type is "from"', () => { useSwapContextMock.mockReturnValue(mockContextValue); @@ -214,21 +210,22 @@ describe('SwapAmountInput', () => { expect(mockContextValue.to.setToken).toHaveBeenCalledWith(mockEthToken); }); - // it('renders the correct balance', () => { - // useSwapContextMock.mockReturnValue(mockContextValue); - // render(); - // - // expect(screen.getByText('Balance: 3304007.277394')).toBeInTheDocument(); - // }); + it('correctly computes sourceTokenOptions excluding destination token', () => { + const mockContextValueWithTokens = { + ...mockContextValue, + to: { + ...mockContextValue.to, + token: mockEthToken, + }, + }; - it('renders a TokenSelectDropdown component if swappableTokens are passed as prop', () => { - useSwapContextMock.mockReturnValue(mockContextValue); + useSwapContextMock.mockReturnValue(mockContextValueWithTokens); render( , ); @@ -236,13 +233,40 @@ describe('SwapAmountInput', () => { expect(dropdown).toBeInTheDocument(); }); - // it('renders a TokenChip component if swappableTokens are not passed as prop', () => { - // useSwapContextMock.mockReturnValue(mockContextValue); - // render(); - // - // const dropdown = screen.getByText('TokenChip'); - // expect(dropdown).toBeInTheDocument(); - // }); + it('hasInsufficientBalance is true when balance is less than amount for type "from"', () => { + const mockContextValueWithLowBalance = { + ...mockContextValue, + from: { + ...mockContextValue.from, + balance: '5', + amount: '10', + }, + }; + + useSwapContextMock.mockReturnValue(mockContextValueWithLowBalance); + render(); + + const input = screen.getByTestId('ockTextInput_Input'); + expect(input).toHaveClass('text-ock-error'); + }); + + it('renders a TokenChip component if swappableTokens are not passed as prop', () => { + useSwapContextMock.mockReturnValue({ + ...mockContextValue, + to: { + ...mockContextValue.to, + token: mockToken, + }, + }); + + render(); + + const chips = screen.getAllByText('TokenChip'); + + expect(chips.length).toBeGreaterThan(0); + + expect(chips[0]).toBeInTheDocument(); + }); it('applies the given className to the button', async () => { useSwapContextMock.mockReturnValue(mockContextValue); From c701b3e2e79ed5b3b767b5f1ce761bfe9f2985b0 Mon Sep 17 00:00:00 2001 From: Paul Cramer Date: Tue, 6 Aug 2024 13:10:48 -0700 Subject: [PATCH 2/2] add changeset --- .changeset/ten-swans-trade.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/ten-swans-trade.md diff --git a/.changeset/ten-swans-trade.md b/.changeset/ten-swans-trade.md new file mode 100644 index 0000000000..62bb7ba5d5 --- /dev/null +++ b/.changeset/ten-swans-trade.md @@ -0,0 +1,5 @@ +--- +"@coinbase/onchainkit": patch +--- + +-**chore**: Add tests for SwapAmountInput. By @cpcramer. #987