Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix saved object share link #66771

Merged
merged 3 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

185 changes: 170 additions & 15 deletions src/plugins/share/public/components/url_panel_content.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
* under the License.
*/

jest.mock('../lib/url_shortener', () => ({}));
import { EuiCopy, EuiRadioGroup, EuiSwitch, EuiSwitchEvent } from '@elastic/eui';

jest.mock('../lib/url_shortener', () => ({ shortenUrl: jest.fn() }));

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

import { UrlPanelContent } from './url_panel_content';
import { ExportUrlAsType, UrlPanelContent } from './url_panel_content';
import { act } from 'react-dom/test-utils';
import { shortenUrl } from '../lib/url_shortener';

const defaultProps = {
allowShortUrl: true,
Expand All @@ -31,19 +35,170 @@ const defaultProps = {
post: () => Promise.resolve({} as any),
};

test('render', () => {
const component = shallow(<UrlPanelContent {...defaultProps} />);
expect(component).toMatchSnapshot();
});
describe('share url panel content', () => {
test('render', () => {
const component = shallow(<UrlPanelContent {...defaultProps} />);
expect(component).toMatchSnapshot();
});

test('should enable saved object export option when objectId is provided', () => {
const component = shallow(<UrlPanelContent {...defaultProps} objectId="id1" />);
expect(component).toMatchSnapshot();
});
test('should enable saved object export option when objectId is provided', () => {
const component = shallow(<UrlPanelContent {...defaultProps} objectId="id1" />);
expect(component).toMatchSnapshot();
});

test('should hide short url section when allowShortUrl is false', () => {
const component = shallow(
<UrlPanelContent {...defaultProps} allowShortUrl={false} objectId="id1" />
);
expect(component).toMatchSnapshot();
});

test('should remove _a query parameter in saved object mode', () => {
const component = shallow(
<UrlPanelContent
{...defaultProps}
shareableUrl="http://localhost:5601/app/myapp#/?_g=()&_a=()"
allowShortUrl={false}
objectId="id1"
/>
);
act(() => {
component.find(EuiRadioGroup).prop('onChange')!(ExportUrlAsType.EXPORT_URL_AS_SAVED_OBJECT);
});
expect(component.find(EuiCopy).prop('textToCopy')).toEqual(
'http://localhost:5601/app/myapp#/?_g=()'
);
});

describe('short url', () => {
test('should generate short url and put it in copy button', async () => {
const shortenUrlMock = shortenUrl as jest.Mock;
shortenUrlMock.mockReset();
shortenUrlMock.mockResolvedValue('http://localhost/short/url');

const component = shallow(
<UrlPanelContent
{...defaultProps}
shareableUrl="http://localhost:5601/app/myapp#/?_g=()&_a=()"
objectId="id1"
/>
);
await act(async () => {
component.find(EuiSwitch).prop('onChange')!(({
target: { checked: true },
} as unknown) as EuiSwitchEvent);
});
expect(shortenUrlMock).toHaveBeenCalledWith(
'http://localhost:5601/app/myapp#/?_g=()&_a=()',
expect.anything()
);
expect(component.find(EuiCopy).prop('textToCopy')).toContain('http://localhost/short/url');
});

test('should hide short url for saved object mode', async () => {
const component = shallow(
<UrlPanelContent
{...defaultProps}
shareableUrl="http://localhost:5601/app/myapp#/"
objectId="id1"
/>
);
act(() => {
component.find(EuiRadioGroup).prop('onChange')!(ExportUrlAsType.EXPORT_URL_AS_SAVED_OBJECT);
});
expect(component.exists(EuiSwitch)).toEqual(false);
});
});

describe('embedded', () => {
const asIframe = (url: string) => `<iframe src="${url}" height="600" width="800"></iframe>`;

test('should add embedded flag to target code in snapshot mode', () => {
const component = shallow(
<UrlPanelContent
{...defaultProps}
shareableUrl="http://localhost:5601/app/myapp#/"
isEmbedded
allowShortUrl={false}
objectId="id1"
/>
);
expect(component.find(EuiCopy).prop('textToCopy')).toEqual(
asIframe('http://localhost:5601/app/myapp#/?embed=true')
);
});

test('should add embedded flag to target code in snapshot mode with existing query parameters', () => {
const component = shallow(
<UrlPanelContent
{...defaultProps}
shareableUrl="http://localhost:5601/app/myapp#/?_g=()&_a=()"
isEmbedded
allowShortUrl={false}
objectId="id1"
/>
);
expect(component.find(EuiCopy).prop('textToCopy')).toEqual(
asIframe('http://localhost:5601/app/myapp#/?embed=true&_g=()&_a=()')
);
});

test('should remove _a query parameter and add embedded flag in saved object mode', () => {
const component = shallow(
<UrlPanelContent
{...defaultProps}
shareableUrl="http://localhost:5601/app/myapp#/?_g=()&_a=()"
isEmbedded
allowShortUrl={false}
objectId="id1"
/>
);
act(() => {
component.find(EuiRadioGroup).prop('onChange')!(ExportUrlAsType.EXPORT_URL_AS_SAVED_OBJECT);
});
expect(component.find(EuiCopy).prop('textToCopy')).toEqual(
asIframe('http://localhost:5601/app/myapp#/?embed=true&_g=()')
);
});

test('should generate short url with embed flag and put it in copy button', async () => {
const shortenUrlMock = shortenUrl as jest.Mock;
shortenUrlMock.mockReset();
shortenUrlMock.mockResolvedValue('http://localhost/short/url');

const component = shallow(
<UrlPanelContent
{...defaultProps}
isEmbedded
shareableUrl="http://localhost:5601/app/myapp#/?_g=()&_a=()"
objectId="id1"
/>
);
await act(async () => {
component.find(EuiSwitch).prop('onChange')!(({
target: { checked: true },
} as unknown) as EuiSwitchEvent);
});
expect(shortenUrlMock).toHaveBeenCalledWith(
'http://localhost:5601/app/myapp#/?embed=true&_g=()&_a=()',
expect.anything()
);
expect(component.find(EuiCopy).prop('textToCopy')).toContain('http://localhost/short/url');
});

test('should hide short url section when allowShortUrl is false', () => {
const component = shallow(
<UrlPanelContent {...defaultProps} allowShortUrl={false} objectId="id1" />
);
expect(component).toMatchSnapshot();
test('should hide short url for saved object mode', async () => {
const component = shallow(
<UrlPanelContent
{...defaultProps}
isEmbedded
shareableUrl="http://localhost:5601/app/myapp#/"
objectId="id1"
/>
);
act(() => {
component.find(EuiRadioGroup).prop('onChange')!(ExportUrlAsType.EXPORT_URL_AS_SAVED_OBJECT);
});
expect(component.exists(EuiSwitch)).toEqual(false);
});
});
});
4 changes: 2 additions & 2 deletions src/plugins/share/public/components/url_panel_content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ interface Props {
post: HttpStart['post'];
}

enum ExportUrlAsType {
export enum ExportUrlAsType {
EXPORT_URL_AS_SAVED_OBJECT = 'savedObject',
EXPORT_URL_AS_SNAPSHOT = 'snapshot',
}
Expand Down Expand Up @@ -181,7 +181,7 @@ export class UrlPanelContent extends Component<Props, State> {
}),
});
if (this.props.isEmbedded) {
formattedUrl = this.makeUrlEmbeddable(url);
formattedUrl = this.makeUrlEmbeddable(formattedUrl);
}

return formattedUrl;
Expand Down