Skip to content

Commit 22a8bdd

Browse files
committed
test(FileLink): test for FileLink added
1 parent f1c17ef commit 22a8bdd

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

@@ -58,24 +58,30 @@ const FileLink = (props: WithChildren<FileLinkProps>) => {
5858
tabIndex,
5959
urlTitle,
6060
extraProps,
61+
qa,
6162
} = props;
63+
const qaAttributes = getQaAttrubutes(qa, 'link', 'link-container');
6264
const fileExt = getFileExt(href) as FileExtension;
6365
const labelTheme = (FileExtensionThemes[fileExt] || 'unknown') as LabelProps['theme'];
6466
const labelSize = LabelSizeMap[textSize];
6567

6668
return (
67-
<div className={b({ext: fileExt, type, size: textSize, theme}, className)}>
69+
<div
70+
className={b({ext: fileExt, type, size: textSize, theme}, className)}
71+
data-qa={qaAttributes.default}
72+
>
6873
<Label className={b('file-label')} size={labelSize} theme={labelTheme}>
6974
{fileExt}
7075
</Label>
71-
<div className={b('link')}>
76+
<div className={b('link')} data-qa={qaAttributes.linkContainer}>
7277
<a
7378
href={href}
7479
onClick={onClick}
7580
tabIndex={tabIndex}
7681
title={urlTitle}
7782
{...getLinkProps(href, hostname)}
7883
{...extraProps}
84+
data-qa={qaAttributes.link}
7985
>
8086
{text}
8187
</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
@@ -182,7 +182,7 @@ export interface LinkProps extends AnalyticsEventsBase, Stylable, Tabbable {
182182
extraProps?: HTMLProps<HTMLAnchorElement>;
183183
}
184184

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

0 commit comments

Comments
 (0)