|
17 | 17 | * under the License. |
18 | 18 | */ |
19 | 19 | import { |
| 20 | + Behavior, |
| 21 | + ChartMetadata, |
20 | 22 | JsonResponse, |
21 | 23 | SupersetClient, |
| 24 | + getChartMetadataRegistry, |
22 | 25 | isFeatureEnabled, |
23 | 26 | } from '@superset-ui/core'; |
24 | 27 | import { waitFor } from 'spec/helpers/testing-library'; |
@@ -55,6 +58,7 @@ import { |
55 | 58 | } from 'spec/fixtures/mockSliceEntities'; |
56 | 59 | import { emptyFilters } from 'spec/fixtures/mockDashboardFilters'; |
57 | 60 | import mockDashboardData from 'spec/fixtures/mockDashboardData'; |
| 61 | +import { getCrossFiltersConfiguration } from 'src/dashboard/util/crossFilters'; |
58 | 62 | import type { RootState } from 'src/dashboard/types'; |
59 | 63 | import { navigateTo, navigateWithState } from 'src/utils/navigationUtils'; |
60 | 64 |
|
@@ -496,6 +500,124 @@ describe('dashboardState actions', () => { |
496 | 500 | ); |
497 | 501 | }); |
498 | 502 |
|
| 503 | + test('Save As sends the cross-filter config with the copy and leaves the source dashboard alone', async () => { |
| 504 | + const sourceDashboardId = 1; |
| 505 | + const vizType = 'interactive_test_chart'; |
| 506 | + const chartLayoutItem = (chartId: number) => ({ |
| 507 | + children: [], |
| 508 | + id: `CHART-${chartId}`, |
| 509 | + meta: { chartId, width: 4, height: 50 }, |
| 510 | + parents: ['ROOT_ID', 'GRID_ID'], |
| 511 | + type: 'CHART', |
| 512 | + }); |
| 513 | + const chart = (id: number) => ({ id, form_data: { viz_type: vizType } }); |
| 514 | + // The stored chartsInScope values are stale on purpose, so the copy |
| 515 | + // only matches if the configuration is recomputed for the current state. |
| 516 | + const state = { |
| 517 | + ...mockState, |
| 518 | + dashboardInfo: { |
| 519 | + id: sourceDashboardId, |
| 520 | + metadata: { |
| 521 | + color_scheme: 'supersetColors', |
| 522 | + chart_configuration: { |
| 523 | + '1': { |
| 524 | + id: 1, |
| 525 | + crossFilters: { |
| 526 | + scope: { rootPath: ['ROOT_ID'], excluded: [2] }, |
| 527 | + chartsInScope: [2], |
| 528 | + }, |
| 529 | + }, |
| 530 | + '2': { |
| 531 | + id: 2, |
| 532 | + crossFilters: { scope: 'global', chartsInScope: [] }, |
| 533 | + }, |
| 534 | + }, |
| 535 | + global_chart_configuration: { |
| 536 | + scope: { rootPath: ['ROOT_ID'], excluded: [] }, |
| 537 | + chartsInScope: [], |
| 538 | + }, |
| 539 | + }, |
| 540 | + }, |
| 541 | + dashboardLayout: { |
| 542 | + past: [], |
| 543 | + present: { |
| 544 | + 'CHART-1': chartLayoutItem(1), |
| 545 | + 'CHART-2': chartLayoutItem(2), |
| 546 | + }, |
| 547 | + future: {}, |
| 548 | + }, |
| 549 | + charts: { 1: chart(1), 2: chart(2) }, |
| 550 | + }; |
| 551 | + const getState = jest.fn(() => state) as unknown as () => any; |
| 552 | + const dispatch: jest.Mock = jest.fn(action => |
| 553 | + typeof action === 'function' ? action(dispatch, getState) : action, |
| 554 | + ); |
| 555 | + getChartMetadataRegistry().registerValue( |
| 556 | + vizType, |
| 557 | + new ChartMetadata({ |
| 558 | + name: 'interactive test chart', |
| 559 | + thumbnail: '.png', |
| 560 | + behaviors: [Behavior.InteractiveChart], |
| 561 | + }), |
| 562 | + ); |
| 563 | + const requestStub = jest |
| 564 | + .spyOn(SupersetClient, 'request') |
| 565 | + .mockResolvedValue({ json: { result: {} } } as any); |
| 566 | + try { |
| 567 | + postStub.mockRestore(); |
| 568 | + postStub = jest.spyOn(SupersetClient, 'post').mockResolvedValue({ |
| 569 | + json: { result: { id: 999, last_modified_time: 0 } }, |
| 570 | + } as any); |
| 571 | + |
| 572 | + await saveDashboardRequest( |
| 573 | + { ...newDashboardData, duplicate_slices: true }, |
| 574 | + sourceDashboardId, |
| 575 | + SAVE_TYPE_NEWDASHBOARD, |
| 576 | + )(dispatch, getState); |
| 577 | + |
| 578 | + await waitFor(() => expect(mockNavigateTo).toHaveBeenCalled()); |
| 579 | + expect( |
| 580 | + dispatch.mock.calls.some( |
| 581 | + ([action]) => action?.type === 'SAVE_CHART_CONFIG_BEGIN', |
| 582 | + ), |
| 583 | + ).toBe(false); |
| 584 | + expect(requestStub).not.toHaveBeenCalled(); |
| 585 | + expect(putStub).not.toHaveBeenCalled(); |
| 586 | + const { endpoint, jsonPayload } = postStub.mock.calls[0][0]; |
| 587 | + expect(endpoint).toBe(`/api/v1/dashboard/${sourceDashboardId}/copy/`); |
| 588 | + const metadata = JSON.parse(jsonPayload.json_metadata); |
| 589 | + const { chartConfiguration, globalChartConfiguration } = |
| 590 | + getCrossFiltersConfiguration( |
| 591 | + state.dashboardLayout.present as any, |
| 592 | + state.dashboardInfo.metadata as any, |
| 593 | + state.charts as any, |
| 594 | + ); |
| 595 | + expect(metadata.chart_configuration).toEqual(chartConfiguration); |
| 596 | + expect(metadata.global_chart_configuration).toEqual( |
| 597 | + globalChartConfiguration, |
| 598 | + ); |
| 599 | + expect(metadata.chart_configuration).toEqual({ |
| 600 | + '1': { |
| 601 | + id: 1, |
| 602 | + crossFilters: { |
| 603 | + scope: { rootPath: ['ROOT_ID'], excluded: [2] }, |
| 604 | + chartsInScope: [], |
| 605 | + }, |
| 606 | + }, |
| 607 | + '2': { |
| 608 | + id: 2, |
| 609 | + crossFilters: { scope: 'global', chartsInScope: [1] }, |
| 610 | + }, |
| 611 | + }); |
| 612 | + expect(metadata.global_chart_configuration.chartsInScope).toEqual([ |
| 613 | + 1, 2, |
| 614 | + ]); |
| 615 | + } finally { |
| 616 | + requestStub.mockRestore(); |
| 617 | + getChartMetadataRegistry().remove(vizType); |
| 618 | + } |
| 619 | + }); |
| 620 | + |
499 | 621 | // `navigateWithState` regression for the |
500 | 622 | // dashboard-properties-changed save path. Two assertions in one shape: |
501 | 623 | // (a) the emitted path is router-relative (`/dashboard/<id>/`), not |
|
0 commit comments