Skip to content

Commit

Permalink
Revert changes within removeWidgetsFromDashboard to go back to what i…
Browse files Browse the repository at this point in the history
…s on main. Updates the test and its resource
  • Loading branch information
NZepeda committed May 31, 2024
1 parent 0819abe commit 00a4cda
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 81 deletions.
119 changes: 95 additions & 24 deletions src/4.3_to_5.0/__test_resources__/testDashboard.ts
Original file line number Diff line number Diff line change
@@ -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: [
{
Expand All @@ -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",
},
Expand All @@ -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: [],
Expand Down
46 changes: 6 additions & 40 deletions src/4.3_to_5.0/removeWidgetsFromDashboard.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
28 changes: 11 additions & 17 deletions src/4.3_to_5.0/removeWidgetsFromDashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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;
};

0 comments on commit 00a4cda

Please sign in to comment.