Skip to content
Snippets Groups Projects
Unverified Commit 8e81f12e authored by Nick Fitzpatrick's avatar Nick Fitzpatrick Committed by GitHub
Browse files

Merging table.columns for dashcards (#35259)

* temp

* Adding unit test
parent b2b9e168
No related branches found
No related tags found
No related merge requests found
......@@ -243,9 +243,34 @@ export function mergeSettings(first = {}, second = {}) {
}
}
}
if (first["table.columns"] && second["table.columns"]) {
merged["table.columns"] = mergeTableColumns(
first["table.columns"],
second["table.columns"],
);
}
return merged;
}
const mergeTableColumns = (firstTableColumns, secondTableColumns) => {
const addedColumns = firstTableColumns.filter(
({ name }) => secondTableColumns.findIndex(col => col.name === name) === -1,
);
const removedColumns = secondTableColumns
.filter(
({ name }) =>
firstTableColumns.findIndex(col => col.name === name) === -1,
)
.map(({ name }) => name);
return [
...secondTableColumns.filter(({ name }) => !removedColumns.includes(name)),
...addedColumns,
];
};
export function getClickBehaviorSettings(settings) {
const newSettings = {};
......
import { ORDERS } from "metabase-types/api/mocks/presets";
import { createMockTableColumnOrderSetting } from "metabase-types/api/mocks";
// NOTE: need to load visualizations first for getSettings to work
import "metabase/visualizations/index";
......@@ -196,6 +199,81 @@ describe("settings framework", () => {
mergeSettings({}, { column_settings: { col1: { set1: "val" } } }),
).toEqual({ column_settings: { col1: { set1: "val" } } });
});
describe("table.columns", () => {
const ID_COLUMN = createMockTableColumnOrderSetting({
name: "ID",
fieldRef: ["field", ORDERS.ID, null],
});
const QUANTITY_COLUMN = createMockTableColumnOrderSetting({
name: "QUANTITY",
fieldRef: ["field", ORDERS.QUANTITY, null],
});
const TAX_COLUMN = createMockTableColumnOrderSetting({
name: "TAX",
fieldRef: ["field", ORDERS.TAX, null],
});
const DISCOUNT_COLUMN = createMockTableColumnOrderSetting({
name: "DISCOUNT",
fieldRef: ["field", ORDERS.DISCOUNT, null],
});
it("should remove columns that don't appear in the first settings", () => {
expect(
mergeSettings(
{
"table.columns": [ID_COLUMN, QUANTITY_COLUMN],
},
{
"table.columns": [ID_COLUMN, QUANTITY_COLUMN, TAX_COLUMN],
},
),
).toEqual({
"table.columns": [ID_COLUMN, QUANTITY_COLUMN],
});
});
it("should add new columns that don't appear in the second settings", () => {
expect(
mergeSettings(
{
"table.columns": [ID_COLUMN, QUANTITY_COLUMN, DISCOUNT_COLUMN],
},
{
"table.columns": [ID_COLUMN, QUANTITY_COLUMN],
},
),
).toEqual({
"table.columns": [ID_COLUMN, QUANTITY_COLUMN, DISCOUNT_COLUMN],
});
});
it("should preserve settings and order from the second settings", () => {
expect(
mergeSettings(
{
"table.columns": [ID_COLUMN, QUANTITY_COLUMN, DISCOUNT_COLUMN],
},
{
"table.columns": [
DISCOUNT_COLUMN,
{ ...ID_COLUMN, enabled: false },
QUANTITY_COLUMN,
TAX_COLUMN,
],
},
),
).toEqual({
"table.columns": [
DISCOUNT_COLUMN,
{ ...ID_COLUMN, enabled: false },
QUANTITY_COLUMN,
],
});
});
});
});
describe("getClickBehaviorSettings", () => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment