From 87999db65928ab8cc3586c2b619cea20be87ef7a Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 14 Aug 2024 18:12:33 +0100 Subject: [PATCH] Adding test cases. --- packages/string-templates/src/index.ts | 5 +++- .../src/processors/preprocessor.ts | 4 +--- .../string-templates/test/helpers.spec.ts | 23 +++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/packages/string-templates/src/index.ts b/packages/string-templates/src/index.ts index 291807d5f0..4a24328265 100644 --- a/packages/string-templates/src/index.ts +++ b/packages/string-templates/src/index.ts @@ -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)) } diff --git a/packages/string-templates/src/processors/preprocessor.ts b/packages/string-templates/src/processors/preprocessor.ts index ee76b5dbcf..95a4d5500d 100644 --- a/packages/string-templates/src/processors/preprocessor.ts +++ b/packages/string-templates/src/processors/preprocessor.ts @@ -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) { @@ -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})` } diff --git a/packages/string-templates/test/helpers.spec.ts b/packages/string-templates/test/helpers.spec.ts index 5f1855535d..566be2b02b 100644 --- a/packages/string-templates/test/helpers.spec.ts +++ b/packages/string-templates/test/helpers.spec.ts @@ -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"]) + }) +})