Skip to content

Commit ca42cf9

Browse files
authored
Add @storybook/testing-react (elastic#103004)
1 parent 99d3f8b commit ca42cf9

File tree

9 files changed

+109
-107
lines changed

9 files changed

+109
-107
lines changed

.eslintrc.js

+12
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,18 @@ module.exports = {
857857
'react-hooks/exhaustive-deps': ['error', { additionalHooks: '^useFetcher$' }],
858858
},
859859
},
860+
{
861+
files: ['x-pack/plugins/apm/**/*.stories.*', 'x-pack/plugins/observability/**/*.stories.*'],
862+
rules: {
863+
'react/function-component-definition': [
864+
'off',
865+
{
866+
namedComponents: 'function-declaration',
867+
unnamedComponents: 'arrow-function',
868+
},
869+
],
870+
},
871+
},
860872

861873
/**
862874
* Fleet overrides

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@
493493
"@storybook/core-events": "^6.1.20",
494494
"@storybook/node-logger": "^6.1.20",
495495
"@storybook/react": "^6.1.20",
496+
"@storybook/testing-react": "^0.0.17",
496497
"@storybook/theming": "^6.1.20",
497498
"@testing-library/dom": "^7.30.3",
498499
"@testing-library/jest-dom": "^5.11.10",

x-pack/plugins/apm/public/components/app/ErrorGroupDetails/DetailView/ExceptionStacktrace.test.tsx

-42
This file was deleted.

x-pack/plugins/apm/public/components/app/ErrorGroupDetails/DetailView/ExceptionStacktrace.stories.tsx x-pack/plugins/apm/public/components/app/ErrorGroupDetails/DetailView/exception_stacktrace.stories.tsx

+40-38
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,31 @@
55
* 2.0.
66
*/
77

8-
import React, { ComponentType } from 'react';
8+
import { Story } from '@storybook/react';
9+
import React, { ComponentProps, ComponentType } from 'react';
910
import { EuiThemeProvider } from '../../../../../../../../src/plugins/kibana_react/common';
10-
import { Exception } from '../../../../../typings/es_schemas/raw/error_raw';
11-
import { ExceptionStacktrace } from './ExceptionStacktrace';
11+
import { ExceptionStacktrace } from './exception_stacktrace';
12+
13+
type Args = ComponentProps<typeof ExceptionStacktrace>;
1214

1315
export default {
1416
title: 'app/ErrorGroupDetails/DetailView/ExceptionStacktrace',
1517
component: ExceptionStacktrace,
1618
decorators: [
17-
(Story: ComponentType) => {
18-
return (
19-
<EuiThemeProvider>
20-
<Story />
21-
</EuiThemeProvider>
22-
);
23-
},
19+
(StoryComponent: ComponentType) => (
20+
<EuiThemeProvider>
21+
<StoryComponent />
22+
</EuiThemeProvider>
23+
),
2424
],
2525
};
2626

27-
export function JavaWithLongLines() {
28-
const exceptions: Exception[] = [
27+
export const JavaWithLongLines: Story<Args> = (args) => (
28+
<ExceptionStacktrace {...args} />
29+
);
30+
JavaWithLongLines.args = {
31+
codeLanguage: 'java',
32+
exceptions: [
2933
{
3034
stacktrace: [
3135
{
@@ -1734,22 +1738,23 @@ export function JavaWithLongLines() {
17341738
'Null return value from advice does not match primitive return type for: public abstract double co.elastic.apm.opbeans.repositories.Numbers.getRevenue()',
17351739
type: 'org.springframework.aop.AopInvocationException',
17361740
},
1737-
];
1738-
1739-
return <ExceptionStacktrace codeLanguage="java" exceptions={exceptions} />;
1740-
}
1741+
],
1742+
};
17411743
JavaWithLongLines.decorators = [
1742-
(Story: ComponentType) => {
1743-
return (
1744-
<div style={{ border: '1px dotted #aaa', width: 768 }}>
1745-
<Story />
1746-
</div>
1747-
);
1748-
},
1744+
(StoryComponent: ComponentType) => (
1745+
<div style={{ border: '1px dotted #aaa', width: 768 }}>
1746+
<StoryComponent />
1747+
</div>
1748+
),
17491749
];
17501750

1751-
export function JavaScriptWithSomeContext() {
1752-
const exceptions: Exception[] = [
1751+
export const JavaScriptWithSomeContext: Story<Args> = (args) => (
1752+
<ExceptionStacktrace {...args} />
1753+
);
1754+
JavaScriptWithSomeContext.storyName = 'JavaScript With Some Context';
1755+
JavaScriptWithSomeContext.args = {
1756+
codeLanguage: 'javascript',
1757+
exceptions: [
17531758
{
17541759
code: '503',
17551760
stacktrace: [
@@ -1870,16 +1875,15 @@ export function JavaScriptWithSomeContext() {
18701875
type: 'Error',
18711876
message: 'Unexpected APM Server response when polling config',
18721877
},
1873-
];
1874-
1875-
return (
1876-
<ExceptionStacktrace codeLanguage="javascript" exceptions={exceptions} />
1877-
);
1878-
}
1879-
JavaScriptWithSomeContext.storyName = 'JavaScript With Some Context';
1878+
],
1879+
};
18801880

1881-
export function RubyWithContextAndLibraryFrames() {
1882-
const exceptions: Exception[] = [
1881+
export const RubyWithContextAndLibraryFrames: Story<Args> = (args) => (
1882+
<ExceptionStacktrace {...args} />
1883+
);
1884+
RubyWithContextAndLibraryFrames.args = {
1885+
codeLanguage: 'ruby',
1886+
exceptions: [
18831887
{
18841888
stacktrace: [
18851889
{
@@ -2536,7 +2540,5 @@ export function RubyWithContextAndLibraryFrames() {
25362540
message: "Couldn't find Order with 'id'=956",
25372541
type: 'ActiveRecord::RecordNotFound',
25382542
},
2539-
];
2540-
2541-
return <ExceptionStacktrace codeLanguage="ruby" exceptions={exceptions} />;
2542-
}
2543+
],
2544+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import { composeStories } from '@storybook/testing-react';
9+
import React from 'react';
10+
import { mount } from 'enzyme';
11+
import * as stories from './exception_stacktrace.stories';
12+
13+
const { JavaWithLongLines } = composeStories(stories);
14+
15+
describe('ExceptionStacktrace', () => {
16+
describe('render', () => {
17+
describe('with stacktraces', () => {
18+
it('renders the stacktraces', () => {
19+
expect(mount(<JavaWithLongLines />).find('Stacktrace')).toHaveLength(3);
20+
});
21+
});
22+
23+
describe('with more than one stack trace', () => {
24+
it('renders cause stacktraces', () => {
25+
expect(
26+
mount(<JavaWithLongLines />).find('CauseStacktrace')
27+
).toHaveLength(2);
28+
});
29+
});
30+
});
31+
});

x-pack/plugins/apm/public/components/app/ErrorGroupDetails/DetailView/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import {
3939
getTabs,
4040
logStacktraceTab,
4141
} from './ErrorTabs';
42-
import { ExceptionStacktrace } from './ExceptionStacktrace';
42+
import { ExceptionStacktrace } from './exception_stacktrace';
4343

4444
const HeaderContainer = euiStyled.div`
4545
display: flex;

x-pack/plugins/apm/public/components/app/Settings/CustomizeUI/CustomLink/CreateEditCustomLinkFlyout/link_preview.test.tsx

+19-26
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,23 @@
55
* 2.0.
66
*/
77

8-
import React from 'react';
9-
import { LinkPreview } from '../CreateEditCustomLinkFlyout/link_preview';
8+
import { composeStories } from '@storybook/testing-react';
109
import {
1110
render,
1211
getNodeText,
1312
getByTestId,
1413
act,
1514
waitFor,
1615
} from '@testing-library/react';
17-
import {
18-
getCallApmApiSpy,
19-
CallApmApiSpy,
20-
} from '../../../../../../services/rest/callApmApiSpy';
16+
import React from 'react';
17+
import * as stories from './link_preview.stories';
18+
19+
const { Example } = composeStories(stories);
2120

2221
export const removeExternalLinkText = (str: string) =>
2322
str.replace(/\(opens in a new tab or window\)/g, '');
2423

2524
describe('LinkPreview', () => {
26-
let callApmApiSpy: CallApmApiSpy;
27-
beforeAll(() => {
28-
callApmApiSpy = getCallApmApiSpy().mockResolvedValue({
29-
transaction: { id: 'foo' },
30-
});
31-
});
32-
afterAll(() => {
33-
jest.clearAllMocks();
34-
});
3525
const getElementValue = (container: HTMLElement, id: string) =>
3626
getNodeText(
3727
((getByTestId(container, id) as HTMLDivElement)
@@ -41,7 +31,7 @@ describe('LinkPreview', () => {
4131
it('shows label and url default values', () => {
4232
act(() => {
4333
const { container } = render(
44-
<LinkPreview label="" url="" filters={[{ key: '', value: '' }]} />
34+
<Example label="" url="" filters={[{ key: '', value: '' }]} />
4535
);
4636
expect(getElementValue(container, 'preview-label')).toEqual('Elastic.co');
4737
expect(getElementValue(container, 'preview-url')).toEqual(
@@ -53,7 +43,7 @@ describe('LinkPreview', () => {
5343
it('shows label and url values', () => {
5444
act(() => {
5545
const { container } = render(
56-
<LinkPreview
46+
<Example
5747
label="foo"
5848
url="https://baz.co"
5949
filters={[{ key: '', value: '' }]}
@@ -71,7 +61,7 @@ describe('LinkPreview', () => {
7161
it("shows warning when couldn't replace context variables", () => {
7262
act(() => {
7363
const { container } = render(
74-
<LinkPreview
64+
<Example
7565
label="foo"
7666
url="https://baz.co?service.name={{invalid}"
7767
filters={[{ key: '', value: '' }]}
@@ -86,20 +76,23 @@ describe('LinkPreview', () => {
8676
expect(getByTestId(container, 'preview-warning')).toBeInTheDocument();
8777
});
8878
});
79+
8980
it('replaces url with transaction id', async () => {
9081
const { container } = render(
91-
<LinkPreview
82+
<Example
9283
label="foo"
9384
url="https://baz.co?transaction={{transaction.id}}"
9485
filters={[{ key: '', value: '' }]}
9586
/>
9687
);
97-
await waitFor(() => expect(callApmApiSpy).toHaveBeenCalled());
98-
expect(getElementValue(container, 'preview-label')).toEqual('foo');
99-
expect(
100-
removeExternalLinkText(
101-
(getByTestId(container, 'preview-link') as HTMLAnchorElement).text
102-
)
103-
).toEqual('https://baz.co?transaction=foo');
88+
89+
await waitFor(() => {
90+
expect(getElementValue(container, 'preview-label')).toEqual('foo');
91+
expect(
92+
removeExternalLinkText(
93+
(getByTestId(container, 'preview-link') as HTMLAnchorElement).text
94+
)
95+
).toEqual('https://baz.co?transaction=0');
96+
});
10497
});
10598
});

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -4307,6 +4307,11 @@
43074307
regenerator-runtime "^0.13.7"
43084308
source-map "^0.7.3"
43094309

4310+
"@storybook/testing-react@^0.0.17":
4311+
version "0.0.17"
4312+
resolved "https://registry.yarnpkg.com/@storybook/testing-react/-/testing-react-0.0.17.tgz#632dd22f8815743f78c182b126f444cf51d92d71"
4313+
integrity sha512-93nbA/JSWDEys1msd438+wzETRFDEgT2aFqJL2y46++zsyv8g2mCYKZkf9E36KQHMQbO1uJBHT8CmrLQa8VmZw==
4314+
43104315
"@storybook/theming@6.1.20", "@storybook/theming@^6.1.20":
43114316
version "6.1.20"
43124317
resolved "https://registry.yarnpkg.com/@storybook/theming/-/theming-6.1.20.tgz#ed0b330a5c08bbe998e9df95e615f0e84a8d663f"

0 commit comments

Comments
 (0)