Node.js: Fix MapModel rowData() calling map function even if the source model returned undefined

Also fix the function signature, which was wrong.
This commit is contained in:
Simon Hausmann 2024-02-07 16:46:56 +01:00 committed by Simon Hausmann
parent 55a8f202ea
commit 2d6edfbd6c
2 changed files with 31 additions and 2 deletions

View file

@ -482,6 +482,31 @@ test("MapModel", (t) => {
t.is(checkModel.rowData(2), "Tisch, Roman");
})
test("MapModel undefined rowData sourcemodel", (t) => {
const nameModel: ArrayModel<Number> = new ArrayModel([
1, 2, 3
]);
let mapFunctionCallCount = 0;
const mapModel = new private_api.MapModel<Number, String>(
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(`

View file

@ -502,8 +502,12 @@ export class MapModel<T, U> extends Model<U> {
* @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);
}
}
}