forked from patternfly/chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourcesCardBase.test.tsx
More file actions
285 lines (262 loc) · 10.1 KB
/
SourcesCardBase.test.tsx
File metadata and controls
285 lines (262 loc) · 10.1 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import '@testing-library/jest-dom';
import SourcesCardBase from './SourcesCardBase';
describe('SourcesCardBase', () => {
it('should render card correctly if one source with only a link is passed in', () => {
render(<SourcesCardBase sources={[{ link: '' }]} />);
expect(screen.getByText('Source 1')).toBeTruthy();
// no buttons or navigation when there is only 1 source
expect(screen.queryByRole('button')).toBeFalsy();
expect(screen.queryByText('1/1')).toBeFalsy();
});
it('should render card correctly if one source with a title is passed in', () => {
render(<SourcesCardBase sources={[{ title: 'How to make an apple pie', link: '' }]} />);
expect(screen.getByText('How to make an apple pie')).toBeTruthy();
});
it('should render card correctly if one source with a body is passed in', () => {
render(<SourcesCardBase sources={[{ link: '', body: 'To make an apple pie, you must first...' }]} />);
expect(screen.getByText('To make an apple pie, you must first...')).toBeTruthy();
});
it('should render card correctly if one source with a title and body is passed in', () => {
render(
<SourcesCardBase
sources={[{ title: 'How to make an apple pie', link: '', body: 'To make an apple pie, you must first...' }]}
/>
);
expect(screen.getByText('How to make an apple pie')).toBeTruthy();
expect(screen.getByText('To make an apple pie, you must first...')).toBeTruthy();
});
it('should render multiple cards correctly', () => {
render(
<SourcesCardBase
sources={[
{ title: 'How to make an apple pie', link: '' },
{ title: 'How to make cookies', link: '' }
]}
/>
);
expect(screen.getByText('How to make an apple pie')).toBeTruthy();
expect(screen.getByText('1/2')).toBeTruthy();
screen.getByRole('button', { name: /Go to previous page/i });
screen.getByRole('button', { name: /Go to next page/i });
});
it('should navigate between cards correctly', async () => {
render(
<SourcesCardBase
sources={[
{ title: 'How to make an apple pie', link: '' },
{ title: 'How to make cookies', link: '' }
]}
/>
);
expect(screen.getByText('How to make an apple pie')).toBeTruthy();
expect(screen.getByText('1/2')).toBeTruthy();
expect(screen.getByRole('button', { name: /Go to previous page/i })).toBeDisabled();
await userEvent.click(screen.getByRole('button', { name: /Go to next page/i }));
expect(screen.queryByText('How to make an apple pie')).toBeFalsy();
expect(screen.getByText('How to make cookies')).toBeTruthy();
expect(screen.getByText('2/2')).toBeTruthy();
expect(screen.getByRole('button', { name: /Go to previous page/i })).toBeEnabled();
expect(screen.getByRole('button', { name: /Go to next page/i })).toBeDisabled();
});
it('should apply className appropriately', () => {
render(
<SourcesCardBase
sources={[
{ title: 'How to make an apple pie', link: '' },
{ title: 'How to make cookies', link: '' }
]}
className="test"
/>
);
const element = screen.getByRole('navigation');
expect(element).toHaveClass('test');
});
it('should disable pagination appropriately', () => {
render(
<SourcesCardBase
sources={[
{ title: 'How to make an apple pie', link: '' },
{ title: 'How to make cookies', link: '' }
]}
isDisabled
/>
);
expect(screen.getByRole('button', { name: /Go to previous page/i })).toBeDisabled();
expect(screen.getByRole('button', { name: /Go to next page/i })).toBeDisabled();
});
it('should render navigation aria label appropriately', () => {
render(
<SourcesCardBase
sources={[
{ title: 'How to make an apple pie', link: '' },
{ title: 'How to make cookies', link: '' }
]}
/>
);
expect(screen.getByRole('navigation', { name: /Pagination/i })).toBeTruthy();
});
it('should change paginationAriaLabel appropriately', () => {
render(
<SourcesCardBase
sources={[
{ title: 'How to make an apple pie', link: '' },
{ title: 'How to make cookies', link: '' }
]}
paginationAriaLabel="Navegación"
/>
);
expect(screen.getByRole('navigation', { name: /Navegación/i })).toBeTruthy();
});
it('should change toNextPageAriaLabel appropriately', () => {
render(
<SourcesCardBase
sources={[
{ title: 'How to make an apple pie', link: '' },
{ title: 'How to make cookies', link: '' }
]}
toNextPageAriaLabel="Pase a la siguiente página"
/>
);
expect(screen.getByRole('button', { name: /Pase a la siguiente página/i })).toBeTruthy();
});
it('should change toPreviousPageAriaLabel appropriately', () => {
render(
<SourcesCardBase
sources={[
{ title: 'How to make an apple pie', link: '' },
{ title: 'How to make cookies', link: '' }
]}
toPreviousPageAriaLabel="Presione para regresar a la página anterior"
/>
);
expect(screen.getByRole('button', { name: /Presione para regresar a la página anterior/i })).toBeTruthy();
});
it('should call onNextClick appropriately', async () => {
const spy = jest.fn();
render(
<SourcesCardBase
sources={[
{ title: 'How to make an apple pie', link: '' },
{ title: 'How to make cookies', link: '' }
]}
onNextClick={spy}
/>
);
await userEvent.click(screen.getByRole('button', { name: /Go to next page/i }));
expect(spy).toHaveBeenCalled();
});
it('should call onPreviousClick appropriately', async () => {
const spy = jest.fn();
render(
<SourcesCardBase
sources={[
{ title: 'How to make an apple pie', link: '' },
{ title: 'How to make cookies', link: '' }
]}
onPreviousClick={spy}
/>
);
await userEvent.click(screen.getByRole('button', { name: /Go to next page/i }));
await userEvent.click(screen.getByRole('button', { name: /Go to previous page/i }));
expect(spy).toHaveBeenCalled();
});
it('should call onSetPage appropriately', async () => {
const spy = jest.fn();
render(
<SourcesCardBase
sources={[
{ title: 'How to make an apple pie', link: '' },
{ title: 'How to make cookies', link: '' }
]}
onSetPage={spy}
/>
);
await userEvent.click(screen.getByRole('button', { name: /Go to next page/i }));
expect(spy).toHaveBeenCalledTimes(1);
await userEvent.click(screen.getByRole('button', { name: /Go to previous page/i }));
expect(spy).toHaveBeenCalledTimes(2);
});
it('should handle showMore appropriately', async () => {
render(
<SourcesCardBase
sources={[
{
title: 'Getting started with Red Hat OpenShift',
link: '#',
body: 'Red Hat OpenShift on IBM Cloud is a managed offering to create your own cluster of compute hosts where you can deploy and manage containerized apps on IBM Cloud ...',
hasShowMore: true
},
{
title: 'Azure Red Hat OpenShift documentation',
link: '#',
body: 'Microsoft Azure Red Hat OpenShift allows you to deploy a production ready Red Hat OpenShift cluster in Azure ...'
},
{
title: 'OKD Documentation: Home',
link: '#',
body: 'OKD is a distribution of Kubernetes optimized for continuous application development and multi-tenant deployment. OKD also serves as the upstream code base upon ...'
}
]}
/>
);
expect(screen.getByRole('region')).toHaveAttribute('class', 'pf-v6-c-expandable-section__content');
});
it('should call onClick appropriately', async () => {
const spy = jest.fn();
render(<SourcesCardBase sources={[{ title: 'How to make an apple pie', link: '', onClick: spy }]} />);
await userEvent.click(screen.getByRole('link', { name: /How to make an apple pie/i }));
expect(spy).toHaveBeenCalled();
});
it('should apply titleProps appropriately', () => {
render(
<SourcesCardBase sources={[{ title: 'How to make an apple pie', link: '', titleProps: { className: 'test' } }]} />
);
expect(screen.getByRole('link', { name: /How to make an apple pie/i })).toHaveClass('test');
});
it('should render with wrap layout when layout prop is set to wrap', () => {
render(
<SourcesCardBase
layout="wrap"
sources={[
{ title: 'How to make an apple pie', link: '' },
{ title: 'How to make cookies', link: '' },
{ title: 'How to make a sandwich', link: '' }
]}
/>
);
expect(screen.getByText('How to make an apple pie')).toBeVisible();
expect(screen.getByText('How to make cookies')).toBeVisible();
expect(screen.getByText('How to make a sandwich')).toBeVisible();
expect(screen.queryByRole('navigation')).not.toBeInTheDocument();
expect(screen.queryByText('1/3')).not.toBeInTheDocument();
});
it('should apply default cardMaxWidth when using wrap layout', () => {
render(
<SourcesCardBase
layout="wrap"
sources={[
{ title: 'How to make an apple pie', link: '' },
{ title: 'How to make cookies', link: '' }
]}
/>
);
const firstCard = screen.getByText('How to make an apple pie').closest('.pf-chatbot__sources-card');
expect(firstCard).toHaveStyle({ maxWidth: '400px' });
});
it('should apply custom cardMaxWidth when using wrap layout', () => {
render(
<SourcesCardBase
layout="wrap"
sources={[
{ title: 'How to make an apple pie', link: '' },
{ title: 'How to make cookies', link: '' }
]}
cardMaxWidth="500px"
/>
);
const firstCard = screen.getByText('How to make an apple pie').closest('.pf-chatbot__sources-card');
expect(firstCard).toHaveStyle({ maxWidth: '500px' });
});
});