"use strict";

const terser = require('terser');

describe('function', () => {
  it('Minify a function with console.log', () => {
    const code = `
      function sum(num1, num2) {
        var result = num1 + num2;
        console.log(result)
      }
    `;
    return terser.minify(code).then(minify => {
      expect(minify.code).toBe('function sum(o,n){var c=o+n;console.log(c)}');
    });
  });
  it('Minify a function with return and console.log', () => {
    const code = `
      function sum(num1, num2) {
        var result = num1 + num2;
        console.log(result);
        return result;
      }
    `;
    return terser.minify(code).then(minify => {
      expect(minify.code).toBe('function sum(n,o){var r=n+o;return console.log(r),r}');
    });
  });
  it('remove unused (dead codes) and unwanted variables inside the function', () => {
    const code = `
      function sum(num1, num2) {
        var result = num1 + num2;
        const functName = 'addition';
        var i = 0;
        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 for loop', () => {
    const code = `
      function sum(num1, num2) {
        for (var i = 0; i < num1; i++) {
            console.log(i)
        }
      }
    `;
    return terser.minify(code).then(minify => {
      expect(minify.code).toBe('function sum(o,n){for(var c=0;c<o;c++)console.log(c)}');
    });
  });
  it('Minify a function with a if condition inside a for loop', () => {
    const code = `
      function sum(num1, num2) {
        for (var i = 0; i < num1; i++) {
          if(i > 0){
            console.log(i)
          }
          else{
            console.log(0);
          }
        }
      }
    `;
    return terser.minify(code).then(minify => {
      expect(minify.code).toBe('function sum(o,l){for(var n=0;n<o;n++)n>0?console.log(n):console.log(0)}');
    });
  });
});