const babel = require('@babel/parser');
const { default: generate } = require('@babel/generator');
const replaceLiteralValuesInTemplate = require('../optimize');

test('should concatenate template if just one literal value ', () => {
  const underTest = 'html`<h1 id="og-msi-main-title" class="og-msi-main-title">${"test"}</h1>`;';
  const program = babel.parse(underTest);
  expect(generate(replaceLiteralValuesInTemplate(program)).code).toEqual(
    'html`<h1 id="og-msi-main-title" class="og-msi-main-title">test</h1>`;'
  );
});

test('should concatenate template if just two or more literal values ', () => {
  const underTest = 'html`<h1 id="og-msi-main-title" class="og-msi-main-title"> ${"test"} ${"test"} </h1>`;';

  const program = babel.parse(underTest);
  expect(generate(replaceLiteralValuesInTemplate(program)).code).toEqual(
    'html`<h1 id="og-msi-main-title" class="og-msi-main-title"> test test </h1>`;'
  );
});

test('should concatenate template and preserve non literal values', () => {
  const underTest = 'html`<h1 id="og-msi-main-title" class="og-msi-main-title"> ${notAliteral} ${"test"} </h1>`;';

  const program = babel.parse(underTest);
  expect(generate(replaceLiteralValuesInTemplate(program)).code).toEqual(
    'html`<h1 id="og-msi-main-title" class="og-msi-main-title"> ${notAliteral} test </h1>`;'
  );
});
test('should concatenate template and preserve non literal valuesc complex', () => {
  const underTest =
    'html`<h1 id="og-msi-main-title" class="og-msi-main-title"> ${notAliteral} ${"test"} ${notAliteral}</h1>`;';

  const program = babel.parse(underTest);
  expect(generate(replaceLiteralValuesInTemplate(program)).code).toEqual(
    'html`<h1 id="og-msi-main-title" class="og-msi-main-title"> ${notAliteral} test ${notAliteral}</h1>`;'
  );
});

test('should pass if template literal', () => {
  const underTest = 'html`<h1 id="og-msi-main-title" class="og-msi-main-title"> ${``} </h1>`;';

  const program = babel.parse(underTest);
  expect(generate(replaceLiteralValuesInTemplate(program)).code).toEqual(
    'html`<h1 id="og-msi-main-title" class="og-msi-main-title"> ${``} </h1>`;'
  );
});
