"use strict";

/* eslint-disable max-lines */
const terser = require('terser');

describe('if condition', () => {
  it('remove unused if condition with true value', () => {
    const code = `

          if (true) {
             console.log('Mail sent to recipient');
          }
        `;
    return terser.minify(code).then(minify => {
      expect(minify.code).toBe('console.log("Mail sent to recipient");');
    });
  });
  it('remove unused if condition with false value', () => {
    const code = `
          if (false) {
             console.log('Mail sent to recipient');
          }
        `;
    return terser.minify(code).then(minify => {
      expect(minify.code).toBe('0;');
    });
  });
  it('remove unused if statements when using a let or var type variable', () => {
    const code = `
          let isMailSent = true;

          if (isMailSent) {
             console.log('Mail sent to recipient');
          }
        `;
    return terser.minify(code).then(minify => {
      expect(minify.code).toBe('let isMailSent=!0;isMailSent&&console.log("Mail sent to recipient");');
    });
  });
  it('remove unused if statements using a const variable', () => {
    const code = `
          const isMailSent = true;

          if (isMailSent) {
             console.log('Mail sent to recipient');
          }
        `;
    return terser.minify(code).then(minify => {
      expect(minify.code).toBe('const isMailSent=!0;console.log("Mail sent to recipient");');
    });
  });
  it('if and else statement', () => {
    const code = `
          let isMailSent = true;

          if (isMailSent) {
             console.log('Mail sent to recipient');
          }else{
            console.log(3)
          }
          `;
    return terser.minify(code).then(minify => {
      expect(minify.code).toBe('let isMailSent=!0;isMailSent?console.log("Mail sent to recipient"):console.log(3);');
    });
  });
  it('if and else statement returns same value', () => {
    const code = `
          let isMailSent = true;

          if (isMailSent) {
             console.log('Mail sent to recipient');
          }else{
            console.log('Mail sent to recipient');
          }
          `;
    return terser.minify(code).then(minify => {
      expect(minify.code).toBe('let isMailSent=!0;console.log("Mail sent to recipient");');
    });
  });
  it('using else if statement return same value', () => {
    const code = `
          let isMailSent = true;

          if (isMailSent) {
             console.log('Mail sent to recipient');
          }else if(!isMailSent){
            console.log('Mail sent to recipient');
          }
          `;
    return terser.minify(code).then(minify => {
      expect(minify.code).toBe('let isMailSent=!0;isMailSent?console.log("Mail sent to recipient"):isMailSent||console.log("Mail sent to recipient");');
    });
  });
  it('using else if statement return different value', () => {
    const code = `
          let isMailSent = true;
          if (isMailSent) {
             console.log('Mail sent to recipient');
          }else if(isMailSent == 1){
            console.log('Mail not sent to recipient');
          }
          `;
    return terser.minify(code).then(minify => {
      expect(minify.code).toBe('let isMailSent=!0;isMailSent?console.log("Mail sent to recipient"):1==isMailSent&&console.log("Mail not sent to recipient");');
    });
  });
  it('skips the false content', () => {
    const code = `
        const size = 4;
        if (size > 100) {
          console.log('Big');
        } else if (size > 20) {
          console.log('Medium');
        } else if (size > 4) {
          console.log('Small');
        } else {
          console.log('Tiny');
        }
    `;
    return terser.minify(code).then(minify => {
      expect(minify.code).toBe('const size=4;console.log("Tiny");');
    });
  });
});
test('nullish operator', () => {
  const code = `
     const a = undefined;
     console.log(undefined??"hi")
    `;
  return terser.minify(code).then(minify => {
    expect(minify.code).toBe('const a=void 0;console.log("hi");');
  });
});
describe('switch Case', () => {
  it('first value matched but runs for all', () => {
    const code = `
        const food = 'oyster';

        switch (food) {
         case 'oyster':
             console.log('The taste of the sea');
             break;
         case 'pizza':
             console.log('A delicious pie');
             break;
         default:
             console.log('Enjoy your meal');
}
        `;
    return terser.minify(code).then(minify => {
      expect(minify.code).toBe('const food="oyster";switch(food){case"oyster":console.log("The taste of the sea");break;case"pizza":console.log("A delicious pie");break;default:console.log("Enjoy your meal")}');
    });
  });
});