-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.test.tsx
More file actions
131 lines (110 loc) · 4.44 KB
/
Button.test.tsx
File metadata and controls
131 lines (110 loc) · 4.44 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { fireEvent, render, screen } from '@testing-library/react';
import React from 'react';
import { describe, expect, it, vi } from 'vitest';
import { Button } from '../';
describe('Button component', () => {
it('Button onClick should be called correctly', () => {
const buttonClick = vi.fn();
render(<Button variant="primary" onClick={buttonClick} />);
const button = screen.getByRole('button');
button.click();
expect(buttonClick).toHaveBeenCalledOnce();
});
it('Button onKeyDown should be called correctly', () => {
const keydown = vi.fn();
render(<Button variant="primary" onKeyDown={keydown} />);
const button = screen.getByRole('button');
fireEvent.keyDown(button, { key: 'Enter', code: 'Enter' });
expect(keydown).toHaveBeenCalledOnce();
});
it('Button should handle default onClick and onKeyDown without crashing', () => {
const { getByRole } = render(<Button />);
const button = getByRole('button');
fireEvent.click(button);
fireEvent.keyDown(button, { key: 'Enter', code: 'Enter' });
});
it('Primary button should render correctly', () => {
const button = render(<Button variant="primary">Primary</Button>);
expect(button).toMatchSnapshot();
});
it('Secondary button should render correctly', () => {
const button = render(<Button variant="secondary">Secondary</Button>);
expect(button).toMatchSnapshot();
});
it('Ghost button should render correctly', () => {
const button = render(<Button variant="ghost">Ghost</Button>);
expect(button).toMatchSnapshot();
});
it('Destructive button should render correctly', () => {
const button = render(<Button variant="destructive">Destructive</Button>);
expect(button).toMatchSnapshot();
});
it('Primary disabled button should render correctly', () => {
const button = render(<Button variant="primary" disabled />);
expect(button).toMatchSnapshot();
});
it('Secondary disabled button should render correctly', () => {
const button = render(<Button variant="secondary" disabled />);
expect(button).toMatchSnapshot();
});
it('Ghost disabled button should render correctly', () => {
const button = render(<Button variant="ghost" disabled />);
expect(button).toMatchSnapshot();
});
it('Destructive disabled button should render correctly', () => {
const button = render(<Button variant="destructive" disabled />);
expect(button).toMatchSnapshot();
});
it('Primary medium button should render correctly', () => {
const button = render(<Button variant="primary" size="medium" />);
expect(button).toMatchSnapshot();
});
it('Primary loading button should render correctly', () => {
const button = render(<Button variant="primary" loading />);
expect(button).toMatchSnapshot();
});
it('Primary submit button should render correctly', () => {
const button = render(<Button variant="primary" type="submit" />);
expect(button).toMatchSnapshot();
});
it('Tertiary button should render correctly', () => {
const button = render(<Button variant="tertiary">Tertiary</Button>);
expect(button).toMatchSnapshot();
});
it('Tertiary disabled button should render correctly', () => {
const button = render(<Button variant="tertiary" disabled />);
expect(button).toMatchSnapshot();
});
it('Tertiary loading button should render correctly', () => {
const button = render(<Button variant="tertiary" loading />);
expect(button).toMatchSnapshot();
});
it('Tertiary medium button should render correctly', () => {
const button = render(<Button variant="tertiary" size="medium" />);
expect(button).toMatchSnapshot();
});
it('Destructive loading button should render correctly', () => {
const button = render(<Button variant="destructive" loading />);
expect(button).toMatchSnapshot();
});
it('Button with id, dataTest, autofocus, buttonDataCy and buttonChildrenDataCy renders correctly', () => {
const button = render(
<Button
variant="primary"
id="btn-id"
dataTest="btn-test"
autofocus
buttonDataCy="btn-cy"
buttonChildrenDataCy="btn-children-cy"
>
With Props
</Button>,
);
expect(button).toMatchSnapshot();
});
it('Button with unknown variant should render default styles', () => {
// @ts-expect-error testing invalid variant
const button = render(<Button variant="unknown">Unknown</Button>);
expect(button).toMatchSnapshot();
});
});