Improve tests
This commit is contained in:
parent
c5f2ef9354
commit
fba5663e66
|
@ -26,6 +26,29 @@ describe("js validator", () => {
|
|||
expect(result).toEqual([])
|
||||
})
|
||||
|
||||
it("allows return not being on the last line", () => {
|
||||
const text = "const foo='bar'\nreturn 123\nconsole.log(foo)"
|
||||
const validators = {}
|
||||
|
||||
const result = validateJsTemplate(text, validators)
|
||||
expect(result).toEqual([])
|
||||
})
|
||||
|
||||
it("throws on missing return", () => {
|
||||
const text = "const foo='bar'\nbar='foo'"
|
||||
const validators = {}
|
||||
|
||||
const result = validateJsTemplate(text, validators)
|
||||
expect(result).toEqual([
|
||||
{
|
||||
from: 0,
|
||||
message: "Your code must return a value.",
|
||||
severity: "error",
|
||||
to: 25,
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
describe("helpers", () => {
|
||||
const validators: CodeValidator = {
|
||||
helperFunction: {
|
||||
|
@ -37,7 +60,7 @@ describe("js validator", () => {
|
|||
const text = "return helpers.helperFunction(1, 99, 'a')"
|
||||
|
||||
const result = validateJsTemplate(text, validators)
|
||||
expect(result).toHaveLength(0)
|
||||
expect(result).toEqual([])
|
||||
})
|
||||
|
||||
it("throws on too few params", () => {
|
||||
|
@ -67,5 +90,49 @@ describe("js validator", () => {
|
|||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("validates helpers on inner functions", () => {
|
||||
const text = `function call(){
|
||||
return helpers.helperFunction(1, 99)
|
||||
}
|
||||
return call()`
|
||||
|
||||
const result = validateJsTemplate(text, validators)
|
||||
expect(result).toEqual([
|
||||
{
|
||||
from: 46,
|
||||
message: `Function "helperFunction" expects 3 parameters (a, b, c), but got 2.`,
|
||||
severity: "error",
|
||||
to: 75,
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("validates multiple helpers", () => {
|
||||
const text =
|
||||
"return helpers.helperFunction(1, 99, 'a') + helpers.helperFunction(1) + helpers.another(1) + helpers.another()"
|
||||
const validators: CodeValidator = {
|
||||
helperFunction: {
|
||||
arguments: ["a", "b", "c"],
|
||||
},
|
||||
another: { arguments: [] },
|
||||
}
|
||||
|
||||
const result = validateJsTemplate(text, validators)
|
||||
expect(result).toEqual([
|
||||
{
|
||||
from: 44,
|
||||
message: `Function "helperFunction" expects 3 parameters (a, b, c), but got 1.`,
|
||||
severity: "error",
|
||||
to: 69,
|
||||
},
|
||||
{
|
||||
from: 72,
|
||||
message: `Function "another" expects 0 parameters (), but got 1.`,
|
||||
severity: "error",
|
||||
to: 90,
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue