"use strict";

const terser = require('terser');

test('remove dead codes and minimize the parameter names', () => {
  const code = `
  const promise = new Promise((resolve, reject) => {
  const res = true;
  if (res) {
    resolve('Resolved!');
  }
  else {
    reject(Error('Error'));
  }
  });
  promise.then((res) => console.log(res), (err) => console.error(err));
   `;
  return terser.minify(code).then(minify => {
    expect(minify.code).toBe('const promise=new Promise(((o,e)=>{o("Resolved!")}));promise.then((o=>console.log(o)),(o=>console.error(o)));');
  });
});
test('remove dead codes and minimize the variables', () => {
  const code = `

    const msg = async function () { 
    const msg = await helloWorld();
    console.log('message',msg);
    }
    const msg1 = async () => { 
    const msg = await helloWorld();
    console.log('message',msg);
    }
    msg();
    msg1(); 
    `;
  return terser.minify(code).then(minify => {
    expect(minify.code).toBe('const msg=async function(){const s=await helloWorld();console.log("message",s)},msg1=async()=>{const s=await helloWorld();console.log("message",s)};msg(),msg1();');
  });
});
test('minimize the set timeout timer', () => {
  const code = `
    function helloWorld() {
    return new Promise(resolve => {
    setTimeout(() => {
      resolve('Hello World!');
    }, 2000);
    });
    }
    `;
  return terser.minify(code).then(minify => {
    expect(minify.code).toBe('function helloWorld(){return new Promise((e=>{setTimeout((()=>{e("Hello World!")}),2e3)}))}');
  });
});