Skip to content

Commit

Permalink
[IMP] pivot: add table when inserting spreadsheet pivot
Browse files Browse the repository at this point in the history
The same ways we have a table when re-inserting a pivot, we should
have a table when inserting a pivot for the first time.

closes #4896

Task: 4132016
Signed-off-by: Lucas Lefèvre (lul) <[email protected]>
  • Loading branch information
hokolomopo committed Sep 20, 2024
1 parent d80d182 commit e41fd95
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
13 changes: 8 additions & 5 deletions src/plugins/ui_feature/insert_pivot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,15 @@ export class InsertPivotPlugin extends UIPlugin {
sheetIdFrom: currentSheetId,
sheetIdTo: sheetId,
});
this.dispatch("UPDATE_CELL", {
const pivot = this.getters.getPivot(pivotId);
this.insertPivotWithTable(
sheetId,
col: 0,
row: 0,
content: `=PIVOT(${formulaId})`,
});
0,
0,
pivotId,
pivot.getTableStructure().export(),
"dynamic"
);
}

private duplicatePivotInNewSheet(pivotId: UID, newPivotId: UID, newSheetId: UID) {
Expand Down
14 changes: 13 additions & 1 deletion tests/pivots/pivot_insert.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Model } from "../../src";
import { PIVOT_TABLE_CONFIG } from "../../src/constants";
import { toZone } from "../../src/helpers";
import { insertPivot } from "../test_helpers/commands_helpers";
import { getCellText } from "../test_helpers/getters_helpers";
import { getCellText, getCoreTable } from "../test_helpers/getters_helpers";

describe("Insert pivot command", () => {
test("Can insert a pivot in a cell", () => {
Expand Down Expand Up @@ -36,4 +37,15 @@ describe("Insert pivot command", () => {
insertPivot(model, "A1", "pivot1", "Sheet2");
expect(model.getters.getPivotCoreDefinition("pivot1")["dataSet"].zone).toEqual(toZone("A1:B2"));
});

test("Inserting a pivot create a table", () => {
const model = new Model();
insertPivot(model, "A1", "pivot1", "Sheet2");
expect(getCellText(model, "A1")).toEqual("=PIVOT(1)");
expect(getCoreTable(model, "A1")).toMatchObject({
range: { zone: toZone("A1") },
config: PIVOT_TABLE_CONFIG,
type: "dynamic",
});
});
});
27 changes: 26 additions & 1 deletion tests/pivots/pivot_menu_items.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Model, SpreadsheetChildEnv } from "../../src";
import { PIVOT_TABLE_CONFIG } from "../../src/constants";
import { toCartesian, toZone } from "../../src/helpers";
import { cellMenuRegistry, topbarMenuRegistry } from "../../src/registries";
import {
Expand All @@ -7,14 +8,22 @@ import {
redo,
selectCell,
setCellContent,
setSelection,
undo,
} from "../test_helpers/commands_helpers";
import { getCell, getCellText, getEvaluatedGrid, getTable } from "../test_helpers/getters_helpers";
import {
getCell,
getCellText,
getCoreTable,
getEvaluatedGrid,
getTable,
} from "../test_helpers/getters_helpers";
import { createModelFromGrid, doAction, getNode, makeTestEnv } from "../test_helpers/helpers";
import { addPivot } from "../test_helpers/pivot_helpers";

const reinsertDynamicPivotPath = ["data", "reinsert_dynamic_pivot", "reinsert_dynamic_pivot_1"];
const reinsertStaticPivotPath = ["data", "reinsert_static_pivot", "reinsert_static_pivot_1"];
const insertPivotPath = ["insert", "insert_pivot"];

describe("Pivot properties menu item", () => {
let model: Model;
Expand Down Expand Up @@ -536,4 +545,20 @@ describe("Pivot reinsertion menu item", () => {
expect(getNode(reinsertDynamicPivotPath, env, topbarMenuRegistry).isVisible(env)).toBeTruthy();
expect(getNode(reinsertStaticPivotPath, env, topbarMenuRegistry).isVisible(env)).toBeTruthy();
});

test("Insert a pivot", () => {
const model = new Model();
const sheetId = model.getters.getActiveSheetId();
const env = makeTestEnv({ model });
setSelection(model, ["A1:B2"]);
doAction(insertPivotPath, env, topbarMenuRegistry);
expect(model.getters.getActiveSheetId()).not.toEqual(sheetId);
expect(model.getters.getPivotIds()).toHaveLength(1);
expect(getCellText(model, "A1")).toEqual(`=PIVOT(1)`);
expect(getCoreTable(model, "A1")).toMatchObject({
range: { zone: toZone("A1") },
config: PIVOT_TABLE_CONFIG,
type: "dynamic",
});
});
});
9 changes: 9 additions & 0 deletions tests/test_helpers/getters_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,15 @@ export function getTable(
return model.getters.getTable({ sheetId, col, row });
}

export function getCoreTable(
model: Model,
xc: string,
sheetId: UID = model.getters.getActiveSheetId()
) {
const { col, row } = toCartesian(xc);
return model.getters.getCoreTable({ sheetId, col, row });
}

export function getFilter(
model: Model,
xc: string,
Expand Down

0 comments on commit e41fd95

Please sign in to comment.