Skip to content

Commit e29ea88

Browse files
committed
[IMP] Pivot: added a check in the allowdispatch
Spec : Currently, one could dispatch the command ADD_PIVOT by specifying a pivotId which corresponds to an already existing pivot. This is quite error prone as it could lead to a situation where we erase an existing PIVOT by mistake.  The goal is to prevent this kind of situation by adding a check in the allowDispatch of the core plugin Pivot which should reject the command ADD_PIVOT if it targets an already exisitng pivot (similarly to the check done for the command INSERT_PIVOT). closes #7573 Task: 5360591 Signed-off-by: Pierre Rousseau (pro) <[email protected]>
1 parent e551f20 commit e29ea88

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

packages/o-spreadsheet-engine/src/plugins/core/pivot.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ export class PivotCorePlugin extends CorePlugin<CoreState> implements CoreState
4646
allowDispatch(cmd: CoreCommand) {
4747
switch (cmd.type) {
4848
case "ADD_PIVOT": {
49+
if (cmd.pivotId in this.pivots) {
50+
return CommandResult.PivotIdTaken;
51+
}
4952
return this.checkValidations(
5053
cmd.pivot,
5154
this.checkDuplicatedMeasureIds,

packages/o-spreadsheet-engine/src/types/commands.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,6 +1460,7 @@ export const enum CommandResult {
14601460
SheetIsHidden = "SheetIsHidden",
14611461
InvalidTableResize = "InvalidTableResize",
14621462
PivotIdNotFound = "PivotIdNotFound",
1463+
PivotIdTaken = "PivotIdTaken",
14631464
PivotInError = "PivotInError",
14641465
EmptyName = "EmptyName",
14651466
ValueCellIsInvalidFormula = "ValueCellIsInvalidFormula",

tests/pivots/pivot_plugin.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,14 @@ describe("Pivot plugin", () => {
247247
expect(updateResult).toBeCancelledBecause(CommandResult.PivotIdNotFound);
248248
});
249249

250+
test("cannot add a pivot with an existing id", () => {
251+
const model = new Model();
252+
const createResult1 = addPivot(model, "A1:A2", {}, "1");
253+
expect(createResult1.isSuccessful).toBe(true);
254+
const createResult2 = addPivot(model, "A1:A2", {}, "1");
255+
expect(createResult2).toBeCancelledBecause(CommandResult.PivotIdTaken);
256+
});
257+
250258
test("cannot duplicate a pivot with a wrong id", () => {
251259
const model = new Model();
252260
const updateResult = model.dispatch("DUPLICATE_PIVOT", {

tests/pivots/spreadsheet_pivot/spreadsheet_pivot_side_panel.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -880,9 +880,9 @@ describe("Spreadsheet pivot side panel", () => {
880880
measures: [{ id: "Amount", fieldName: "Amount", aggregator: "sum" }],
881881
sortedColumn,
882882
},
883-
"1"
883+
"2"
884884
);
885-
env.openSidePanel("PivotSidePanel", { pivotId: "1" });
885+
env.openSidePanel("PivotSidePanel", { pivotId: "2" });
886886
await nextTick();
887887
});
888888

@@ -893,29 +893,29 @@ describe("Spreadsheet pivot side panel", () => {
893893
});
894894

895895
test("Does not display sorting for pivot with no sorting or invalid sorting ", async () => {
896-
updatePivot(model, "1", { sortedColumn: undefined });
897-
env.openSidePanel("PivotSidePanel", { pivotId: "1" });
896+
updatePivot(model, "2", { sortedColumn: undefined });
897+
env.openSidePanel("PivotSidePanel", { pivotId: "2" });
898898
await nextTick();
899899
expect(".o-sidePanel .o-pivot-sort").toHaveCount(0);
900900

901-
updatePivot(model, "1", {
901+
updatePivot(model, "2", {
902902
sortedColumn: { order: "asc", measure: "Yolo", domain: [] },
903903
});
904904
await nextTick();
905905
expect(".o-sidePanel .o-pivot-sort").toHaveCount(0);
906906
});
907907

908908
test("Pivot sorting is removed when removing the sorted measure", async () => {
909-
expect(model.getters.getPivotCoreDefinition("1").sortedColumn).toEqual(sortedColumn);
909+
expect(model.getters.getPivotCoreDefinition("2").sortedColumn).toEqual(sortedColumn);
910910
click(fixture, ".pivot-measure .fa-trash");
911-
expect(model.getters.getPivotCoreDefinition("1").sortedColumn).toBeUndefined();
911+
expect(model.getters.getPivotCoreDefinition("2").sortedColumn).toBeUndefined();
912912
});
913913

914914
test("Pivot sorting is removed when removing a column", async () => {
915-
expect(model.getters.getPivotCoreDefinition("1").sortedColumn).toEqual(sortedColumn);
915+
expect(model.getters.getPivotCoreDefinition("2").sortedColumn).toEqual(sortedColumn);
916916
const column = fixture.querySelectorAll(".pivot-dimension")[0];
917917
click(column, ".fa-trash");
918-
expect(model.getters.getPivotCoreDefinition("1").sortedColumn).toBeUndefined();
918+
expect(model.getters.getPivotCoreDefinition("2").sortedColumn).toBeUndefined();
919919
});
920920
});
921921

0 commit comments

Comments
 (0)