forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManualEntryFallback.test.tsx
More file actions
60 lines (55 loc) · 2.52 KB
/
Copy pathManualEntryFallback.test.tsx
File metadata and controls
60 lines (55 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { describe, it, expect, vi } from 'vitest';
import { render, screen, fireEvent } from '@testing-library/react';
import { ManualEntryFallback } from './ManualEntryFallback';
describe('ManualEntryFallback', () => {
it('renders form fields and buttons', () => {
render(
<ManualEntryFallback onSubmit={vi.fn()} onCancel={vi.fn()} />
);
expect(screen.getByLabelText(/amount/i)).toBeInTheDocument();
expect(screen.getByLabelText(/date/i)).toBeInTheDocument();
expect(screen.getByLabelText(/merchant/i)).toBeInTheDocument();
expect(screen.getByLabelText(/notes/i)).toBeInTheDocument();
expect(screen.getByRole('button', { name: /back/i })).toBeInTheDocument();
expect(screen.getByRole('button', { name: /save details/i })).toBeInTheDocument();
});
it('calls onCancel when Back is clicked', () => {
const onCancel = vi.fn();
render(<ManualEntryFallback onSubmit={vi.fn()} onCancel={onCancel} />);
fireEvent.click(screen.getByRole('button', { name: /back/i }));
expect(onCancel).toHaveBeenCalledTimes(1);
});
it('shows error when amount is invalid on submit', async () => {
const onSubmit = vi.fn();
render(<ManualEntryFallback onSubmit={onSubmit} onCancel={vi.fn()} />);
fireEvent.change(screen.getByLabelText(/amount/i), { target: { value: 'abc' } });
fireEvent.click(screen.getByRole('button', { name: /save details/i }));
expect(screen.getByText(/valid amount/i)).toBeInTheDocument();
expect(onSubmit).not.toHaveBeenCalled();
});
it('calls onSubmit with entered data when amount is valid', () => {
const onSubmit = vi.fn();
render(<ManualEntryFallback onSubmit={onSubmit} onCancel={vi.fn()} />);
fireEvent.change(screen.getByLabelText(/amount/i), { target: { value: '25.50' } });
fireEvent.change(screen.getByLabelText(/merchant/i), { target: { value: 'Store' } });
fireEvent.click(screen.getByRole('button', { name: /save details/i }));
expect(onSubmit).toHaveBeenCalledWith(
expect.objectContaining({
amount: '25.50',
merchant: 'Store',
})
);
});
it('uses default date when not provided', () => {
const onSubmit = vi.fn();
render(<ManualEntryFallback onSubmit={onSubmit} onCancel={vi.fn()} />);
fireEvent.change(screen.getByLabelText(/amount/i), { target: { value: '10' } });
fireEvent.click(screen.getByRole('button', { name: /save details/i }));
expect(onSubmit).toHaveBeenCalledWith(
expect.objectContaining({
amount: '10.00',
date: expect.any(String),
})
);
});
});