Skip to content
Open
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
47 changes: 46 additions & 1 deletion superset-frontend/src/features/tags/TagModal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
* specific language governing permissions and limitations
* under the License.
*/
import { render, screen } from 'spec/helpers/testing-library';
import {
render,
screen,
userEvent,
waitFor,
within,
} from 'spec/helpers/testing-library';
import TagModal from 'src/features/tags/TagModal';
import fetchMock from 'fetch-mock';
import { Tag } from 'src/views/CRUD/types';
Expand All @@ -31,6 +37,11 @@ const mockedProps = {

const fetchEditFetchObjects = `glob:*/api/v1/tag/get_objects/?tags=*`;

fetchMock.get('glob:*/api/v1/chart/?q=*', {
count: 1,
result: [{ id: 1, slice_name: 'Sales chart' }],
});

test('should render', () => {
const { container } = render(<TagModal {...mockedProps} />);
expect(container).toBeInTheDocument();
Expand All @@ -50,6 +61,40 @@ test('description input has an accessible name', async () => {
expect(await screen.findByLabelText('Tag description')).toBeInTheDocument();
});

test.each(['Cancel', 'Save', 'Close'])(
'reopening the create form after %s clears the name and chart',
async action => {
if (action === 'Save') {
fetchMock.post('glob:*/api/v1/tag/', { id: 1 });
}
const onHide = jest.fn();
const { rerender } = render(
<TagModal {...mockedProps} onHide={onHide} clearOnHide />,
);

await userEvent.type(screen.getByLabelText('Tag name'), 'Draft tag');
await userEvent.click(
screen.getByRole('combobox', { name: 'Select charts' }),
);
await userEvent.click(await screen.findByText('Sales chart'));
expect(
within(screen.getByRole('dialog')).getByText('Sales chart'),
).toBeInTheDocument();

await userEvent.click(screen.getByRole('button', { name: action }));
await waitFor(() => expect(onHide).toHaveBeenCalledTimes(1));
rerender(
<TagModal {...mockedProps} onHide={onHide} clearOnHide show={false} />,
);
rerender(<TagModal {...mockedProps} onHide={onHide} clearOnHide />);

expect(screen.getByLabelText('Tag name')).toHaveValue('');
expect(
within(screen.getByRole('dialog')).queryByText('Sales chart'),
).not.toBeInTheDocument();
},
);

test('renders correctly in edit mode', () => {
fetchMock.get(fetchEditFetchObjects, [[]]);
const editTag: Tag = {
Expand Down
12 changes: 7 additions & 5 deletions superset-frontend/src/features/tags/TagModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ const TagModal: FC<TagModalProps> = ({
setSavedQueriesToTag([]);
};

const handleHide = () => {
if (clearOnHide) clearTagForm();
onHide();
};

useEffect(() => {
const resourceMap: { [key: string]: TaggableResourceOption[] } = {
[TaggableResources.Dashboard]: [],
Expand Down Expand Up @@ -275,17 +280,14 @@ const TagModal: FC<TagModalProps> = ({
title={isEditMode ? t('Edit Tag') : t('Create Tag')}
/>
}
onHide={() => {
if (clearOnHide) clearTagForm();
onHide();
}}
onHide={handleHide}
show={show}
footer={
<div>
<Button
data-test="modal-save-dashboard-button"
buttonStyle="secondary"
onClick={onHide}
onClick={handleHide}
>
{t('Cancel')}
</Button>
Expand Down
Loading