This is an automated email from the ASF dual-hosted git repository.

msyavuz pushed a commit to branch msyavuz/chore/unsaved-changes-new-charts
in repository https://gitbox.apache.org/repos/asf/superset.git


The following commit(s) were added to 
refs/heads/msyavuz/chore/unsaved-changes-new-charts by this push:
     new 4cfd99064a8 chore: add tests
4cfd99064a8 is described below

commit 4cfd99064a89b1d946bd26fc9eebf8750f9688ad
Author: Mehmet Salih Yavuz <[email protected]>
AuthorDate: Fri Feb 6 00:43:07 2026 +0300

    chore: add tests
---
 .../ExploreChartHeader/ExploreChartHeader.test.tsx | 141 +++++++++++++++++++++
 1 file changed, 141 insertions(+)

diff --git 
a/superset-frontend/src/explore/components/ExploreChartHeader/ExploreChartHeader.test.tsx
 
b/superset-frontend/src/explore/components/ExploreChartHeader/ExploreChartHeader.test.tsx
index 7f182ec4053..4b8990d433d 100644
--- 
a/superset-frontend/src/explore/components/ExploreChartHeader/ExploreChartHeader.test.tsx
+++ 
b/superset-frontend/src/explore/components/ExploreChartHeader/ExploreChartHeader.test.tsx
@@ -227,6 +227,147 @@ describe('ExploreChartHeader', () => {
     );
   });
 
+  test('does not show unsaved changes for new charts on initial load', async 
() => {
+    const props = createProps({
+      slice: null,
+      chart: {
+        ...createProps().chart,
+        sliceFormData: null,
+      },
+      formData: {
+        viz_type: VizType.Histogram,
+        datasource: '49__table',
+      },
+    });
+
+    render(<ExploreHeader {...props} />, { useRedux: true });
+
+    expect(
+      await screen.findByText(/add the name of the chart/i),
+    ).toBeInTheDocument();
+
+    expect(useUnsavedChangesPrompt).toHaveBeenCalledWith(
+      expect.objectContaining({
+        hasUnsavedChanges: false,
+      }),
+    );
+  });
+
+  test('shows unsaved changes for new charts when user makes changes', async 
() => {
+    const initialFormData = {
+      viz_type: VizType.Histogram,
+      datasource: '49__table',
+      metrics: ['count'],
+    };
+
+    const modifiedFormData = {
+      ...initialFormData,
+      metrics: ['count', 'sum'],
+    };
+
+    const props = createProps({
+      slice: null,
+      chart: {
+        ...createProps().chart,
+        sliceFormData: null,
+      },
+      formData: initialFormData,
+    });
+
+    const { rerender } = render(<ExploreHeader {...props} />, {
+      useRedux: true,
+    });
+
+    // Initial render should not have unsaved changes
+    expect(useUnsavedChangesPrompt).toHaveBeenLastCalledWith(
+      expect.objectContaining({
+        hasUnsavedChanges: false,
+      }),
+    );
+
+    // Simulate user making changes
+    const modifiedProps = {
+      ...props,
+      formData: modifiedFormData,
+    };
+
+    rerender(<ExploreHeader {...modifiedProps} />, { useRedux: true });
+
+    await waitFor(() => {
+      expect(useUnsavedChangesPrompt).toHaveBeenLastCalledWith(
+        expect.objectContaining({
+          hasUnsavedChanges: true,
+        }),
+      );
+    });
+  });
+
+  test('shows unsaved changes for existing charts when form data differs from 
saved', async () => {
+    const savedFormData = {
+      viz_type: VizType.Histogram,
+      datasource: '49__table',
+      metrics: ['count'],
+    };
+
+    const currentFormData = {
+      ...savedFormData,
+      metrics: ['sum'],
+    };
+
+    const props = createProps({
+      formData: currentFormData,
+      chart: {
+        ...createProps().chart,
+        sliceFormData: savedFormData,
+      },
+    });
+
+    render(<ExploreHeader {...props} />, { useRedux: true });
+
+    expect(useUnsavedChangesPrompt).toHaveBeenCalledWith(
+      expect.objectContaining({
+        hasUnsavedChanges: true,
+      }),
+    );
+  });
+
+  test('does not show unsaved changes for existing charts when form data 
matches saved', async () => {
+    const baseFormData = {
+      viz_type: VizType.Histogram,
+      datasource: '49__table',
+      slice_id: 318,
+      url_params: {},
+      granularity_sqla: 'time_start',
+      time_range: 'No filter',
+      all_columns_x: ['age'],
+      adhoc_filters: [],
+      row_limit: 10000,
+      groupby: null,
+      color_scheme: 'supersetColors',
+      label_colors: {},
+      link_length: '25',
+      x_axis_label: 'age',
+      y_axis_label: 'count',
+    };
+
+    const props = createProps({
+      formData: baseFormData,
+      sliceName: 'Age distribution of respondents',
+      chart: {
+        ...createProps().chart,
+        sliceFormData: { ...baseFormData },
+      },
+    });
+
+    render(<ExploreHeader {...props} />, { useRedux: true });
+
+    expect(useUnsavedChangesPrompt).toHaveBeenCalledWith(
+      expect.objectContaining({
+        hasUnsavedChanges: false,
+      }),
+    );
+  });
+
   test('Save chart', async () => {
     const setSaveChartModalVisibilitySpy = jest.spyOn(
       saveModalActions,

Reply via email to