| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | 'use strict'; |
| 8 | |
| 9 | const {evalStringConcat} = require('../evalToString'); |
| 10 | const parser = require('@babel/parser'); |
| 11 | |
| 12 | const parse = source => parser.parse(`(${source});`).program.body[0].expression; // quick way to get an exp node |
| 13 | |
| 14 | const parseAndEval = source => evalStringConcat(parse(source)); |
| 15 | |
| 16 | describe('evalToString', () => { |
| 17 | it('should support StringLiteral', () => { |
| 18 | expect(parseAndEval(`'foobar'`)).toBe('foobar'); |
| 19 | expect(parseAndEval(`'yowassup'`)).toBe('yowassup'); |
| 20 | }); |
| 21 | |
| 22 | it('should support string concat (`+`)', () => { |
| 23 | expect(parseAndEval(`'foo ' + 'bar'`)).toBe('foo bar'); |
| 24 | }); |
| 25 | |
| 26 | it('should throw when it finds other types', () => { |
| 27 | expect(() => parseAndEval(`'foo ' + true`)).toThrow(/Unsupported type/); |
| 28 | expect(() => parseAndEval(`'foo ' + 3`)).toThrow(/Unsupported type/); |
| 29 | expect(() => parseAndEval(`'foo ' + null`)).toThrow(/Unsupported type/); |
| 30 | expect(() => parseAndEval(`'foo ' + undefined`)).toThrow( |
| 31 | /Unsupported type/ |
| 32 | ); |
| 33 | }); |
| 34 | }); |