Skip to content

Commit 5527d75

Browse files
committed
test(FileLink): test for FileLink added
1 parent 55ece9f commit 5527d75

File tree

3 files changed

+111
-4
lines changed

3 files changed

+111
-4
lines changed

src/components/FileLink/FileLink.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {Label, LabelProps} from '@gravity-ui/uikit';
44

55
import {LocationContext} from '../../context/locationContext';
66
import {FileLinkProps, TextSize, WithChildren} from '../../models';
7-
import {block, getLinkProps} from '../../utils';
7+
import {block, getLinkProps, getQaAttrubutes} from '../../utils';
88

99
import './FileLink.scss';
1010

@@ -57,23 +57,29 @@ const FileLink = (props: WithChildren<FileLinkProps>) => {
5757
onClick,
5858
tabIndex,
5959
urlTitle,
60+
qa,
6061
} = props;
62+
const qaAttributes = getQaAttrubutes(qa, 'link', 'link-container');
6163
const fileExt = getFileExt(href) as FileExtension;
6264
const labelTheme = (FileExtensionThemes[fileExt] || 'unknown') as LabelProps['theme'];
6365
const labelSize = LabelSizeMap[textSize];
6466

6567
return (
66-
<div className={b({ext: fileExt, type, size: textSize, theme}, className)}>
68+
<div
69+
className={b({ext: fileExt, type, size: textSize, theme}, className)}
70+
data-qa={qaAttributes.default}
71+
>
6772
<Label className={b('file-label')} size={labelSize} theme={labelTheme}>
6873
{fileExt}
6974
</Label>
70-
<div className={b('link')}>
75+
<div className={b('link')} data-qa={qaAttributes.linkContainer}>
7176
<a
7277
href={href}
7378
onClick={onClick}
7479
tabIndex={tabIndex}
7580
title={urlTitle}
7681
{...getLinkProps(href, hostname)}
82+
data-qa={qaAttributes.link}
7783
>
7884
{text}
7985
</a>
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import React from 'react';
2+
3+
import {render, screen} from '@testing-library/react';
4+
5+
import {testCustomClassName, testOnClick} from '../../../../test-utils/shared/common';
6+
import {FileLinkProps} from '../../../models';
7+
import {getQaAttrubutes} from '../../../utils';
8+
import FileLink from '../FileLink';
9+
10+
const fileLink = {
11+
href: 'qwe.pdf',
12+
text: 'Link to file',
13+
qa: 'file-link-component',
14+
};
15+
16+
const TYPES: Array<FileLinkProps['type']> = ['horizontal', 'vertical'];
17+
const TEXT_SIZRS: Array<FileLinkProps['textSize']> = ['s', 'xs', 'm', 'l'];
18+
const THEMES: Array<FileLinkProps['theme']> = ['default', 'dark', 'light'];
19+
20+
const qaAttributes = getQaAttrubutes(fileLink.qa, 'link', 'link-container');
21+
22+
describe('FileLink', () => {
23+
test('render FileLink by default', async () => {
24+
render(<FileLink {...fileLink} />);
25+
const component = screen.getByTestId(qaAttributes.default);
26+
27+
expect(component).toBeInTheDocument();
28+
expect(component).toBeVisible();
29+
});
30+
31+
test('render FileLink with text', async () => {
32+
render(<FileLink {...fileLink} />);
33+
const component = screen.getByTestId(qaAttributes.link);
34+
35+
expect(component).toHaveTextContent(fileLink.text);
36+
});
37+
38+
test('render FileLink with href', async () => {
39+
render(<FileLink {...fileLink} />);
40+
const component = screen.getByTestId(qaAttributes.link);
41+
42+
expect(component).toHaveAttribute('href', fileLink.href);
43+
});
44+
45+
test.each(new Array<FileLinkProps['type']>(...TYPES))('render with given "%s" type', (type) => {
46+
render(<FileLink {...fileLink} type={type} />);
47+
const cardBase = screen.getByTestId(qaAttributes.default);
48+
49+
expect(cardBase).toHaveClass(`pc-file-link_type_${type}`);
50+
});
51+
52+
test.each(new Array<FileLinkProps['textSize']>(...TEXT_SIZRS))(
53+
'render with given "%s" textSize',
54+
(textSize) => {
55+
render(<FileLink {...fileLink} textSize={textSize} />);
56+
const cardBase = screen.getByTestId(qaAttributes.default);
57+
58+
expect(cardBase).toHaveClass(`pc-file-link_size_${textSize}`);
59+
},
60+
);
61+
62+
test('add className', () => {
63+
testCustomClassName<FileLinkProps>({
64+
component: FileLink,
65+
props: fileLink,
66+
});
67+
});
68+
69+
test.each(new Array<FileLinkProps['theme']>(...THEMES))(
70+
'render with given "%s" theme',
71+
(theme) => {
72+
render(<FileLink {...fileLink} theme={theme} />);
73+
const cardBase = screen.getByTestId(qaAttributes.default);
74+
75+
expect(cardBase).toHaveClass(`pc-file-link_theme_${theme}`);
76+
},
77+
);
78+
79+
test('call onClick', async () => {
80+
testOnClick<FileLinkProps>({
81+
component: FileLink,
82+
props: fileLink,
83+
options: {qaId: qaAttributes.link},
84+
});
85+
});
86+
87+
test.each(new Array<number>(1, 2, 3))('render with given "%s" tabIndex', (tabIndex) => {
88+
render(<FileLink {...fileLink} tabIndex={tabIndex} />);
89+
const cardBase = screen.getByTestId(qaAttributes.link);
90+
91+
expect(cardBase).toHaveAttribute('tabIndex', tabIndex.toString());
92+
});
93+
94+
test('render FileLink with urlTitle', async () => {
95+
const urlTitle = 'Url Title';
96+
render(<FileLink {...fileLink} urlTitle={urlTitle} />);
97+
const component = screen.getByTestId(qaAttributes.link);
98+
99+
expect(component).toHaveAttribute('title', urlTitle);
100+
});
101+
});

src/models/constructor-items/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ export interface LinkProps extends AnalyticsEventsBase, Stylable, Tabbable {
181181
pixelEvents?: ButtonPixel;
182182
}
183183

184-
export interface FileLinkProps extends ClassNameProps, Tabbable {
184+
export interface FileLinkProps extends ClassNameProps, Tabbable, QAProps {
185185
href: string;
186186
text: ReactNode;
187187
type?: FileLinkType;

0 commit comments

Comments
 (0)