mirror of
https://github.com/apache/superset.git
synced 2026-04-19 16:14:52 +00:00
fix(view-in-sqllab): unable to open virtual dataset after discarding chart edits (#35931)
This commit is contained in:
@@ -316,7 +316,6 @@ class DatasourceControl extends PureComponent {
|
||||
datasourceKey: `${datasource.id}__${datasource.type}`,
|
||||
sql: datasource.sql,
|
||||
};
|
||||
|
||||
const defaultDatasourceMenuItems = [];
|
||||
if (this.props.isEditable && !isMissingDatasource) {
|
||||
defaultDatasourceMenuItems.push({
|
||||
|
||||
@@ -20,6 +20,7 @@ import { getClientErrorObject, t } from '@superset-ui/core';
|
||||
import { useEffect, useRef, useCallback, useState } from 'react';
|
||||
import { useHistory } from 'react-router-dom';
|
||||
import { useBeforeUnload } from 'src/hooks/useBeforeUnload';
|
||||
import type { Location } from 'history';
|
||||
|
||||
type UseUnsavedChangesPromptProps = {
|
||||
hasUnsavedChanges: boolean;
|
||||
@@ -69,7 +70,13 @@ export const useUnsavedChangesPrompt = ({
|
||||
}, [onSave]);
|
||||
|
||||
const blockCallback = useCallback(
|
||||
({ pathname }: { pathname: string }) => {
|
||||
({
|
||||
pathname,
|
||||
state,
|
||||
}: {
|
||||
pathname: Location['pathname'];
|
||||
state: Location['state'];
|
||||
}) => {
|
||||
if (manualSaveRef.current) {
|
||||
manualSaveRef.current = false;
|
||||
return undefined;
|
||||
@@ -77,7 +84,7 @@ export const useUnsavedChangesPrompt = ({
|
||||
|
||||
confirmNavigationRef.current = () => {
|
||||
unblockRef.current?.();
|
||||
history.push(pathname);
|
||||
history.push(pathname, state);
|
||||
};
|
||||
|
||||
setShowModal(true);
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
import { renderHook } from '@testing-library/react-hooks';
|
||||
import { renderHook, act } from '@testing-library/react-hooks';
|
||||
import { Router } from 'react-router-dom';
|
||||
import { createMemoryHistory } from 'history';
|
||||
import { useUnsavedChangesPrompt } from '.';
|
||||
@@ -124,3 +124,42 @@ test('should close modal when handleConfirmNavigation is called', () => {
|
||||
|
||||
expect(result.current.showModal).toBe(false);
|
||||
});
|
||||
|
||||
test('should preserve pathname and state when confirming navigation', () => {
|
||||
const onSave = jest.fn();
|
||||
const history = createMemoryHistory();
|
||||
const wrapper = ({ children }: any) => (
|
||||
<Router history={history}>{children}</Router>
|
||||
);
|
||||
|
||||
const locationState = { fromDashboard: true, dashboardId: 123 };
|
||||
const pathname = '/another-page';
|
||||
|
||||
const { result } = renderHook(
|
||||
() => useUnsavedChangesPrompt({ hasUnsavedChanges: true, onSave }),
|
||||
{ wrapper },
|
||||
);
|
||||
|
||||
const pushSpy = jest.spyOn(history, 'push');
|
||||
|
||||
// Simulate a blocked navigation (the hook sets up history.block internally)
|
||||
act(() => {
|
||||
history.push(pathname, locationState);
|
||||
});
|
||||
|
||||
// Modal should now be visible
|
||||
expect(result.current.showModal).toBe(true);
|
||||
|
||||
// Confirm navigation
|
||||
act(() => {
|
||||
result.current.handleConfirmNavigation();
|
||||
});
|
||||
|
||||
// Modal should close
|
||||
expect(result.current.showModal).toBe(false);
|
||||
|
||||
// Verify correct call with pathname and state preserved
|
||||
expect(pushSpy).toHaveBeenCalledWith(pathname, locationState);
|
||||
|
||||
pushSpy.mockRestore();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user