Skip to content

Commit

Permalink
[Maps] render attribution with no url as text instead of EuiLink (#31873
Browse files Browse the repository at this point in the history
)

* [Maps] render attribution with no url as text instead of EuiLink

* remove check for length
  • Loading branch information
nreese authored Feb 25, 2019
1 parent a7c4d70 commit 0f5ad29
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AttributionControl is rendered 1`] = `
<EuiPanel
className="mapWidgetControl mapAttributionControl"
grow={false}
hasShadow={false}
paddingSize="none"
>
<EuiText
color="subdued"
grow={true}
size="xs"
>
<small>
attribution with no link
,
<EuiLink
color="subdued"
href="https://coolmaps.com"
target="_blank"
type="button"
>
attribution with link
</EuiLink>
</small>
</EuiText>
</EuiPanel>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,18 @@ export class AttributionControl extends React.Component {

componentDidMount() {
this._isMounted = true;
this._syncMbMapWithAttribution();
this._loadAttributions();
}

componentWillUnmount() {
this._isMounted = false;
}

componentDidUpdate() {
this._syncMbMapWithAttribution();
this._loadAttributions();
}

_syncMbMapWithAttribution = async () => {

_loadAttributions = async () => {
const attributionPromises = this.props.layerList.map(async (layer) => {
try {
return await layer.getAttributions();
Expand Down Expand Up @@ -63,11 +62,21 @@ export class AttributionControl extends React.Component {
}
};

_renderAttribution({ url, label }) {
if (!url) {
return label;
}

return (
<EuiLink color="subdued" href={url} target="_blank">{label}</EuiLink>
);
}

_renderAttributions() {
return this.state.uniqueAttributions.map((attribution, index) => {
return (
<Fragment key={index}>
<EuiLink color="subdued" href={attribution.url} target="_blank">{attribution.label}</EuiLink>
{this._renderAttribution(attribution)}
{index < (this.state.uniqueAttributions.length - 1) && ', '}
</Fragment>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';
import { shallow } from 'enzyme';

import { AttributionControl } from './view';

describe('AttributionControl', () => {
test('is rendered', async () => {
const mockLayer1 = {
getAttributions: async () => {
return [
{ url: '', label: 'attribution with no link' }
];
}
};
const mockLayer2 = {
getAttributions: async () => {
return [
{ url: 'https://coolmaps.com', label: 'attribution with link' }
];
}
};
const component = shallow(
<AttributionControl
layerList={[mockLayer1, mockLayer2]}
/>
);

// Ensure all promises resolve
await new Promise(resolve => process.nextTick(resolve));
// Ensure the state changes are reflected
component.update();

expect(component)
.toMatchSnapshot();
});
});

0 comments on commit 0f5ad29

Please sign in to comment.