[FIX] documents_spreadsheet: fix See record menu if not loaded

Steps to reproduce:

- wrap a pivot function inside a IFERROR e.g. =IFERROR(PIVOT("1", "probability"), 42)
- reload the spreadsheet
- before the pivot is loaded (throttle the network in the dev tools): right click the cell
- click on "See records" menu item
=> boom

closes odoo/odoo#162759

Task: 3847477
X-original-commit: aeadd065e869a5cd6b971d6d458ba9d8e1edcc1c
Signed-off-by: Lucas Lefèvre (lul) <lul@odoo.com>
This commit is contained in:
Lucas Lefèvre (lul)
2024-04-21 09:42:54 +00:00
parent 459412b860
commit 7351f4241c
3 changed files with 35 additions and 2 deletions
@@ -27,7 +27,13 @@ export const SEE_RECORDS_PIVOT_VISIBLE = (position, env) => {
const cell = env.model.getters.getCorrespondingFormulaCell(position);
const evaluatedCell = env.model.getters.getEvaluatedCell(position);
const argsDomain = env.model.getters.getPivotDomainArgsFromPosition(position);
const pivotId = env.model.getters.getPivotIdFromPosition(position);
if (!env.model.getters.isExistingPivot(pivotId)) {
return false;
}
const dataSource = env.model.getters.getPivotDataSource(pivotId);
return (
dataSource.isReady() &&
evaluatedCell.type !== "empty" &&
evaluatedCell.type !== "error" &&
argsDomain !== undefined &&
@@ -199,7 +199,7 @@ export class PivotUIPlugin extends spreadsheet.UIPlugin {
const cell = this.getters.getCorrespondingFormulaCell(position);
if (cell && cell.isFormula) {
const pivotFunction = this.getters.getFirstPivotFunction(cell.compiledFormula.tokens);
if (pivotFunction) {
if (pivotFunction && pivotFunction.args[0]) {
return pivotFunction.args[0].toString();
}
}
@@ -1,5 +1,5 @@
/** @odoo-module */
import { nextTick } from "@web/../tests/helpers/utils";
import { makeDeferred, nextTick } from "@web/../tests/helpers/utils";
import { selectCell } from "@spreadsheet/../tests/utils/commands";
import { doMenuAction, getActionMenu } from "@spreadsheet/../tests/utils/ui";
@@ -183,3 +183,30 @@ QUnit.test("Can see records on ODOO.PIVOT.TABLE cells", async function (assert)
setCellContent(model, "A3", `=ODOO.PIVOT.TABLE("1",,,FALSE)`, "42");
await checkCells(data_cells);
});
QUnit.test(
"See records is not visible if the pivot is not loaded, even if the cell has a value",
async function (assert) {
let deferred = undefined;
const { env, model } = await createSpreadsheetWithPivot({
arch: /*xml*/ `
<pivot>
<field name="probability" type="measure"/>
</pivot>
`,
mockRPC: async function (route, args) {
if (deferred && args.method === "read_group" && args.model === "partner") {
await deferred;
}
},
});
setCellContent(model, "A1", '=IFERROR(ODOO.PIVOT("1","probability"), 42)');
deferred = makeDeferred();
model.dispatch("REFRESH_ALL_DATA_SOURCES");
const action = cellMenuRegistry.getAll().find((item) => item.id === "pivot_see_records");
assert.strictEqual(action.isVisible(env), false);
deferred.resolve();
await nextTick();
assert.strictEqual(action.isVisible(env), true);
}
);