From dd9710863638a85a8a054a9a9ee8fc430413be2d Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Tue, 19 May 2020 18:20:51 +0200 Subject: [PATCH] Fix saved object share link (#66771) --- .../url_panel_content.test.tsx.snap | 6 +- .../components/url_panel_content.test.tsx | 185 ++++++++++++++++-- .../public/components/url_panel_content.tsx | 4 +- 3 files changed, 175 insertions(+), 20 deletions(-) diff --git a/src/plugins/share/public/components/__snapshots__/url_panel_content.test.tsx.snap b/src/plugins/share/public/components/__snapshots__/url_panel_content.test.tsx.snap index c10ca551308809..8787e0c0273755 100644 --- a/src/plugins/share/public/components/__snapshots__/url_panel_content.test.tsx.snap +++ b/src/plugins/share/public/components/__snapshots__/url_panel_content.test.tsx.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`render 1`] = ` +exports[`share url panel content render 1`] = ` `; -exports[`should enable saved object export option when objectId is provided 1`] = ` +exports[`share url panel content should enable saved object export option when objectId is provided 1`] = ` `; -exports[`should hide short url section when allowShortUrl is false 1`] = ` +exports[`share url panel content should hide short url section when allowShortUrl is false 1`] = ` ({})); +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, @@ -31,19 +35,170 @@ const defaultProps = { post: () => Promise.resolve({} as any), }; -test('render', () => { - const component = shallow(); - expect(component).toMatchSnapshot(); -}); +describe('share url panel content', () => { + test('render', () => { + const component = shallow(); + expect(component).toMatchSnapshot(); + }); -test('should enable saved object export option when objectId is provided', () => { - const component = shallow(); - expect(component).toMatchSnapshot(); -}); + test('should enable saved object export option when objectId is provided', () => { + const component = shallow(); + expect(component).toMatchSnapshot(); + }); + + test('should hide short url section when allowShortUrl is false', () => { + const component = shallow( + + ); + expect(component).toMatchSnapshot(); + }); + + test('should remove _a query parameter in saved object mode', () => { + const component = shallow( + + ); + 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( + + ); + 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( + + ); + act(() => { + component.find(EuiRadioGroup).prop('onChange')!(ExportUrlAsType.EXPORT_URL_AS_SAVED_OBJECT); + }); + expect(component.exists(EuiSwitch)).toEqual(false); + }); + }); + + describe('embedded', () => { + const asIframe = (url: string) => ``; + + test('should add embedded flag to target code in snapshot mode', () => { + const component = shallow( + + ); + 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( + + ); + 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( + + ); + 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( + + ); + 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( - - ); - expect(component).toMatchSnapshot(); + test('should hide short url for saved object mode', async () => { + const component = shallow( + + ); + act(() => { + component.find(EuiRadioGroup).prop('onChange')!(ExportUrlAsType.EXPORT_URL_AS_SAVED_OBJECT); + }); + expect(component.exists(EuiSwitch)).toEqual(false); + }); + }); }); diff --git a/src/plugins/share/public/components/url_panel_content.tsx b/src/plugins/share/public/components/url_panel_content.tsx index 2b1159be890033..804b606696a83b 100644 --- a/src/plugins/share/public/components/url_panel_content.tsx +++ b/src/plugins/share/public/components/url_panel_content.tsx @@ -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', } @@ -181,7 +181,7 @@ export class UrlPanelContent extends Component { }), }); if (this.props.isEmbedded) { - formattedUrl = this.makeUrlEmbeddable(url); + formattedUrl = this.makeUrlEmbeddable(formattedUrl); } return formattedUrl;