Skip to content

Commit

Permalink
Adding test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
mike12345567 committed Aug 14, 2024
1 parent 50d6e83 commit 87999db
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
5 changes: 4 additions & 1 deletion packages/string-templates/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ function testObject(object: any) {
}
}

function findOverlappingHelpers(context: object) {
function findOverlappingHelpers(context?: object) {
if (!context) {
return []
}
const contextKeys = Object.keys(context)
return contextKeys.filter(key => helperNames.includes(key))
}
Expand Down
4 changes: 1 addition & 3 deletions packages/string-templates/src/processors/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ export const PreprocessorNames = {
class Preprocessor {
name: string
private fn: any
private helperNames: string[]

constructor(name: string, fn: any) {
this.name = name
this.fn = fn
this.helperNames = HelperNames()
}

process(fullString: string, statement: string, opts: Object) {
Expand Down Expand Up @@ -81,7 +79,7 @@ export const processors = [
if (
!noHelpers &&
!opts.disabledHelpers?.includes(testHelper) &&
this.helperNames.some(option => testHelper === option.toLowerCase())
HelperNames().some(option => testHelper === option.toLowerCase())
) {
insideStatement = `(${insideStatement})`
}
Expand Down
23 changes: 23 additions & 0 deletions packages/string-templates/test/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,26 @@ describe("uuid", () => {
expect(output).toMatch(UUID_REGEX)
})
})

describe("helper overlap", () => {
it("should use context over helpers (regex test helper)", async () => {
const output = await processString("{{ test }}", { test: "a" })
expect(output).toEqual("a")
})

it("should use helper if no sum in context, return the context value otherwise", async () => {
const hbs = "{{ sum 1 2 }}"
const output = await processString(hbs, {})
expect(output).toEqual("3")
const secondaryOutput = await processString(hbs, { sum: "a" })
expect(secondaryOutput).toEqual("a")
})

it("should handle multiple cases", async () => {
const output = await processString("{{ literal (split test sum) }}", {
test: "a-b",
sum: "-",
})
expect(output).toEqual(["a", "b"])
})
})

0 comments on commit 87999db

Please sign in to comment.