Skip to content

Commit fd8a543

Browse files
test: test className and style on every component (#530)
[ci skip]
1 parent 888d069 commit fd8a543

File tree

6 files changed

+123
-84
lines changed

6 files changed

+123
-84
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"@storybook/storybook-deployer": "^2.8.5",
5353
"@testing-library/react": "^10.0.4",
5454
"@types/enzyme": "^3.10.5",
55-
"@types/jest": "^25.2.1",
55+
"@types/jest": "^25.2.3",
5656
"@types/react": "^16.9.34",
5757
"@types/react-dom": "^16.9.6",
5858
"@types/sinon": "^9.0.0",

packages/main/src/components/Bar/Bar.test.tsx

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,65 @@
11
import { createPassThroughPropsTest } from '@shared/tests/utils';
2-
import { mount } from 'enzyme';
2+
import { render, screen, cleanup } from '@testing-library/react';
33
import { Bar } from '@ui5/webcomponents-react/lib/Bar';
4+
import { BarDesign } from '@ui5/webcomponents-react/lib/BarDesign';
45
import React from 'react';
56

6-
const createRenderLabel = (text) => () => {
7-
return <p>{text}</p>;
8-
};
7+
const text1 = 'Content Left';
8+
const text2 = 'Content Middle';
9+
const text3 = 'Content Right';
910

1011
describe('Bar', () => {
12+
afterEach(cleanup);
13+
1114
test('Renders with default Props', () => {
12-
expect(mount(<Bar />).render()).toMatchSnapshot();
15+
expect(render(<Bar />).asFragment()).toMatchSnapshot();
1316
});
1417

1518
test('Render all content', () => {
16-
const wrapper = mount(
17-
<Bar
18-
contentLeft={<div>Content Left</div>}
19-
contentMiddle={<div>Content Middle</div>}
20-
contentRight={<div>Content Right</div>}
21-
/>
19+
const utils = render(
20+
<Bar contentLeft={<div>{text1}</div>} contentMiddle={<div>{text2}</div>} contentRight={<div>{text3}</div>} />
2221
);
23-
expect(wrapper.render()).toMatchSnapshot();
22+
23+
expect(screen.getByText(text1).textContent).toEqual(text1);
24+
expect(screen.getByText(text2).textContent).toEqual(text2);
25+
expect(screen.getByText(text3).textContent).toEqual(text3);
26+
27+
expect(utils.asFragment()).toMatchSnapshot();
2428
});
2529

26-
test('Render all content', () => {
27-
const text1 = 'Content Left';
28-
const text2 = 'Content Middle';
29-
const text3 = 'Content Right';
30+
test('Test classes for all modes', () => {
31+
const { rerender } = render(
32+
<Bar
33+
design={BarDesign.Footer}
34+
contentLeft={<div>{text1}</div>}
35+
contentMiddle={<div>{text2}</div>}
36+
contentRight={<div>{text3}</div>}
37+
data-testid={'bar'}
38+
/>
39+
);
40+
expect(screen.getByTestId('bar').className).toMatch(/footer/);
3041

31-
const wrapper = mount(
32-
<Bar contentLeft={<div>{text1}</div>} contentMiddle={<div>{text2}</div>} contentRight={<div>{text3}</div>} />
42+
rerender(
43+
<Bar
44+
design={BarDesign.SubHeader}
45+
contentLeft={<div>{text1}</div>}
46+
contentMiddle={<div>{text2}</div>}
47+
contentRight={<div>{text3}</div>}
48+
data-testid={'bar'}
49+
/>
3350
);
34-
expect(wrapper.text()).toContain(text1);
35-
expect(wrapper.text()).toContain(text2);
36-
expect(wrapper.text()).toContain(text3);
37-
});
3851

39-
test('Has Fiori 3 padding', () => {
40-
const wrapper = mount(
41-
<Bar renderContentLeft={createRenderLabel('Test')} renderContentRight={createRenderLabel('Right')} />
42-
)
43-
.find('div')
44-
.at(1);
45-
const node = wrapper.getDOMNode();
46-
expect(window.getComputedStyle(node).paddingLeft).toEqual('0.5rem');
52+
expect(screen.getByTestId('bar').className).toMatch(/subHeader/);
53+
rerender(
54+
<Bar
55+
design={BarDesign.FloatingFooter}
56+
contentLeft={<div>{text1}</div>}
57+
contentMiddle={<div>{text2}</div>}
58+
contentRight={<div>{text3}</div>}
59+
data-testid={'bar'}
60+
/>
61+
);
62+
expect(screen.getByTestId('bar').className).toMatch(/floatingFooter/);
4763
});
4864

4965
createPassThroughPropsTest(Bar);
Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,65 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`Bar Render all content 1`] = `
4-
<div
5-
class="Bar-bar-0 Bar-auto-0"
6-
data-bar-part="Root"
7-
>
4+
<DocumentFragment>
85
<div
9-
class="Bar-left-0"
10-
data-bar-part="Left"
11-
>
12-
<div>
13-
Content Left
14-
</div>
15-
</div>
16-
<div
17-
class="Bar-center-0"
18-
data-bar-part="Center"
6+
class="Bar-bar-0-2-1 Bar-auto-0-2-6"
7+
data-bar-part="Root"
198
>
209
<div
21-
class="Bar-inner-0"
10+
class="Bar-left-0-2-2"
11+
data-bar-part="Left"
2212
>
2313
<div>
24-
Content Middle
14+
Content Left
2515
</div>
2616
</div>
27-
</div>
28-
<div
29-
class="Bar-right-0"
30-
data-bar-part="Right"
31-
>
32-
<div>
33-
Content Right
17+
<div
18+
class="Bar-center-0-2-3"
19+
data-bar-part="Center"
20+
>
21+
<div
22+
class="Bar-inner-0-2-4"
23+
>
24+
<div>
25+
Content Middle
26+
</div>
27+
</div>
28+
</div>
29+
<div
30+
class="Bar-right-0-2-5"
31+
data-bar-part="Right"
32+
>
33+
<div>
34+
Content Right
35+
</div>
3436
</div>
3537
</div>
36-
</div>
38+
</DocumentFragment>
3739
`;
3840

3941
exports[`Bar Renders with default Props 1`] = `
40-
<div
41-
class="Bar-bar-0 Bar-auto-0"
42-
data-bar-part="Root"
43-
>
44-
<div
45-
class="Bar-left-0"
46-
data-bar-part="Left"
47-
/>
42+
<DocumentFragment>
4843
<div
49-
class="Bar-center-0"
50-
data-bar-part="Center"
44+
class="Bar-bar-0-2-1 Bar-auto-0-2-6"
45+
data-bar-part="Root"
5146
>
5247
<div
53-
class="Bar-inner-0"
48+
class="Bar-left-0-2-2"
49+
data-bar-part="Left"
50+
/>
51+
<div
52+
class="Bar-center-0-2-3"
53+
data-bar-part="Center"
54+
>
55+
<div
56+
class="Bar-inner-0-2-4"
57+
/>
58+
</div>
59+
<div
60+
class="Bar-right-0-2-5"
61+
data-bar-part="Right"
5462
/>
5563
</div>
56-
<div
57-
class="Bar-right-0"
58-
data-bar-part="Right"
59-
/>
60-
</div>
64+
</DocumentFragment>
6165
`;

packages/main/src/components/FilterBar/index.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const useStyles = createComponentStyles(styles, { name: 'FilterBar' });
2121
* <code>import { FilterBar } from '@ui5/webcomponents-react/lib/FilterBar';</code>
2222
*/
2323
const FilterBar: FC<FilterBarPropTypes> = forwardRef((props: FilterBarPropTypes, ref: RefObject<HTMLDivElement>) => {
24-
const { children, search, variants } = props as FilterBarInternalProps;
24+
const { children, search, variants, className, style, tooltip, slot } = props as FilterBarInternalProps;
2525
const [showFilters, setShowFilters] = useState(true);
2626

2727
const classes = useStyles();
@@ -39,8 +39,20 @@ const FilterBar: FC<FilterBarPropTypes> = forwardRef((props: FilterBarPropTypes,
3939

4040
const passThroughProps = usePassThroughHtmlProps(props);
4141

42+
const filterBarClasses = StyleClassHelper.of(classes.outerContainer);
43+
if (className) {
44+
filterBarClasses.put(className);
45+
}
46+
4247
return (
43-
<div ref={ref} className={classes.outerContainer} {...passThroughProps}>
48+
<div
49+
ref={ref}
50+
className={filterBarClasses.valueOf()}
51+
style={style}
52+
title={tooltip}
53+
slot={slot}
54+
{...passThroughProps}
55+
>
4456
<div className={classes.filterBarHeader}>
4557
{variants}
4658
{search && <div className={classes.vLine}> {search} </div>}

shared/tests/utils.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { mount } from 'enzyme';
21
import React, { ComponentType } from 'react';
2+
import { render, screen } from '@testing-library/react';
33

44
export const modifyObjectProperty = (object: any, attr: string, value: any) => {
55
Object.defineProperty(object, attr, {
@@ -20,23 +20,30 @@ export const setUserAgentString = (userAgent) => {
2020

2121
export const createPassThroughPropsTest = (Component: ComponentType<any>, props = {}) => {
2222
test('Pass Through HTML Standard Props', () => {
23-
const wrapper = mount(
23+
render(
2424
<Component
25+
data-testid={'component-to-be-tested'}
2526
data-special-test-prop="data-prop"
2627
aria-labelledby="aria-prop"
2728
id="element-id"
2829
disabled-custom-prop
30+
className="thisClassIsUsedForTestingPurposesOnly"
31+
style={{ pointerEvents: 'none' }}
2932
{...props}
3033
/>
3134
);
32-
const html = wrapper.html();
3335

34-
expect(html).toMatch(/data-special-test-prop="data-prop"/);
35-
expect(html).toMatch(/aria-labelledby="aria-prop"/);
36+
const element = screen.getByTestId('component-to-be-tested');
37+
38+
expect(element.classList.contains('thisClassIsUsedForTestingPurposesOnly')).toBeTruthy();
39+
expect(element.style.pointerEvents).toEqual('none');
40+
41+
expect(element.dataset.specialTestProp).toEqual('data-prop');
42+
expect(element.getAttribute('aria-labelledby')).toEqual('aria-prop');
3643
// special handling for ObjectPage Sections because of own ID handling...
3744
if (Component.displayName !== 'ObjectPageSection' && Component.displayName !== 'ObjectPageSubSection') {
38-
expect(html).toMatch(/id="element-id"/);
45+
expect(element.id).toBe('element-id');
3946
}
40-
expect(html).not.toMatch(/disabled-custom-prop/);
47+
expect(element.hasAttribute('disabled-custom-prop')).toBeFalsy();
4148
});
4249
};

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3755,10 +3755,10 @@
37553755
"@types/istanbul-lib-coverage" "*"
37563756
"@types/istanbul-lib-report" "*"
37573757

3758-
"@types/jest@^25.2.1":
3759-
version "25.2.1"
3760-
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-25.2.1.tgz#9544cd438607955381c1bdbdb97767a249297db5"
3761-
integrity sha512-msra1bCaAeEdkSyA0CZ6gW1ukMIvZ5YoJkdXw/qhQdsuuDlFTcEUrUw8CLCPt2rVRUfXlClVvK2gvPs9IokZaA==
3758+
"@types/jest@^25.2.3":
3759+
version "25.2.3"
3760+
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-25.2.3.tgz#33d27e4c4716caae4eced355097a47ad363fdcaf"
3761+
integrity sha512-JXc1nK/tXHiDhV55dvfzqtmP4S3sy3T3ouV2tkViZgxY/zeUkcpQcQPGRlgF4KmWzWW5oiWYSZwtCB+2RsE4Fw==
37623762
dependencies:
37633763
jest-diff "^25.2.1"
37643764
pretty-format "^25.2.1"

0 commit comments

Comments
 (0)