Skip to content

Commit

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

describe('DepositDropdown', () => {
const mockTokenInfos = new Map();
mockTokenInfos.set('0x123', { name: 'Test Token' });

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

const mockGui = {
saveDeposit: vi.fn(),
isDepositPreset: vi.fn()
};

it('renders token name correctly', () => {
const { getByText } = render(DepositDropdown, {
props: {
deposit: mockDeposit,
gui: mockGui as unknown as DotrainOrderGui,
tokenInfos: mockTokenInfos
}
});

expect(getByText('Test Token')).toBeTruthy();
});

it('shows "Choose deposit amount" by default', () => {
const { getByText } = render(DepositDropdown, {
props: {
deposit: mockDeposit,
gui: mockGui as unknown as DotrainOrderGui,
tokenInfos: mockTokenInfos
}
});

expect(getByText('Choose deposit amount')).toBeTruthy();
});

it('calls saveDeposit when preset is selected', async () => {
mockGui.isDepositPreset.mockReturnValue(true);

const { getByText } = render(DepositDropdown, {
props: {
deposit: mockDeposit,
gui: mockGui as unknown as DotrainOrderGui,
tokenInfos: mockTokenInfos
}
});

await fireEvent.click(getByText('Choose deposit amount'));
await fireEvent.click(getByText('100'));

expect(mockGui.saveDeposit).toHaveBeenCalledWith('TEST', '100');
});

it('shows input field when custom value is selected', async () => {
mockGui.isDepositPreset.mockReturnValue(false);

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

await fireEvent.click(getByText('Choose deposit amount'));
await fireEvent.click(getByText('Custom value'));

expect(getByPlaceholderText('Enter deposit amount')).toBeTruthy();
});

it('calls saveDeposit when custom value is entered', async () => {
mockGui.isDepositPreset.mockReturnValue(false);

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

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

expect(mockGui.saveDeposit).toHaveBeenCalledWith('TEST', '150');
});
});

0 comments on commit c435a4c

Please sign in to comment.