Skip to content

Commit

Permalink
test depositButtons.test.
Browse files Browse the repository at this point in the history
  • Loading branch information
hardingjam committed Dec 27, 2024
1 parent c435a4c commit 33ba188
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
88 changes: 88 additions & 0 deletions packages/ui-components/src/__tests__/DepositButtons.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, fireEvent } from '@testing-library/svelte';
import DepositButtons from '../lib/components/deployment/wizard/DepositButtons.svelte';
import type { GuiDeposit } from '@rainlanguage/orderbook/js_api';
import type { ComponentProps } from 'svelte';

type DepositButtonsProps = ComponentProps<DepositButtons>;

describe('DepositButtons', () => {
const mockGui = {
isDepositPreset: vi.fn(),
saveDeposit: vi.fn()
};

const mockTokenInfos = new Map([
['0x123', { name: 'Test Token', symbol: 'TEST' }]
]);

const mockDeposit: GuiDeposit = {
token: { address: '0x123' },
token_name: 'TEST',
presets: ['100', '200', '300']
} as unknown as GuiDeposit

beforeEach(() => {
vi.clearAllMocks();
});

it('renders token name and presets', () => {
const { getByText } = render(DepositButtons, {
props: {
deposit: mockDeposit,
gui: mockGui,
tokenInfos: mockTokenInfos
} as unknown as DepositButtonsProps
});

expect(getByText('Test Token')).toBeTruthy();
expect(getByText('100')).toBeTruthy();
expect(getByText('200')).toBeTruthy();
expect(getByText('300')).toBeTruthy();
expect(getByText('Custom')).toBeTruthy();
});

it('handles preset button clicks', async () => {
const { getByText } = render(DepositButtons, {
props: {
deposit: mockDeposit,
gui: mockGui,
tokenInfos: mockTokenInfos
} as unknown as DepositButtonsProps
});

await fireEvent.click(getByText('100'));
expect(mockGui.saveDeposit).toHaveBeenCalledWith('TEST', '100');
});

it('shows custom input when Custom button is clicked', async () => {
const { getByText, getByPlaceholderText } = render(DepositButtons, {
props: {
deposit: mockDeposit,
gui: mockGui,
tokenInfos: mockTokenInfos
} as unknown as DepositButtonsProps
});

await fireEvent.click(getByText('Custom'));
expect(getByPlaceholderText('Enter deposit amount')).toBeTruthy();
expect(mockGui.saveDeposit).toHaveBeenCalledWith('TEST', '');
});

it('handles custom input changes', async () => {
mockGui.isDepositPreset.mockReturnValue(false);

const { getByPlaceholderText } = render(DepositButtons, {
props: {
deposit: mockDeposit,
gui: mockGui,
tokenInfos: mockTokenInfos
} as unknown as DepositButtonsProps
});

const input = getByPlaceholderText('Enter deposit amount');
await fireEvent.input(input, { target: { value: '150' } });

expect(mockGui.saveDeposit).toHaveBeenCalledWith('TEST', '150');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import type {
DotrainOrderGui,
GuiDeposit,
TokenDeposit,
GuiFieldDefinition,
SelectTokens,
TokenInfos,
Expand All @@ -22,7 +22,7 @@
export let allFieldDefinitions: GuiFieldDefinition[];
export let allTokenInputs: Vault[];
export let allTokenOutputs: Vault[];
export let allDeposits: GuiDeposit[];
export let allDeposits: TokenDeposit[];
export let inputVaultIds: string[];
export let outputVaultIds: string[];
export let isLimitStrat: boolean;
Expand Down

0 comments on commit 33ba188

Please sign in to comment.