"use strict";

const terser = require('terser');

test('arrow function reducing the arguments name', () => {
  const code = `
     const sum = (param1, param2) => {
        return param1 + param2;
     };
     console.log(sum(2, 5));
    `;
  return terser.minify(code).then(minify => {
    expect(minify.code).toBe('const sum=(o,s)=>o+s;console.log(sum(2,5));');
  });
});
test('arrow function with console', () => {
  const code = `
    const printHello = () => {
        console.log('hi')
       return ('hello');
    };
    printHello();
    `;
  return terser.minify(code).then(minify => {
    expect(minify.code).toBe('const printHello=()=>(console.log("hi"),"hello");console.log("hi");');
  });
});
test('remove unused or unwanted codes inside function', () => {
  const code = `
    function a(){
       let x = 3;
       let a = 1;
       //this is a function
       let result = (a == 1) ? true : false
       result = (x == 1) ? true : false;
    }
    `;
  return terser.minify(code).then(minify => {
    expect(minify.code).toBe('function a(){let n=!0;n=!1}');
  });
});
describe('function', () => {
  it('Minify a simple function', () => {
    const code = `
      function sum(num1, num2) {
        return num1 + num2;
      }
    `;
    return terser.minify(code).then(minify => {
      expect(minify.code).toBe('function sum(n,u){return n+u}');
    });
  });
  it('Minify a function with  a variable declaration', () => {
    const code = `
      function sum(num1, num2) {
        var result = num1 + num2;
        return result;
      }
    `;
    return terser.minify(code).then(minify => {
      expect(minify.code).toBe('function sum(n,u){return n+u}');
    });
  });
  it('Minify a function with a if condition', () => {
    const code = `
       function sum(num1, num2) {
        if (num1 > num2) {
          return num1;
        } else {
          return num2;
        }
      }
    `;
    return terser.minify(code).then(minify => {
      expect(minify.code).toBe('function sum(n,u){return n>u?n:u}');
    });
  });
  it('Minify a function with return statement', () => {
    const code = `
      function sum(num1, num2) {
        var result = num1 + num2;
        return result;
      }
    `;
    return terser.minify(code).then(minify => {
      expect(minify.code).toBe('function sum(n,u){return n+u}');
    });
  });
});