/**
 * To prevent problems related to timezones (e.g. date formating), we can set
 * node timezone in jest config file. Now we are sure all tests are executed
 * on the same timezone no matter where our colleagues or our CI server are.
 *
 * // jest.config.js
 * process.env.TZ = 'UTC';
 *
 * module.exports = {
 *   // ...
 * }
 */

import {
  formatCurrency,
  formatDate,
  sizeToCompany,
  userNameToInitials,
  trimSpaces
} from './formatters';

import { cleanup } from '@testing-library/react';

afterEach(cleanup);

describe('Check "utils/common/formatters"', () => {
  // formatCurrency

  it(`Check formatCurrency function with undefined`, () => {
    expect(formatCurrency()).toEqual('-');
  });

  it(`Check formatCurrency function with 0`, () => {
    const number = 0;
    const result = '0';
    expect(formatCurrency(number)).toEqual(result);
  });

  it(`Check formatCurrency function with 59`, () => {
    const number = 59;
    const result = '$59';
    expect(formatCurrency(number)).toEqual(result);
  });

  it(`Check formatCurrency function with 159`, () => {
    const number = 159;
    const result = '$0.16k';
    expect(formatCurrency(number)).toEqual(result);
  });

  it(`Check formatCurrency function with 1059`, () => {
    const number = 1059;
    const result = '$1.06k';
    expect(formatCurrency(number)).toEqual(result);
  });

  it(`Check formatCurrency function with -10590`, () => {
    const number = -10590;
    const result = '$10.59k';
    expect(formatCurrency(number)).toEqual(result);
  });

  it(`Check formatCurrency function with 1059000`, () => {
    const number = 1059000;
    const result = '$1.06m';
    expect(formatCurrency(number)).toEqual(result);
  });

  it(`Check formatCurrency function with 1059000000`, () => {
    const number = 1059000000;
    const result = '$1.06b';
    expect(formatCurrency(number)).toEqual(result);
  });

  // formatDate

  it(`Check formatDate function with ISO 8601 date`, () => {
    const date = '2020-02-29T00:00:00.000Z';
    const result = 'Feb 29, 2020';
    expect(formatDate(date)).toEqual(result);
  });

  it(`Check formatDate function with ISO 8601 date`, () => {
    const date = '2021-11-21T12:00:00.000Z';
    const result = 'Nov 21, 2021';
    expect(formatDate(date)).toEqual(result);
  });

  // sizeToCompany

  it(`Check sizeToCompany function with empty value`, () => {
    const size = '';
    const result = 'NA';
    expect(sizeToCompany(size)).toEqual(result);
  });

  it(`Check sizeToCompany function with known values`, () => {
    expect(sizeToCompany('Small')).toContain('Small');
    expect(sizeToCompany('SMB')).toContain('SMB');
    expect(sizeToCompany('Medium')).toContain('Medium');
    expect(sizeToCompany('Large')).toContain('Large');
    expect(sizeToCompany('Enterprise')).toContain('Enterprise');
  });

  it(`Check sizeToCompany function with unknown value`, () => {
    const size = 'unknown';
    const result = 'unknown';
    expect(sizeToCompany(size)).toEqual(result);
  });

  it(`Check userNameToInitials with empty username`, () => {
    const userName = '';
    const result = {
      color: '',
      first: '',
      initials: '',
      second: ''
    };
    expect(userNameToInitials(userName)).toEqual(result);
    expect(userNameToInitials(userName, undefined, true)).toEqual(result);
  });

  it(`Check userNameToInitials without specialColors`, () => {
    const userName = 'TestUserName';
    expect(userNameToInitials(userName)).toEqual({
      color: '',
      first: 'T',
      initials: 'T',
      second: ''
    });
    expect(userNameToInitials(userName, undefined, true)).toEqual({
      color: '',
      first: 'T',
      initials: 'TT',
      second: 'T'
    });
  });

  it(`Check userNameToInitials with specialColors`, () => {
    const specialColors = {
      1: 'red',
      2: 'amber',
      3: 'green'
    };
    const userName = 'User Name';
    expect(userNameToInitials(userName, specialColors)).toEqual({
      color: specialColors[3],
      first: 'U',
      initials: 'UN',
      second: 'N'
    });
  });

  it(`Check trim spaces in text`, () => {
    const text = 'Hello World';
    const result = 'HelloWorld';
    expect(trimSpaces(text)).toEqual(result);
  });

  it(`Check trim spaces if undefined`, () => {
    const text = undefined;
    const result = undefined;
    expect(trimSpaces(text)).toEqual(result);
  });
});
