Add wrong hbs tests

This commit is contained in:
Adria Navarro 2025-02-27 14:41:37 +01:00
parent e8b6a3c90e
commit af74d3df62
1 changed files with 23 additions and 9 deletions

View File

@ -2,7 +2,7 @@ import { validateHbsTemplate } from "../hbs"
import { CodeValidator } from "@/types" import { CodeValidator } from "@/types"
describe("hbs validator", () => { describe("hbs validator", () => {
it("validate empty strings", () => { it("validates empty strings", () => {
const text = "" const text = ""
const validators = {} const validators = {}
@ -10,7 +10,7 @@ describe("hbs validator", () => {
expect(result).toHaveLength(0) expect(result).toHaveLength(0)
}) })
it("validate strings without hbs expressions", () => { it("validates strings without hbs expressions", () => {
const text = "first line\nand another one" const text = "first line\nand another one"
const validators = {} const validators = {}
@ -23,7 +23,7 @@ describe("hbs validator", () => {
fieldName: {}, fieldName: {},
} }
it("validate valid expressions", () => { it("validates valid expressions", () => {
const text = "{{ fieldName }}" const text = "{{ fieldName }}"
const result = validateHbsTemplate(text, validators) const result = validateHbsTemplate(text, validators)
@ -105,7 +105,7 @@ describe("hbs validator", () => {
}, },
} }
it("validate valid params", () => { it("validates valid params", () => {
const text = "{{ helperFunction 1 99 'a' }}" const text = "{{ helperFunction 1 99 'a' }}"
const result = validateHbsTemplate(text, validators) const result = validateHbsTemplate(text, validators)
@ -152,21 +152,21 @@ describe("hbs validator", () => {
}, },
} }
it("validate valid params", () => { it("validates valid params", () => {
const text = "{{#bodyFunction 1 99 }}body{{/bodyFunction}}" const text = "{{#bodyFunction 1 99 }}body{{/bodyFunction}}"
const result = validateHbsTemplate(text, validators) const result = validateHbsTemplate(text, validators)
expect(result).toHaveLength(0) expect(result).toHaveLength(0)
}) })
it("validate empty bodies", () => { it("validates empty bodies", () => {
const text = "{{#bodyFunction 1 99 }}{{/bodyFunction}}" const text = "{{#bodyFunction 1 99 }}{{/bodyFunction}}"
const result = validateHbsTemplate(text, validators) const result = validateHbsTemplate(text, validators)
expect(result).toHaveLength(0) expect(result).toHaveLength(0)
}) })
it("validate too little parameters", () => { it("validates too little parameters", () => {
const text = "{{#bodyFunction 1 }}{{/bodyFunction}}" const text = "{{#bodyFunction 1 }}{{/bodyFunction}}"
const result = validateHbsTemplate(text, validators) const result = validateHbsTemplate(text, validators)
@ -180,7 +180,7 @@ describe("hbs validator", () => {
]) ])
}) })
it("validate too many parameters", () => { it("validates too many parameters", () => {
const text = "{{#bodyFunction 1 99 'a' 0 }}{{/bodyFunction}}" const text = "{{#bodyFunction 1 99 'a' 0 }}{{/bodyFunction}}"
const result = validateHbsTemplate(text, validators) const result = validateHbsTemplate(text, validators)
@ -194,7 +194,7 @@ describe("hbs validator", () => {
]) ])
}) })
it("validate non-supported body usages", () => { it("validates non-supported body usages", () => {
const text = "{{#nonBodyFunction 1 99}}{{/nonBodyFunction}}" const text = "{{#nonBodyFunction 1 99}}{{/nonBodyFunction}}"
const result = validateHbsTemplate(text, validators) const result = validateHbsTemplate(text, validators)
@ -273,4 +273,18 @@ describe("hbs validator", () => {
}) })
}) })
}) })
it("validates wrong hbs code", () => {
const text = "{{#fieldName}}{{/wrong}}"
const result = validateHbsTemplate(text, {})
expect(result).toEqual([
{
from: 0,
message: `The handlebars code is not valid:\nfieldName doesn't match wrong - 1:3`,
severity: "error",
to: text.length,
},
])
})
}) })