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`)).toThrowError(
    
  28.       /Unsupported type/
    
  29.     );
    
  30.     expect(() => parseAndEval(`'foo ' + 3`)).toThrowError(/Unsupported type/);
    
  31.     expect(() => parseAndEval(`'foo ' + null`)).toThrowError(
    
  32.       /Unsupported type/
    
  33.     );
    
  34.     expect(() => parseAndEval(`'foo ' + undefined`)).toThrowError(
    
  35.       /Unsupported type/
    
  36.     );
    
  37.   });
    
  38. });