|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +import { renderMustacheString, renderMustacheObject } from './mustache_renderer'; |
| 8 | + |
| 9 | +const variables = { |
| 10 | + a: 1, |
| 11 | + b: '2', |
| 12 | + c: false, |
| 13 | + d: null, |
| 14 | + e: undefined, |
| 15 | + f: { |
| 16 | + g: 3, |
| 17 | + }, |
| 18 | + lt: '<', |
| 19 | + gt: '>', |
| 20 | + amp: '&', |
| 21 | + nl: '\n', |
| 22 | + dq: '"', |
| 23 | + bt: '`', |
| 24 | + bs: '\\', |
| 25 | + st: '*', |
| 26 | + ul: '_', |
| 27 | + st_lt: '*<', |
| 28 | +}; |
| 29 | + |
| 30 | +describe('mustache_renderer', () => { |
| 31 | + describe('renderMustacheString()', () => { |
| 32 | + it('handles basic templating that does not need escaping', () => { |
| 33 | + expect(renderMustacheString('', variables, 'none')).toBe(''); |
| 34 | + expect(renderMustacheString('{{a}}', variables, 'none')).toBe('1'); |
| 35 | + expect(renderMustacheString('{{b}}', variables, 'none')).toBe('2'); |
| 36 | + expect(renderMustacheString('{{c}}', variables, 'none')).toBe('false'); |
| 37 | + expect(renderMustacheString('{{d}}', variables, 'none')).toBe(''); |
| 38 | + expect(renderMustacheString('{{e}}', variables, 'none')).toBe(''); |
| 39 | + expect(renderMustacheString('{{f.g}}', variables, 'none')).toBe('3'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('handles escape:none with commonly escaped strings', () => { |
| 43 | + expect(renderMustacheString('{{lt}}', variables, 'none')).toBe(variables.lt); |
| 44 | + expect(renderMustacheString('{{gt}}', variables, 'none')).toBe(variables.gt); |
| 45 | + expect(renderMustacheString('{{amp}}', variables, 'none')).toBe(variables.amp); |
| 46 | + expect(renderMustacheString('{{nl}}', variables, 'none')).toBe(variables.nl); |
| 47 | + expect(renderMustacheString('{{dq}}', variables, 'none')).toBe(variables.dq); |
| 48 | + expect(renderMustacheString('{{bt}}', variables, 'none')).toBe(variables.bt); |
| 49 | + expect(renderMustacheString('{{bs}}', variables, 'none')).toBe(variables.bs); |
| 50 | + expect(renderMustacheString('{{st}}', variables, 'none')).toBe(variables.st); |
| 51 | + expect(renderMustacheString('{{ul}}', variables, 'none')).toBe(variables.ul); |
| 52 | + }); |
| 53 | + |
| 54 | + it('handles escape:markdown with commonly escaped strings', () => { |
| 55 | + expect(renderMustacheString('{{lt}}', variables, 'markdown')).toBe(variables.lt); |
| 56 | + expect(renderMustacheString('{{gt}}', variables, 'markdown')).toBe(variables.gt); |
| 57 | + expect(renderMustacheString('{{amp}}', variables, 'markdown')).toBe(variables.amp); |
| 58 | + expect(renderMustacheString('{{nl}}', variables, 'markdown')).toBe(variables.nl); |
| 59 | + expect(renderMustacheString('{{dq}}', variables, 'markdown')).toBe(variables.dq); |
| 60 | + expect(renderMustacheString('{{bt}}', variables, 'markdown')).toBe('\\' + variables.bt); |
| 61 | + expect(renderMustacheString('{{bs}}', variables, 'markdown')).toBe('\\' + variables.bs); |
| 62 | + expect(renderMustacheString('{{st}}', variables, 'markdown')).toBe('\\' + variables.st); |
| 63 | + expect(renderMustacheString('{{ul}}', variables, 'markdown')).toBe('\\' + variables.ul); |
| 64 | + }); |
| 65 | + |
| 66 | + it('handles triple escapes', () => { |
| 67 | + expect(renderMustacheString('{{{bt}}}', variables, 'markdown')).toBe(variables.bt); |
| 68 | + expect(renderMustacheString('{{{bs}}}', variables, 'markdown')).toBe(variables.bs); |
| 69 | + expect(renderMustacheString('{{{st}}}', variables, 'markdown')).toBe(variables.st); |
| 70 | + expect(renderMustacheString('{{{ul}}}', variables, 'markdown')).toBe(variables.ul); |
| 71 | + }); |
| 72 | + |
| 73 | + it('handles escape:slack with commonly escaped strings', () => { |
| 74 | + expect(renderMustacheString('{{lt}}', variables, 'slack')).toBe('<'); |
| 75 | + expect(renderMustacheString('{{gt}}', variables, 'slack')).toBe('>'); |
| 76 | + expect(renderMustacheString('{{amp}}', variables, 'slack')).toBe('&'); |
| 77 | + expect(renderMustacheString('{{nl}}', variables, 'slack')).toBe(variables.nl); |
| 78 | + expect(renderMustacheString('{{dq}}', variables, 'slack')).toBe(variables.dq); |
| 79 | + expect(renderMustacheString('{{bt}}', variables, 'slack')).toBe(`'`); |
| 80 | + expect(renderMustacheString('{{bs}}', variables, 'slack')).toBe(variables.bs); |
| 81 | + expect(renderMustacheString('{{st}}', variables, 'slack')).toBe('`*`'); |
| 82 | + expect(renderMustacheString('{{ul}}', variables, 'slack')).toBe('`_`'); |
| 83 | + // html escapes not needed when using backtic escaping |
| 84 | + expect(renderMustacheString('{{st_lt}}', variables, 'slack')).toBe('`*<`'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('handles escape:json with commonly escaped strings', () => { |
| 88 | + expect(renderMustacheString('{{lt}}', variables, 'json')).toBe(variables.lt); |
| 89 | + expect(renderMustacheString('{{gt}}', variables, 'json')).toBe(variables.gt); |
| 90 | + expect(renderMustacheString('{{amp}}', variables, 'json')).toBe(variables.amp); |
| 91 | + expect(renderMustacheString('{{nl}}', variables, 'json')).toBe('\\n'); |
| 92 | + expect(renderMustacheString('{{dq}}', variables, 'json')).toBe('\\"'); |
| 93 | + expect(renderMustacheString('{{bt}}', variables, 'json')).toBe(variables.bt); |
| 94 | + expect(renderMustacheString('{{bs}}', variables, 'json')).toBe('\\\\'); |
| 95 | + expect(renderMustacheString('{{st}}', variables, 'json')).toBe(variables.st); |
| 96 | + expect(renderMustacheString('{{ul}}', variables, 'json')).toBe(variables.ul); |
| 97 | + }); |
| 98 | + |
| 99 | + it('handles errors', () => { |
| 100 | + expect(renderMustacheString('{{a}', variables, 'none')).toMatchInlineSnapshot( |
| 101 | + `"error rendering mustache template \\"{{a}\\": Unclosed tag at 4"` |
| 102 | + ); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + const object = { |
| 107 | + literal: 0, |
| 108 | + literals: { |
| 109 | + a: 1, |
| 110 | + b: '2', |
| 111 | + c: true, |
| 112 | + d: null, |
| 113 | + e: undefined, |
| 114 | + eval: '{{lt}}{{b}}{{gt}}', |
| 115 | + }, |
| 116 | + list: ['{{a}}', '{{bt}}{{st}}{{bt}}'], |
| 117 | + object: { |
| 118 | + a: ['{{a}}', '{{bt}}{{st}}{{bt}}'], |
| 119 | + }, |
| 120 | + }; |
| 121 | + |
| 122 | + describe('renderMustacheObject()', () => { |
| 123 | + it('handles deep objects', () => { |
| 124 | + expect(renderMustacheObject(object, variables)).toMatchInlineSnapshot(` |
| 125 | + Object { |
| 126 | + "list": Array [ |
| 127 | + "1", |
| 128 | + "\`*\`", |
| 129 | + ], |
| 130 | + "literal": 0, |
| 131 | + "literals": Object { |
| 132 | + "a": 1, |
| 133 | + "b": "2", |
| 134 | + "c": true, |
| 135 | + "d": null, |
| 136 | + "e": undefined, |
| 137 | + "eval": "<2>", |
| 138 | + }, |
| 139 | + "object": Object { |
| 140 | + "a": Array [ |
| 141 | + "1", |
| 142 | + "\`*\`", |
| 143 | + ], |
| 144 | + }, |
| 145 | + } |
| 146 | + `); |
| 147 | + }); |
| 148 | + |
| 149 | + it('handles primitive objects', () => { |
| 150 | + expect(renderMustacheObject(undefined, variables)).toMatchInlineSnapshot(`undefined`); |
| 151 | + expect(renderMustacheObject(null, variables)).toMatchInlineSnapshot(`null`); |
| 152 | + expect(renderMustacheObject(0, variables)).toMatchInlineSnapshot(`0`); |
| 153 | + expect(renderMustacheObject(true, variables)).toMatchInlineSnapshot(`true`); |
| 154 | + expect(renderMustacheObject('{{a}}', variables)).toMatchInlineSnapshot(`"1"`); |
| 155 | + expect(renderMustacheObject(['{{a}}'], variables)).toMatchInlineSnapshot(` |
| 156 | + Array [ |
| 157 | + "1", |
| 158 | + ] |
| 159 | + `); |
| 160 | + }); |
| 161 | + |
| 162 | + it('handles errors', () => { |
| 163 | + expect(renderMustacheObject({ a: '{{a}' }, variables)).toMatchInlineSnapshot(` |
| 164 | + Object { |
| 165 | + "a": "error rendering mustache template \\"{{a}\\": Unclosed tag at 4", |
| 166 | + } |
| 167 | + `); |
| 168 | + }); |
| 169 | + }); |
| 170 | + |
| 171 | + describe('augmented object variables', () => { |
| 172 | + const deepVariables = { |
| 173 | + a: 1, |
| 174 | + b: { c: 2, d: [3, 4] }, |
| 175 | + e: [5, { f: 6, g: 7 }], |
| 176 | + }; |
| 177 | + expect(renderMustacheObject({ x: '{{a}} - {{b}} -- {{e}} ' }, deepVariables)) |
| 178 | + .toMatchInlineSnapshot(` |
| 179 | + Object { |
| 180 | + "x": "1 - {\\"c\\":2,\\"d\\":[3,4]} -- 5,{\\"f\\":6,\\"g\\":7} ", |
| 181 | + } |
| 182 | + `); |
| 183 | + |
| 184 | + const expected = '1 - {"c":2,"d":[3,4]} -- 5,{"f":6,"g":7}'; |
| 185 | + expect(renderMustacheString('{{a}} - {{b}} -- {{e}}', deepVariables, 'none')).toEqual(expected); |
| 186 | + }); |
| 187 | +}); |
0 commit comments