summaryrefslogtreecommitdiff
path: root/dev/lib/handlebars/src/spec/index.compiler.test.ts
blob: ef5c55f218e6df919e6a91f7e08603962ace3f14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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)
        );
      });
    });
  });
});