From 4da04e0a05ce708a8088ecc7d5ac51deeee024b1 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 17 Feb 2025 11:04:30 +0100 Subject: [PATCH] Add parameter tests --- .../CodeEditor/validator/tests/hbs.spec.ts | 56 +++++++++++++++++-- packages/builder/tsconfig.json | 3 +- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/packages/builder/src/components/common/CodeEditor/validator/tests/hbs.spec.ts b/packages/builder/src/components/common/CodeEditor/validator/tests/hbs.spec.ts index 1555175169..fe41e4c1e1 100644 --- a/packages/builder/src/components/common/CodeEditor/validator/tests/hbs.spec.ts +++ b/packages/builder/src/components/common/CodeEditor/validator/tests/hbs.spec.ts @@ -1,7 +1,8 @@ import { validateHbsTemplate } from "../hbs" +import { CodeValidator } from "@/types" describe("hbs validator", () => { - it("can validate empty strings", () => { + it("validate empty strings", () => { const text = "" const validators = {} @@ -9,7 +10,7 @@ describe("hbs validator", () => { expect(result).toHaveLength(0) }) - it("can validate strings without hbs expressions", () => { + it("validate strings without hbs expressions", () => { const text = "first line\nand another one" const validators = {} @@ -22,14 +23,14 @@ describe("hbs validator", () => { fieldName: {}, } - it("can validate valid expressions", () => { + it("validate valid expressions", () => { const text = "{{ fieldName }}" const result = validateHbsTemplate(text, validators) expect(result).toHaveLength(0) }) - it("can validate invalid expressions", () => { + it("throws on invalid expressions", () => { const text = "{{ anotherFieldName }}" const result = validateHbsTemplate(text, validators) @@ -43,7 +44,7 @@ describe("hbs validator", () => { ]) }) - it("can validate untrimmed invalid expressions", () => { + it("throws on untrimmed invalid expressions", () => { const text = " {{ anotherFieldName }}" const result = validateHbsTemplate(text, validators) @@ -57,7 +58,7 @@ describe("hbs validator", () => { ]) }) - it("can validate invalid expressions between valid lines", () => { + it("throws on invalid expressions between valid lines", () => { const text = "literal expression\nthe value is {{ anotherFieldName }}\nanother expression" @@ -72,4 +73,47 @@ describe("hbs validator", () => { ]) }) }) + + describe("expressions with parameters", () => { + const validators: CodeValidator = { + helperFunction: { + arguments: ["a", "b", "c"], + }, + } + + it("validate valid params", () => { + const text = "{{ helperFunction 1 99 'a' }}" + + const result = validateHbsTemplate(text, validators) + expect(result).toHaveLength(0) + }) + + it("throws on too few params", () => { + const text = "{{ helperFunction 100 }}" + + const result = validateHbsTemplate(text, validators) + expect(result).toEqual([ + { + from: 0, + message: `Helper "helperFunction" expects 3 parameters (a, b, c), but got 1.`, + severity: "error", + to: 24, + }, + ]) + }) + + it("throws on too many params", () => { + const text = "{{ helperFunction 1 99 'a' 100 }}" + + const result = validateHbsTemplate(text, validators) + expect(result).toEqual([ + { + from: 0, + message: `Helper "helperFunction" expects 3 parameters (a, b, c), but got 4.`, + severity: "error", + to: 34, + }, + ]) + }) + }) }) diff --git a/packages/builder/tsconfig.json b/packages/builder/tsconfig.json index d698e2fe1d..a9e640123f 100644 --- a/packages/builder/tsconfig.json +++ b/packages/builder/tsconfig.json @@ -14,5 +14,6 @@ "assets/*": ["assets/*"], "@/*": ["src/*"] } - } + }, + "exclude": [] }