-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathaction-button.component.spec.tsx
More file actions
208 lines (182 loc) · 5.19 KB
/
Copy pathaction-button.component.spec.tsx
File metadata and controls
208 lines (182 loc) · 5.19 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import { fireEvent, render, screen } from '@testing-library/react';
import { vi } from 'vitest';
import { ActionButton } from './action-button.component';
import { ShortcutOptions } from '@/common/shortcut';
import { useModalDialogContext } from '@/core/providers';
vi.mock('@/core/providers', () => ({
useModalDialogContext: vi.fn(),
}));
describe('ActionButton', () => {
let onClick: () => void;
let shortcutOptions: ShortcutOptions;
beforeEach(() => {
vi.mocked(useModalDialogContext).mockReturnValue({
modalDialog: {
isOpen: false,
selectedComponent: null,
title: '',
},
openModal: vi.fn(),
closeModal: vi.fn(),
});
onClick = vi.fn();
shortcutOptions = {
description: 'TestDescription',
id: 'shortcut',
targetKey: ['a'],
targetKeyLabel: 'A',
};
Object.defineProperty(window.navigator, 'userAgent', {
value: 'Mac',
configurable: true,
});
});
it('should render the button with the provided label and icon', () => {
render(
<ActionButton
icon={<span>Icon</span>}
label="Label"
onClick={onClick}
shortcutOptions={shortcutOptions}
/>
);
expect(screen.getByText('Icon')).not.toBeNull();
expect(screen.getByText('Label')).not.toBeNull();
});
it('should call the onClick callback when the button is clicked', () => {
const { getByText } = render(
<ActionButton
icon={<span>Icon</span>}
label="Label"
onClick={onClick}
shortcutOptions={shortcutOptions}
/>
);
fireEvent.click(getByText('Label'));
expect(onClick).toHaveBeenCalled();
});
describe('tooltip display', () => {
it('should render system modifier correctly', () => {
// Test Mac
const { getByRole, rerender } = render(
<ActionButton
icon={<span>Icon</span>}
label="Label"
onClick={onClick}
shortcutOptions={{
...shortcutOptions,
modifierType: 'system',
}}
/>
);
expect(getByRole('tooltip').textContent).toBe('(⌘ + A)');
// Test Windows
Object.defineProperty(window.navigator, 'userAgent', {
value: 'Windows',
configurable: true,
});
rerender(
<ActionButton
icon={<span>Icon</span>}
label="Label"
onClick={onClick}
shortcutOptions={{
...shortcutOptions,
modifierType: 'system',
}}
/>
);
expect(getByRole('tooltip').textContent).toBe('(Ctrl + A)');
});
it('should render alt tooltip correctly', () => {
const { getByRole } = render(
<ActionButton
icon={<span>Icon</span>}
label="Label"
onClick={onClick}
shortcutOptions={{
...shortcutOptions,
modifierType: 'alt',
}}
/>
);
expect(getByRole('tooltip').textContent).toBe('(Alt + A)');
});
it('should render mo-modifier tooltip correctly', () => {
const { getByRole } = render(
<ActionButton
icon={<span>Icon</span>}
label="Label"
onClick={onClick}
shortcutOptions={{
...shortcutOptions,
modifierType: 'none',
}}
/>
);
expect(getByRole('tooltip').textContent).toBe('(A)');
});
it('should not show tooltip when disabled', () => {
const { queryByRole } = render(
<ActionButton
icon={<span>Icon</span>}
label="Label"
onClick={onClick}
disabled
shortcutOptions={shortcutOptions}
/>
);
expect(queryByRole('tooltip')).toBeNull();
});
});
it('should disable the button if the disabled prop is true', () => {
const { getByText } = render(
<ActionButton
icon={<span>Icon</span>}
label="Label"
onClick={onClick}
disabled
shortcutOptions={shortcutOptions}
/>
);
const button = getByText('Label').closest('button');
expect(button).toHaveProperty('disabled', true);
});
it('should hide the label when showLabel is false', () => {
render(
<ActionButton
icon={<span>Icon</span>}
label="Label"
onClick={onClick}
showLabel={false}
shortcutOptions={shortcutOptions}
/>
);
expect(screen.queryByText('Label')).toBeNull();
});
it('should apply tooltipBottom by default when tooltipPosition is not provided', () => {
const { getByRole } = render(
<ActionButton
icon={<span>Icon</span>}
label="Label"
onClick={onClick}
shortcutOptions={shortcutOptions}
/>
);
const tooltip = getByRole('tooltip');
expect(tooltip.className).toContain('tooltipBottom');
});
it('should apply tooltipTop class when tooltipPosition is top', () => {
const { getByRole } = render(
<ActionButton
icon={<span>Icon</span>}
label="Label"
onClick={onClick}
tooltipPosition="top"
shortcutOptions={shortcutOptions}
/>
);
const tooltip = getByRole('tooltip');
expect(tooltip.className).toContain('tooltipTop');
});
});