From 2d6edfbd6ce60b40e66cd5ce1d864c548238bccc Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Wed, 7 Feb 2024 16:46:56 +0100 Subject: [PATCH] Node.js: Fix MapModel rowData() calling map function even if the source model returned undefined Also fix the function signature, which was wrong. --- .../__test__/js_value_conversion.spec.mts | 25 +++++++++++++++++++ api/node/index.ts | 8 ++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/api/node/__test__/js_value_conversion.spec.mts b/api/node/__test__/js_value_conversion.spec.mts index b3877905c..c87ef0bc0 100644 --- a/api/node/__test__/js_value_conversion.spec.mts +++ b/api/node/__test__/js_value_conversion.spec.mts @@ -482,6 +482,31 @@ test("MapModel", (t) => { t.is(checkModel.rowData(2), "Tisch, Roman"); }) +test("MapModel undefined rowData sourcemodel", (t) => { + const nameModel: ArrayModel = new ArrayModel([ + 1, 2, 3 + ]); + + let mapFunctionCallCount = 0; + const mapModel = new private_api.MapModel( + nameModel, + (data) => { + mapFunctionCallCount++; + return data.toString(); + } + ); + + for (let i = 0; i < mapModel.rowCount(); ++i) { + mapModel.rowData(i); + } + t.deepEqual(mapFunctionCallCount, mapModel.rowCount()); + mapFunctionCallCount = 0; + t.is(nameModel.rowData(nameModel.rowCount()), undefined); + t.deepEqual(mapFunctionCallCount, 0); + t.is(mapModel.rowData(mapModel.rowCount()), undefined); + t.deepEqual(mapFunctionCallCount, 0); +}) + test('ArrayModel rowCount', (t) => { let compiler = new private_api.ComponentCompiler; let definition = compiler.buildFromSource(` diff --git a/api/node/index.ts b/api/node/index.ts index 7e40823d7..32e37d2c1 100644 --- a/api/node/index.ts +++ b/api/node/index.ts @@ -502,8 +502,12 @@ export class MapModel extends Model { * @param row index in range 0..(rowCount() - 1). * @returns undefined if row is out of range otherwise the data. */ - rowData(row: number): U { - return this.#mapFunction(this.sourceModel.rowData(row)); + rowData(row: number): U | undefined { + let data = this.sourceModel.rowData(row); + if (data === undefined) { + return undefined; + } + return this.#mapFunction(data); } } }