Skip to content

Commit

Permalink
chore: Add Embed Modal extension override and tests (apache#26759)
Browse files Browse the repository at this point in the history
  • Loading branch information
geido authored and eschutho committed Jan 30, 2024
1 parent a56303b commit d2d0962
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,21 @@ export interface SliceHeaderExtension {
dashboardId: number;
}

/**
* Interface for extensions to Embed Modal
*/
export interface DashboardEmbedModalExtensions {
dashboardId: string;
show: boolean;
onHide: () => void;
}

export type Extensions = Partial<{
'alertsreports.header.icon': React.ComponentType;
'embedded.documentation.configuration_details': React.ComponentType<ConfigDetailsProps>;
'embedded.documentation.description': ReturningDisplayable;
'embedded.documentation.url': string;
'embedded.modal': React.ComponentType<DashboardEmbedModalExtensions>;
'dashboard.nav.right': React.ComponentType;
'navbar.right-menu.item.icon': React.ComponentType<RightMenuItemIconProps>;
'navbar.right': React.ComponentType;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF 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.
*/

import React from 'react';
import {
render,
screen,
fireEvent,
waitFor,
} from 'spec/helpers/testing-library';
import '@testing-library/jest-dom';
import {
SupersetApiError,
getExtensionsRegistry,
makeApi,
} from '@superset-ui/core';
import setupExtensions from 'src/setup/setupExtensions';
import DashboardEmbedModal from './index';

const defaultResponse = {
result: { uuid: 'uuid', dashboard_id: '1', allowed_domains: ['example.com'] },
};

jest.mock('@superset-ui/core', () => ({
...jest.requireActual<any>('@superset-ui/core'),
makeApi: jest.fn(),
}));

const mockOnHide = jest.fn();
const defaultProps = {
dashboardId: '1',
show: true,
onHide: mockOnHide,
};
const resetMockApi = () => {
(makeApi as any).mockReturnValue(
jest.fn().mockResolvedValue(defaultResponse),
);
};
const setMockApiNotFound = () => {
const notFound = new SupersetApiError({ message: 'Not found', status: 404 });
(makeApi as any).mockReturnValue(jest.fn().mockRejectedValue(notFound));
};

const setup = () => {
render(<DashboardEmbedModal {...defaultProps} />, { useRedux: true });
resetMockApi();
};

beforeEach(() => {
jest.clearAllMocks();
resetMockApi();
});

test('renders', async () => {
setup();
expect(await screen.findByText('Embed')).toBeInTheDocument();
});

test('renders loading state', async () => {
setup();
await waitFor(() => {
expect(screen.getByRole('status', { name: 'Loading' })).toBeInTheDocument();
});
});

test('renders the modal default content', async () => {
render(<DashboardEmbedModal {...defaultProps} />, { useRedux: true });
expect(await screen.findByText('Settings')).toBeInTheDocument();
expect(
screen.getByText(new RegExp(/Allowed Domains/, 'i')),
).toBeInTheDocument();
});

test('renders the correct actions when dashboard is ready to embed', async () => {
setup();
expect(
await screen.findByRole('button', { name: 'Deactivate' }),
).toBeInTheDocument();
expect(
screen.getByRole('button', { name: 'Save changes' }),
).toBeInTheDocument();
});

test('renders the correct actions when dashboard is not ready to embed', async () => {
setMockApiNotFound();
setup();
expect(
await screen.findByRole('button', { name: 'Enable embedding' }),
).toBeInTheDocument();
});

test('enables embedding', async () => {
setMockApiNotFound();
setup();

const enableEmbed = await screen.findByRole('button', {
name: 'Enable embedding',
});
expect(enableEmbed).toBeInTheDocument();

fireEvent.click(enableEmbed);

expect(
await screen.findByRole('button', { name: 'Deactivate' }),
).toBeInTheDocument();
});

test('shows and hides the confirmation modal on deactivation', async () => {
setup();

const deactivate = await screen.findByRole('button', { name: 'Deactivate' });
fireEvent.click(deactivate);

expect(await screen.findByText('Disable embedding?')).toBeInTheDocument();
expect(
screen.getByText('This will remove your current embed configuration.'),
).toBeInTheDocument();

const okBtn = screen.getByRole('button', { name: 'OK' });
fireEvent.click(okBtn);

await waitFor(() => {
expect(screen.queryByText('Disable embedding?')).not.toBeInTheDocument();
});
});

test('enables the "Save Changes" button', async () => {
setup();

const allowedDomainsInput = await screen.findByLabelText(
new RegExp(/Allowed Domains/, 'i'),
);
const saveChangesBtn = screen.getByRole('button', { name: 'Save changes' });

expect(saveChangesBtn).toBeDisabled();
expect(allowedDomainsInput).toBeInTheDocument();

fireEvent.change(allowedDomainsInput, { target: { value: 'test.com' } });
expect(saveChangesBtn).toBeEnabled();
});

test('adds extension to DashboardEmbedModal', async () => {
const extensionsRegistry = getExtensionsRegistry();

extensionsRegistry.set('embedded.modal', () => (
<>dashboard.embed.modal.extension component</>
));

setupExtensions();
setup();

expect(
await screen.findByText('dashboard.embed.modal.extension component'),
).toBeInTheDocument();
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import Button from 'src/components/Button';
import { Input } from 'src/components/Input';
import { useToasts } from 'src/components/MessageToasts/withToasts';
import { FormItem } from 'src/components/Form';
import { EmbeddedDashboard } from '../types';
import { EmbeddedDashboard } from 'src/dashboard/types';

const extensionsRegistry = getExtensionsRegistry();

Expand Down Expand Up @@ -135,6 +135,7 @@ export const DashboardEmbedControls = ({ dashboardId, onHide }: Props) => {
// 404 just means the dashboard isn't currently embedded
return { result: null };
}
addDangerToast(t('Sorry, something went wrong. Please try again.'));
throw err;
})
.then(({ result }) => {
Expand Down Expand Up @@ -199,6 +200,7 @@ export const DashboardEmbedControls = ({ dashboardId, onHide }: Props) => {
</label>
<Input
name="allowed-domains"
id="allowed-domains"
value={allowedDomains}
placeholder="superset.example.com"
onChange={event => setAllowedDomains(event.target.value)}
Expand Down Expand Up @@ -237,12 +239,17 @@ export const DashboardEmbedControls = ({ dashboardId, onHide }: Props) => {
);
};

export const DashboardEmbedModal = (props: Props) => {
const DashboardEmbedModal = (props: Props) => {
const { show, onHide } = props;
const DashboardEmbedModalExtension = extensionsRegistry.get('embedded.modal');

return (
return DashboardEmbedModalExtension ? (
<DashboardEmbedModalExtension {...props} />
) : (
<Modal show={show} onHide={onHide} title={t('Embed')} hideFooter>
<DashboardEmbedControls {...props} />
</Modal>
);
};

export default DashboardEmbedModal;
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import setPeriodicRunner, {
stopPeriodicRender,
} from 'src/dashboard/util/setPeriodicRunner';
import { PageHeaderWithActions } from 'src/components/PageHeaderWithActions';
import { DashboardEmbedModal } from '../DashboardEmbedControls';
import DashboardEmbedModal from '../EmbeddedModal';
import OverwriteConfirm from '../OverwriteConfirm';

const extensionsRegistry = getExtensionsRegistry();
Expand Down

0 comments on commit d2d0962

Please sign in to comment.