From e41fd9529c718cf54c0a80e63a7c36aeff7c2cd0 Mon Sep 17 00:00:00 2001 From: "Adrien Minne (adrm)" Date: Thu, 22 Aug 2024 15:10:32 +0200 Subject: [PATCH] [IMP] pivot: add table when inserting spreadsheet pivot MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 odoo/o-spreadsheet#4896 Task: 4132016 Signed-off-by: Lucas Lefèvre (lul) --- src/plugins/ui_feature/insert_pivot.ts | 13 ++++++++----- tests/pivots/pivot_insert.test.ts | 14 ++++++++++++- tests/pivots/pivot_menu_items.test.ts | 27 +++++++++++++++++++++++++- tests/test_helpers/getters_helpers.ts | 9 +++++++++ 4 files changed, 56 insertions(+), 7 deletions(-) diff --git a/src/plugins/ui_feature/insert_pivot.ts b/src/plugins/ui_feature/insert_pivot.ts index ef2a653b06..f09e712976 100644 --- a/src/plugins/ui_feature/insert_pivot.ts +++ b/src/plugins/ui_feature/insert_pivot.ts @@ -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) { diff --git a/tests/pivots/pivot_insert.test.ts b/tests/pivots/pivot_insert.test.ts index ae1f498a6d..1cf219724b 100644 --- a/tests/pivots/pivot_insert.test.ts +++ b/tests/pivots/pivot_insert.test.ts @@ -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", () => { @@ -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", + }); + }); }); diff --git a/tests/pivots/pivot_menu_items.test.ts b/tests/pivots/pivot_menu_items.test.ts index b13ee0b745..9850df97e3 100644 --- a/tests/pivots/pivot_menu_items.test.ts +++ b/tests/pivots/pivot_menu_items.test.ts @@ -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 { @@ -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; @@ -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", + }); + }); }); diff --git a/tests/test_helpers/getters_helpers.ts b/tests/test_helpers/getters_helpers.ts index 802210f2b3..46e2128f8a 100644 --- a/tests/test_helpers/getters_helpers.ts +++ b/tests/test_helpers/getters_helpers.ts @@ -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,