aboutsummaryrefslogtreecommitdiff
path: root/dev/lib/handlebars/src/spec/index.compiler.test.ts
diff options
context:
space:
mode:
authorDarius Jahandarie <djahandarie@gmail.com>2023-11-04 18:45:57 +0900
committerDarius Jahandarie <djahandarie@gmail.com>2023-11-04 18:45:57 +0900
commitef79eab44bfd000792c610b968b5ceefd41e76a0 (patch)
tree48b04f30f6248caedbd880801aa49402a9e8066a /dev/lib/handlebars/src/spec/index.compiler.test.ts
parent376151096431d4362e4baaacf0cef4a534e169f7 (diff)
Modernize codebase
- Use ES modules - Remove vendored libs and build them from npm using esbuild - Switch from JSZip to zip.js
Diffstat (limited to 'dev/lib/handlebars/src/spec/index.compiler.test.ts')
-rw-r--r--dev/lib/handlebars/src/spec/index.compiler.test.ts86
1 files changed, 86 insertions, 0 deletions
diff --git a/dev/lib/handlebars/src/spec/index.compiler.test.ts b/dev/lib/handlebars/src/spec/index.compiler.test.ts
new file mode 100644
index 00000000..ef5c55f2
--- /dev/null
+++ b/dev/lib/handlebars/src/spec/index.compiler.test.ts
@@ -0,0 +1,86 @@
+/*
+ * This file is forked from the handlebars project (https://github.com/handlebars-lang/handlebars.js),
+ * and may include modifications made by Elasticsearch B.V.
+ * Elasticsearch B.V. licenses this file to you under the MIT License.
+ * See `packages/kbn-handlebars/LICENSE` for more information.
+ */
+
+import Handlebars from '../..';
+import { forEachCompileFunctionName } from '../__jest__/test_bench';
+
+describe('compiler', () => {
+ forEachCompileFunctionName((compileName) => {
+ const compile = Handlebars[compileName].bind(Handlebars);
+
+ describe(`#${compileName}`, () => {
+ it('should fail with invalid input', () => {
+ expect(function () {
+ compile(null);
+ }).toThrow(
+ `You must pass a string or Handlebars AST to Handlebars.${compileName}. You passed null`
+ );
+
+ expect(function () {
+ compile({});
+ }).toThrow(
+ `You must pass a string or Handlebars AST to Handlebars.${compileName}. You passed [object Object]`
+ );
+ });
+
+ it('should include the location in the error (row and column)', () => {
+ try {
+ compile(' \n {{#if}}\n{{/def}}')();
+ expect(true).toEqual(false);
+ } catch (err) {
+ expect(err.message).toEqual("if doesn't match def - 2:5");
+ if (Object.getOwnPropertyDescriptor(err, 'column')!.writable) {
+ // In Safari 8, the column-property is read-only. This means that even if it is set with defineProperty,
+ // its value won't change (https://github.com/jquery/esprima/issues/1290#issuecomment-132455482)
+ // Since this was neither working in Handlebars 3 nor in 4.0.5, we only check the column for other browsers.
+ expect(err.column).toEqual(5);
+ }
+ expect(err.lineNumber).toEqual(2);
+ }
+ });
+
+ it('should include the location as enumerable property', () => {
+ try {
+ compile(' \n {{#if}}\n{{/def}}')();
+ expect(true).toEqual(false);
+ } catch (err) {
+ expect(Object.prototype.propertyIsEnumerable.call(err, 'column')).toEqual(true);
+ }
+ });
+
+ it('can utilize AST instance', () => {
+ expect(
+ compile({
+ type: 'Program',
+ body: [{ type: 'ContentStatement', value: 'Hello' }],
+ })()
+ ).toEqual('Hello');
+ });
+
+ it('can pass through an empty string', () => {
+ expect(compile('')()).toEqual('');
+ });
+
+ it('should not modify the options.data property(GH-1327)', () => {
+ // The `data` property is supposed to be a boolean, but in this test we want to ignore that
+ const options = { data: [{ a: 'foo' }, { a: 'bar' }] as unknown as boolean };
+ compile('{{#each data}}{{@index}}:{{a}} {{/each}}', options)();
+ expect(JSON.stringify(options, null, 2)).toEqual(
+ JSON.stringify({ data: [{ a: 'foo' }, { a: 'bar' }] }, null, 2)
+ );
+ });
+
+ it('should not modify the options.knownHelpers property(GH-1327)', () => {
+ const options = { knownHelpers: {} };
+ compile('{{#each data}}{{@index}}:{{a}} {{/each}}', options)();
+ expect(JSON.stringify(options, null, 2)).toEqual(
+ JSON.stringify({ knownHelpers: {} }, null, 2)
+ );
+ });
+ });
+ });
+});