Skip to content

Commit be2cdf2

Browse files
authored
test(BackgroundImage): test for BackgroundImage added (#169)
* test(BackgroundImage): test for BackgroundImage added
1 parent 9fbbefe commit be2cdf2

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed

src/components/BackgroundImage/BackgroundImage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import './BackgroundImage.scss';
99
const b = block('storage-background-image');
1010

1111
const BackgroundImage = (props: WithChildren<BackgroundImageProps>) => {
12-
const {children, src, desktop, className, imageClassName, style, hide} = props;
12+
const {children, src, desktop, className, imageClassName, style, hide, qa} = props;
1313

1414
return (
15-
<div className={b(null, className)} style={style}>
15+
<div className={b(null, className)} style={style} data-qa={qa}>
1616
{(src || desktop) && !hide && <Image {...props} className={b('img', imageClassName)} />}
1717
{children && <div className={b('container')}>{children}</div>}
1818
</div>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import React from 'react';
2+
3+
import {render, screen} from '@testing-library/react';
4+
import {BackgroundImageProps} from 'src/models';
5+
6+
import {testCustomClassName, testCustomStyle} from '../../../../test-utils/shared/common';
7+
import BackgroundImage from '../BackgroundImage';
8+
9+
const qaId = 'background-image-component';
10+
11+
const imageSrc =
12+
'https://storage.yandexcloud.net/cloud-www-assets/constructor/storybook/images/img-gray.png';
13+
14+
describe('BackgroundImage', () => {
15+
test('Render BackgroundImage by default', async () => {
16+
render(<BackgroundImage qa={qaId} />);
17+
18+
const component = screen.getByTestId(qaId);
19+
expect(component).toBeInTheDocument();
20+
expect(component).toBeVisible();
21+
});
22+
23+
test('add image as src prop', () => {
24+
render(<BackgroundImage src={imageSrc} />);
25+
const component = screen.getByRole('img');
26+
27+
expect(component).toHaveAttribute('src', imageSrc);
28+
});
29+
30+
test('add image as desktop prop', () => {
31+
render(<BackgroundImage desktop={imageSrc} />);
32+
const component = screen.getByRole('img');
33+
34+
expect(component).toHaveAttribute('src', imageSrc);
35+
});
36+
37+
test('should hide image', () => {
38+
render(<BackgroundImage src={imageSrc} hide qa={qaId} />);
39+
const component = screen.getByTestId(qaId);
40+
const imageComponent = screen.queryByRole('img');
41+
42+
expect(component).toBeInTheDocument();
43+
expect(component).toBeVisible();
44+
expect(imageComponent).not.toBeInTheDocument();
45+
});
46+
47+
test('should render children', () => {
48+
const childText = 'child-component';
49+
render(
50+
<BackgroundImage src={imageSrc} hide qa={qaId}>
51+
<p>{childText}</p>
52+
</BackgroundImage>,
53+
);
54+
const component = screen.getByText(childText);
55+
56+
expect(component).toBeInTheDocument();
57+
expect(component).toBeVisible();
58+
});
59+
60+
test('add className', () => {
61+
testCustomClassName<BackgroundImageProps>({
62+
component: BackgroundImage,
63+
props: {qa: qaId},
64+
});
65+
});
66+
67+
test('add style', () => {
68+
testCustomStyle<BackgroundImageProps>({
69+
component: BackgroundImage,
70+
props: {qa: qaId},
71+
});
72+
});
73+
74+
test('add className to image', () => {
75+
const className = 'my-class';
76+
77+
render(<BackgroundImage src={imageSrc} imageClassName={className} />);
78+
const component = screen.getByRole('img');
79+
80+
expect(component).toHaveClass(className);
81+
});
82+
});

src/components/Image/Image.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface ImageProps extends Partial<ImageObjectProps>, Partial<ImageDevi
1313
className?: string;
1414
onClick?: MouseEventHandler;
1515
containerClassName?: string;
16+
qa?: string;
1617
}
1718

1819
const checkWebP = (src: string) => {
@@ -32,6 +33,7 @@ const Image = (props: ImageProps) => {
3233
className,
3334
onClick,
3435
containerClassName,
36+
qa,
3537
} = props;
3638
const [imgLoadingError, setImgLoadingError] = useState(false);
3739

@@ -48,7 +50,7 @@ const Image = (props: ImageProps) => {
4850
imgLoadingError;
4951

5052
return (
51-
<picture className={containerClassName}>
53+
<picture className={containerClassName} data-qa={qa}>
5254
{mobile && (
5355
<Fragment>
5456
{!disableWebp && (

src/models/constructor-items/common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ export interface BackgroundImageProps
131131
style?: CSSProperties;
132132
imageClassName?: string;
133133
hide?: boolean;
134+
qa?: string;
134135
}
135136

136137
//components props

0 commit comments

Comments
 (0)