forked from Vero-protocol/vero-core-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathField.test.tsx
More file actions
93 lines (86 loc) · 2.34 KB
/
Copy pathField.test.tsx
File metadata and controls
93 lines (86 loc) · 2.34 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { describe, it, expect, vi } from 'vitest';
import { render, screen, fireEvent } from '@testing-library/react';
import { Field } from './Field';
describe('Field', () => {
it('renders the label and input by id linkage', () => {
render(
<Field
label="RPC endpoint"
value=""
onChange={() => undefined}
/>,
);
const input = screen.getByLabelText(/RPC endpoint/i);
expect(input).toBeInTheDocument();
expect(input.tagName).toBe('INPUT');
});
it('invokes onChange with the new value', () => {
const onChange = vi.fn();
render(
<Field
label="RPC endpoint"
value=""
onChange={onChange}
/>,
);
fireEvent.change(screen.getByLabelText(/RPC endpoint/i), {
target: { value: 'https://example.com' },
});
expect(onChange).toHaveBeenCalledWith('https://example.com');
});
it('renders hint text when no error is set', () => {
render(
<Field
label="RPC endpoint"
value=""
onChange={() => undefined}
hint="Must use HTTPS"
/>,
);
expect(screen.getByText(/Must use HTTPS/i)).toBeInTheDocument();
});
it('hides hint and shows error with role=alert when error is set', () => {
render(
<Field
label="RPC endpoint"
value="bad"
onChange={() => undefined}
error="Invalid URL"
hint="Must use HTTPS"
/>,
);
const error = screen.getByRole('alert');
expect(error).toHaveTextContent('Invalid URL');
expect(screen.queryByText(/Must use HTTPS/i)).not.toBeInTheDocument();
});
it('sets aria-invalid only when an error is present', () => {
const { rerender } = render(
<Field
label="RPC"
value=""
onChange={() => undefined}
/>,
);
expect(screen.getByLabelText(/RPC/i)).not.toHaveAttribute('aria-invalid');
rerender(
<Field
label="RPC"
value="bad"
onChange={() => undefined}
error="bad"
/>,
);
expect(screen.getByLabelText(/RPC/i)).toHaveAttribute('aria-invalid', 'true');
});
it('renders optional marker when optional=true', () => {
render(
<Field
label="Webhook URL"
value=""
onChange={() => undefined}
optional
/>,
);
expect(screen.getByText(/optional/i)).toBeInTheDocument();
});
});