Add markdown parser to ErrorPanel (#39432)

* Add markdown parsing to PanelError for type string

* Add jest tests

* Remove obsolete test
This commit is contained in:
Matthias Wilhelm 2019-06-25 22:28:16 +02:00 committed by GitHub
parent 57869c64df
commit b029e82d53
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 138 additions and 1 deletions

View file

@ -0,0 +1,96 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`PanelError renders given React Element 1`] = `
<div
class="dshPanel__error panel-content"
>
<div
class="euiText euiText--extraSmall"
>
<div
class="euiTextColor euiTextColor--subdued"
>
<svg
class="euiIcon euiIcon--medium euiIcon--danger euiIcon-isLoading"
focusable="false"
height="16"
viewBox="0 0 16 16"
width="16"
xmlns="http://www.w3.org/2000/svg"
/>
<div
class="euiSpacer euiSpacer--s"
/>
<div>
test
</div>
</div>
</div>
</div>
`;
exports[`PanelError renders plain string 1`] = `
<div
class="dshPanel__error panel-content"
>
<div
class="euiText euiText--extraSmall"
>
<div
class="euiTextColor euiTextColor--subdued"
>
<svg
class="euiIcon euiIcon--medium euiIcon--danger euiIcon-isLoading"
focusable="false"
height="16"
viewBox="0 0 16 16"
width="16"
xmlns="http://www.w3.org/2000/svg"
/>
<div
class="euiSpacer euiSpacer--s"
/>
<div>
<p>
test
</p>
</div>
</div>
</div>
</div>
`;
exports[`PanelError renders string with markdown link 1`] = `
<div
class="dshPanel__error panel-content"
>
<div
class="euiText euiText--extraSmall"
>
<div
class="euiTextColor euiTextColor--subdued"
>
<svg
class="euiIcon euiIcon--medium euiIcon--danger euiIcon-isLoading"
focusable="false"
height="16"
viewBox="0 0 16 16"
width="16"
xmlns="http://www.w3.org/2000/svg"
/>
<div
class="euiSpacer euiSpacer--s"
/>
<div>
<p>
<a
href="http://www.elastic.co/"
>
test
</a>
</p>
</div>
</div>
</div>
</div>
`;

View file

@ -0,0 +1,40 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
// TODO: remove this when EUI supports types for this.
// @ts-ignore: implicit any for JS file
import { takeMountedSnapshot } from '@elastic/eui/lib/test';
import React from 'react';
import { PanelError } from './panel_error';
import { mount } from 'enzyme';
test('PanelError renders plain string', () => {
const component = mount(<PanelError error="test" />);
expect(takeMountedSnapshot(component)).toMatchSnapshot();
});
test('PanelError renders string with markdown link', () => {
const component = mount(<PanelError error="[test](http://www.elastic.co/)" />);
expect(takeMountedSnapshot(component)).toMatchSnapshot();
});
test('PanelError renders given React Element ', () => {
const component = mount(<PanelError error={<div>test</div>} />);
expect(takeMountedSnapshot(component)).toMatchSnapshot();
});

View file

@ -18,6 +18,7 @@
*/
import { EuiIcon, EuiSpacer, EuiText } from '@elastic/eui';
import ReactMarkdown from 'react-markdown';
import React from 'react';
export interface PanelErrorProps {
@ -30,7 +31,7 @@ export function PanelError({ error }: PanelErrorProps) {
<EuiText color="subdued" size="xs">
<EuiIcon type="alert" color="danger" />
<EuiSpacer size="s" />
{error}
{typeof error === 'string' ? <ReactMarkdown source={error} /> : error}
</EuiText>
</div>
);