diff --git a/src/4.3_to_5.0/__test_resources__/testDashboard.ts b/src/4.3_to_5.0/__test_resources__/testDashboard.ts index a4c35dfc..171d73f5 100644 --- a/src/4.3_to_5.0/__test_resources__/testDashboard.ts +++ b/src/4.3_to_5.0/__test_resources__/testDashboard.ts @@ -1,10 +1,10 @@ import { DashboardState } from "@activeviam/activeui-sdk-5.0"; -export const testDashboard: DashboardState<"serialized"> = { - name: "Test dashboard", +export const testDashboard: DashboardState<"deserialized"> = { + name: "Dashboard with multiple widgets", pages: { "p-0": { - name: "test widget", + name: "", layout: { children: [ { @@ -14,9 +14,13 @@ export const testDashboard: DashboardState<"serialized"> = { leafKey: "3", }, { - size: 0.91, + size: 0.455, leafKey: "1", }, + { + size: 0.455, + leafKey: "5", + }, ], direction: "column", }, @@ -40,37 +44,104 @@ export const testDashboard: DashboardState<"serialized"> = { content: { "1": { query: { - mdx: "SELECT NON EMPTY Crossjoin([As Of Date].[As Of Date].[As Of Date].Members, {[Measures].[HypoPnL]}) ON COLUMNS, NON EMPTY Hierarchize(Crossjoin([Book Structure].[Profit Center].[Profit Center].Members, [Book Structure].[Book].[Book].Members)) ON ROWS FROM [TestCube]", + mdx: `SELECT + NON EMPTY { + [Measures].[contributors.COUNT] + } ON COLUMNS + FROM [EquityDerivativesCube]`, updateMode: "once", }, filters: [], - queryContext: [ - { - key: "mdx.hiddengrandtotals", - value: 1, - }, - ], + queryContext: [], mapping: { - rows: [ - "[Book Structure].[Profit Center].[Profit Center]", - "[Book Structure].[Book].[Book]", - ], - columns: ["[As Of Date].[As Of Date].[As Of Date]", "ALL_MEASURES"], - measures: ["[Measures].[HypoPnL]"], + rows: [], + columns: ["ALL_MEASURES"], + measures: ["[Measures].[contributors.COUNT]"], }, - name: "Pivot Table", - serverKey: "my-server", + name: "My first table", + serverKey: "ap-murex-cube", widgetKey: "table", columnWidths: {}, }, - "2": {}, + "2": { + query: { + mdx: `SELECT + NON EMPTY { + [Measures].[contributors.COUNT] + } ON COLUMNS + FROM [EquityDerivativesCube]`, + updateMode: "once", + }, + filters: [], + queryContext: [], + mapping: { + rows: [], + columns: ["ALL_MEASURES"], + measures: ["[Measures].[contributors.COUNT]"], + }, + name: "my table", + serverKey: "my server", + widgetKey: "table", + }, "3": { - name: "Dashboard Filters", - widgetKey: "filters", + query: { + mdx: `SELECT + NON EMPTY { + [Measures].[contributors.COUNT] + } ON COLUMNS + FROM [EquityDerivativesCube]`, + updateMode: "once", + }, + filters: [], + queryContext: [], + mapping: { + rows: [], + columns: ["ALL_MEASURES"], + measures: ["[Measures].[contributors.COUNT]"], + }, + name: "my table", + serverKey: "my server", + widgetKey: "table", }, "4": { - name: "Mdx Editor", - widgetKey: "mdx-editor", + query: { + mdx: `SELECT + NON EMPTY { + [Measures].[contributors.COUNT] + } ON COLUMNS + FROM [EquityDerivativesCube]`, + updateMode: "once", + }, + filters: [], + queryContext: [], + mapping: { + rows: [], + columns: ["ALL_MEASURES"], + measures: ["[Measures].[contributors.COUNT]"], + }, + name: "my table", + serverKey: "my server", + widgetKey: "table", + }, + "5": { + query: { + mdx: `SELECT + NON EMPTY { + [Measures].[contributors.COUNT] + } ON COLUMNS + FROM [EquityDerivativesCube]`, + updateMode: "once", + }, + filters: [], + queryContext: [], + mapping: { + rows: [], + columns: ["ALL_MEASURES"], + measures: ["[Measures].[contributors.COUNT]"], + }, + name: "my table", + serverKey: "my server", + widgetKey: "table", }, }, filters: [], diff --git a/src/4.3_to_5.0/removeWidgetsFromDashboard.test.ts b/src/4.3_to_5.0/removeWidgetsFromDashboard.test.ts index 7429bc20..2616cd63 100644 --- a/src/4.3_to_5.0/removeWidgetsFromDashboard.test.ts +++ b/src/4.3_to_5.0/removeWidgetsFromDashboard.test.ts @@ -1,51 +1,17 @@ -import { - AWidgetState, - deserializeDashboardState, -} from "@activeviam/activeui-sdk-5.0"; import { testDashboard } from "./__test_resources__/testDashboard"; import { removeWidgetsFromDashboard } from "./removeWidgetsFromDashboard"; describe("removeWidgetsFromDashboard", () => { it("removes the widgets identified by `keysOfLeavesToRemove` from the dashboard", () => { - const keysOfWidgetsToRemove = ["filters"]; - const widgetPluginKeysInDashboard: string[] = []; - const keysOfLeavesToRemove: { - [pageKey: string]: string[]; - } = {}; + const keysOfLeavesToRemove: { [key: string]: string[] } = { + "p-0": ["2", "3", "4"], + }; - // Safeguard to make sure that the test makes sense: before checking that the key has been removed in the migrated dashboard, check that it's here in the first place in the legacy dashboard. - Object.values(testDashboard.pages).forEach((page, index) => { - const pageKey = `p-${index}`; - Object.values(page.content).forEach((widget: AWidgetState) => { - if (widgetPluginKeysInDashboard.includes(widget.widgetKey)) { - keysOfLeavesToRemove[pageKey] = [ - ...(keysOfLeavesToRemove[pageKey] ?? []), - widget.widgetKey, - ]; - } - widgetPluginKeysInDashboard.push(widget.widgetKey); - }); - }); - - expect(widgetPluginKeysInDashboard).toEqual( - expect.arrayContaining(keysOfWidgetsToRemove), - ); - - const dashboardWithWidgetsRemoved = removeWidgetsFromDashboard( - deserializeDashboardState(testDashboard), + const updatedDashboard = removeWidgetsFromDashboard( + testDashboard, keysOfLeavesToRemove, ); - const widgetKeysInDashboardAfterRemoval: string[] = []; - - Object.values(dashboardWithWidgetsRemoved.pages).forEach((page) => { - Object.values(page.content).forEach((widget) => { - widgetKeysInDashboardAfterRemoval.push(widget.widgetKey); - }); - }); - - widgetKeysInDashboardAfterRemoval.forEach((widgetKey) => - expect(!keysOfWidgetsToRemove.includes(widgetKey)), - ); + expect(updatedDashboard).toMatchInlineSnapshot(); }); }); diff --git a/src/4.3_to_5.0/removeWidgetsFromDashboard.ts b/src/4.3_to_5.0/removeWidgetsFromDashboard.ts index 967cd381..21227b08 100644 --- a/src/4.3_to_5.0/removeWidgetsFromDashboard.ts +++ b/src/4.3_to_5.0/removeWidgetsFromDashboard.ts @@ -3,7 +3,7 @@ import { getLayoutPath, removeWidget, } from "@activeviam/activeui-sdk-5.0"; -import _cloneDeep from "lodash/cloneDeep"; +import { produce } from "immer"; /** * Removes the widgets identified by `keysOfLeavesToRemove` from the given `dashboard`. @@ -12,23 +12,17 @@ export const removeWidgetsFromDashboard = ( dashboard: DashboardState<"deserialized">, keysOfLeavesToRemove: { [key: string]: string[] }, ): DashboardState<"deserialized"> => { - const dashboardWithWidgetsRemoved = _cloneDeep(dashboard); - - Object.keys(keysOfLeavesToRemove).forEach((pageKey) => { - keysOfLeavesToRemove[pageKey].forEach((leafKey) => { - const layoutPath = getLayoutPath( - dashboardWithWidgetsRemoved.pages[pageKey].layout, - leafKey, - ); - const updatedPage = removeWidget({ - dashboardState: dashboardWithWidgetsRemoved, - layoutPath, - leafKey, - pageKey, + return produce(dashboard, (draft) => { + Object.keys(keysOfLeavesToRemove).forEach((pageKey) => { + keysOfLeavesToRemove[pageKey].forEach((leafKey) => { + const layoutPath = getLayoutPath(draft.pages[pageKey].layout, leafKey); + draft.pages[pageKey] = removeWidget({ + dashboardState: draft, + layoutPath, + leafKey, + pageKey, + }).pages[pageKey]; }); - dashboardWithWidgetsRemoved.pages[pageKey] = updatedPage.pages[pageKey]; }); }); - - return dashboardWithWidgetsRemoved; };