Skip to content

Commit

Permalink
defect(expressionJs): return undefined if variable not exist in model (
Browse files Browse the repository at this point in the history
…#8)

* Check if expression is invalid
* check if dependencies is not exist in model and return undefined

Covers [UID-630](https://bonitasoft.atlassian.net/browse/UID-630)
  • Loading branch information
benjaminParisel authored Jan 20, 2022
1 parent 3ffce05 commit 3b5a4dd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/resolvers/ExpressionResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@ export class ExpressionResolver extends Resolver {

resolve(): void {
// use strict. Avoid pollution of the global object.
let expression = new Function(
'$data',//inject all data
'uiTranslate',//inject translate function
'"use strict";' + this.content);
try {
let expression = new Function(
'$data',//inject all data
'uiTranslate',//inject translate function
'"use strict";' + this.content);

this.model[this.name] = expression(
this.model, // all data
//TODO Implement Translate
(text: string) => text// translate function
);
this.model[this.name] = expression(
this.model, // all data
//TODO Implement Translate
(text: string) => text// translate function
);
}catch (e) {
console.error('This following error occur on expression resolution',e);
}
}

hasDependencies(): boolean {
Expand Down
18 changes: 18 additions & 0 deletions tests/resolvers/ExpressionResolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,22 @@ describe('expressionResolver', () => {
expect(typeof model.myJsVar).to.equal("string");
expect(model.myJsVar).to.equal('return "Hello here"');
});

it('should return undefined when referenced variable is not exist in model', () => {
let expression = new ExpressionResolver(model, 'myJsVar', 'return $data.unknownVariable');

expression.resolve();

expect(expression.hasDependencies()).to.equal(true);
expect(model.myJsVar).to.equal(undefined);
});

it('should return undefined when variable used in expression is invalid', () => {
let expression = new ExpressionResolver(model, 'myJsVar', 'return $unknownVariable');

expression.resolve();

expect(expression.hasDependencies()).to.equal(false);
expect(model.myJsVar).to.equal(undefined);
});
});

0 comments on commit 3b5a4dd

Please sign in to comment.