Skip to content

Commit

Permalink
⚡ Add expression callback tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ivov committed Dec 13, 2021
1 parent 984a7a5 commit 52ff01a
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 120 deletions.
236 changes: 118 additions & 118 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/workflow/src/Expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ export class Expression {

// Execute the expression
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
const returnValue = tmpl.tmpl(parameterValue, data);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-explicit-any
const returnValue = tmpl.tmpl(parameterValue, data) as any;
if (typeof returnValue === 'function') {
throw new Error('Expression resolved to a function. Please add "()"');
} else if (returnValue !== null && typeof returnValue === 'object') {
Expand Down
49 changes: 49 additions & 0 deletions packages/workflow/test/Expression.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as tmpl from 'riot-tmpl';

describe('Expression', () => {
beforeAll(() => {
tmpl.brackets.set('{{ }}');
});

test('evaluate sum of integers', () => {
const evaluated = tmpl.tmpl('{{ 1 + 2 }}');
expect(evaluated).toEqual(3);
});

test('evaluate array method with same-line anonymous callback', () => {
const evaluated = tmpl.tmpl('{{ [1, 2, 3].filter(function (v) { return v > 2 }) }}');
expect(evaluated).toEqual([3]);
});

test('evaluate array method with single-line anonymous callback', () => {
const evaluated = tmpl.tmpl(`{{ [1, 2, 3].filter(function (v) {
return v > 2
}) }}`);
expect(evaluated).toEqual([3]);
});

test('evaluate array method with multi-line anonymous callback', () => {
const evaluated = tmpl.tmpl(`{{ [1, 2, 3].filter(function (v) {
const a = 1;
return v > 2
}) }}`);
expect(evaluated).toEqual([3]);
});

test('evaluate array method with same-line arrow callback, unbracketed return', () => {
const evaluated = tmpl.tmpl('{{ [1, 2, 3].filter((v) => v > 2) }}');
expect(evaluated).toEqual([3]);
});

test('evaluate array method with single-line anonymous callback, bracketed return', () => {
const evaluated = tmpl.tmpl(`{{ [1, 2, 3].filter((v) => { return v > 2 }) }}`);
expect(evaluated).toEqual([3]);
});

test('evaluate array method with single-line arrow callback', () => {
const evaluated = tmpl.tmpl(`{{ [1, 2, 3].filter((v) => {
return v > 2
}) }}`);
expect(evaluated).toEqual([3]);
});
});
20 changes: 20 additions & 0 deletions packages/workflow/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
declare module 'riot-tmpl' {
export const brackets: {
/**
* Set expression brackets other than the default `{ ... }`
*/
set: (brackets: string) => void;
};

export const tmpl: {
/**
* Evaluate a `tmpl` expression.
*/
(expression: string, data?: object): unknown;

/**
* Handle an error from evaluating a `tmpl` expression.
*/
errorHandler: () => void;
};
}

0 comments on commit 52ff01a

Please sign in to comment.