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

chore: Add Embed Modal extension override and tests #26759

Merged
merged 2 commits into from
Jan 25, 2024
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 @@ -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(() => {
geido marked this conversation as resolved.
Show resolved Hide resolved
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();
geido marked this conversation as resolved.
Show resolved Hide resolved
});
});

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 { 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 @@
// 404 just means the dashboard isn't currently embedded
return { result: null };
}
addDangerToast(t('Sorry, something went wrong. Please try again.'));

Check warning on line 138 in superset-frontend/src/dashboard/components/EmbeddedModal/index.tsx

View check run for this annotation

Codecov / codecov/patch

superset-frontend/src/dashboard/components/EmbeddedModal/index.tsx#L138

Added line #L138 was not covered by tests
throw err;
})
.then(({ result }) => {
Expand Down Expand Up @@ -199,6 +200,7 @@
</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 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
Loading