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

[Dashboard Navigation] Add external link icon #174407

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import React from 'react';
import { getDashboardLocatorParamsFromEmbeddable } from '@kbn/dashboard-plugin/public';
import { DashboardContainer } from '@kbn/dashboard-plugin/public/dashboard_container';
import { DEFAULT_DASHBOARD_DRILLDOWN_OPTIONS } from '@kbn/presentation-util-plugin/public';
import { createEvent, fireEvent, render, screen, waitFor } from '@testing-library/react';
import { createEvent, fireEvent, render, screen, waitFor, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import { LINKS_VERTICAL_LAYOUT } from '../../../common/content_management';
Expand Down Expand Up @@ -104,8 +104,15 @@ describe('Dashboard link component', () => {
expect(fetchDashboard).toHaveBeenCalledWith(defaultLinkInfo.destination);
await waitFor(() => expect(onRender).toHaveBeenCalledTimes(1));

// renders dashboard title
const link = await screen.findByTestId('dashboardLink--foo');
expect(link).toHaveTextContent('another dashboard');

// does not render external link icon
const externalIcon = within(link).queryByText('External link');
expect(externalIcon).toBeNull();

// calls `navigate` on click
userEvent.click(link);
expect(dashboardContainer.locator?.getRedirectUrl).toBeCalledWith({
dashboardId: '456',
Expand Down Expand Up @@ -134,7 +141,7 @@ describe('Dashboard link component', () => {
expect(preventDefault).toHaveBeenCalledTimes(0);
});

test('openInNewTab uses window.open, not navigateToApp', async () => {
test('openInNewTab uses window.open, not navigateToApp, and renders external icon', async () => {
const linkInfo = {
...defaultLinkInfo,
options: { ...DEFAULT_DASHBOARD_DRILLDOWN_OPTIONS, openInNewTab: true },
Expand All @@ -155,6 +162,12 @@ describe('Dashboard link component', () => {
await waitFor(() => expect(onRender).toHaveBeenCalledTimes(1));
const link = await screen.findByTestId('dashboardLink--foo');
expect(link).toBeInTheDocument();

// external link icon is rendered
const externalIcon = within(link).getByText('External link');
expect(externalIcon?.getAttribute('data-euiicon-type')).toBe('popout');

// calls `window.open`
userEvent.click(link);
expect(dashboardContainer.locator?.navigate).toBeCalledTimes(0);
expect(window.open).toHaveBeenCalledWith('https://my-kibana.com/dashboard/123', '_blank');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ export const DashboardLinkComponent = ({
'dashboardLinkError--noLabel': !link.label,
})}
label={linkLabel}
external={link.options?.openInNewTab}
data-test-subj={error ? `${id}--error` : `${id}`}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import React from 'react';

import userEvent from '@testing-library/user-event';
import { createEvent, fireEvent, render, screen } from '@testing-library/react';
import { createEvent, fireEvent, render, screen, within } from '@testing-library/react';
import { LinksEmbeddable, LinksContext } from '../../embeddable/links_embeddable';
import { mockLinksPanel } from '../../../common/mocks';
import { LINKS_VERTICAL_LAYOUT } from '../../../common/content_management';
Expand Down Expand Up @@ -37,7 +37,7 @@ describe('external link component', () => {
jest.clearAllMocks();
});

test('by default opens in new tab', async () => {
test('by default opens in new tab and renders external icon', async () => {
render(
<LinksContext.Provider value={links}>
<ExternalLinkComponent
Expand All @@ -51,10 +51,27 @@ describe('external link component', () => {
expect(onRender).toBeCalledTimes(1);
const link = await screen.findByTestId('externalLink--foo');
expect(link).toBeInTheDocument();
await userEvent.click(link);
const externalIcon = within(link).getByText('External link');
expect(externalIcon.getAttribute('data-euiicon-type')).toBe('popout');
userEvent.click(link);
expect(window.open).toHaveBeenCalledWith('https://example.com', '_blank');
});

test('renders external icon even when `openInNewTab` setting is `false`', async () => {
const linkInfo = {
...defaultLinkInfo,
options: { ...DEFAULT_URL_DRILLDOWN_OPTIONS, openInNewTab: false },
};
render(
<LinksContext.Provider value={links}>
<ExternalLinkComponent link={linkInfo} layout={LINKS_VERTICAL_LAYOUT} onRender={onRender} />
</LinksContext.Provider>
);
const link = await screen.findByTestId('externalLink--foo');
const externalIcon = within(link).getByText('External link');
expect(externalIcon?.getAttribute('data-euiicon-type')).toBe('popout');
});

test('modified click does not trigger event.preventDefault', async () => {
const linkInfo = {
...defaultLinkInfo,
Expand Down Expand Up @@ -86,7 +103,7 @@ describe('external link component', () => {
);
expect(onRender).toBeCalledTimes(1);
const link = await screen.findByTestId('externalLink--foo');
await userEvent.click(link);
userEvent.click(link);
expect(coreServices.application.navigateToUrl).toBeCalledTimes(1);
expect(coreServices.application.navigateToUrl).toBeCalledWith('https://example.com');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const ExternalLinkComponent = ({
return (
<EuiListGroupItem
size="s"
external
color="text"
isDisabled={!link.destination || !isValidUrl}
className={'linksPanelLink'}
Expand Down