-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.component.test.tsx
80 lines (75 loc) · 2.79 KB
/
button.component.test.tsx
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
import { describe, it, expect, vi } from 'vitest';
import { render } from '@solidjs/testing-library';
import { Button } from './button.component';
describe('Button', () => {
it('should render with default values', () => {
const { getByRole } = render(() => <Button>Click me</Button>);
const button = getByRole('button');
expect(button).toBeInTheDocument();
expect(button).toHaveClass('spx-button');
expect(button).toHaveTextContent('Click me');
expect(button.querySelector('iconify-icon')).not.toBeInTheDocument();
expect(button).toHaveAttribute('spx-variant', 'contained');
expect(button).toHaveAttribute('spx-color', 'primary');
expect(button).not.toHaveAttribute('spx-rounded');
expect(button).not.toBeDisabled();
});
it('should render with custom values', () => {
const { getByRole } = render(() => (
<Button
icon="mdi:star"
variant="outlined"
color="gradient"
rounded
disabled
attrs={{
id: 'button',
}}
>
Click me
</Button>
));
const button = getByRole('button');
expect(button).toBeInTheDocument();
expect(button).toHaveClass('spx-button');
expect(button).toHaveTextContent('Click me');
expect(button.querySelector('iconify-icon')).toBeInTheDocument();
expect(button.querySelector('iconify-icon')).toHaveAttribute('icon', 'mdi:star');
expect(button).toHaveAttribute('spx-variant', 'outlined');
expect(button).toHaveAttribute('spx-color', 'gradient');
expect(button).toHaveAttribute('spx-rounded');
expect(button).toBeDisabled();
expect(button).toHaveAttribute('id', 'button');
});
it('should call onClick handler', () => {
const onClick = vi.fn();
const { getByRole } = render(() => <Button onClick={onClick}>Click me</Button>);
const button = getByRole('button');
button.click();
expect(onClick).toHaveBeenCalledTimes(1);
const event = onClick.mock.calls[0][0];
expect(event).toBeInstanceOf(MouseEvent);
});
it('should render loader and disable button when loading', async () => {
const onClick = vi.fn();
const { getByRole } = render(() => (
<Button onClick={onClick} loading={true}>
Click me
</Button>
));
const button = getByRole('button');
expect(button).toBeDisabled();
expect(button.querySelector('iconify-icon')).toBeInTheDocument();
expect(button.querySelector('iconify-icon')).toHaveAttribute(
'icon',
'svg-spinners:ring-resize',
);
button.click();
expect(onClick).not.toHaveBeenCalled();
});
it('should accept inline styles', () => {
const { getByRole } = render(() => <Button style={{ width: '1337px' }}>Click me</Button>);
const button = getByRole('button');
expect(button).toHaveStyle({ width: '1337px' });
});
});