import { bigintTransformer } from '.';

describe('bigintTransformer', () => {
  describe('to', () => {
    it('number型を整数を表す文字列に変換できる', () => {
      expect(bigintTransformer.to(0)).toBe('0');
      expect(bigintTransformer.to(1)).toBe('1');
      expect(bigintTransformer.to(1234567890)).toBe('1234567890');
      expect(bigintTransformer.to(9007199254740991)).toBe('9007199254740991');
      expect(bigintTransformer.to(-1)).toBe('-1');
    });
    it('少数点以下がある場合はエラーとなる', () => {
      expect(() => bigintTransformer.to(1.1)).toThrowError(
        '1.1 is not integer.',
      );
    });
    it('Number.MAX_SAFE_INTEGERを超える値を変換しようとするとエラーになる', () => {
      expect(() => bigintTransformer.to(9007199254740992)).toThrowError(
        'value is greater than 9007199254740991.',
      );
      expect(() => bigintTransformer.to(9223372036854775807)).toThrowError(
        'value is greater than 9007199254740991.',
      );
    });
  });
  describe('from', () => {
    it('bigint型の文字列をnumber型に変換できる', () => {
      expect(bigintTransformer.from('0')).toBe(0);
      expect(bigintTransformer.from('1')).toBe(1);
      expect(bigintTransformer.from('1234567890')).toBe(1234567890);
      expect(bigintTransformer.from('-1')).toBe(-1);
    });
    it('Number.MAX_SAFE_INTEGERを超える値を変換しようとするとエラーになる', () => {
      expect(() => bigintTransformer.from('9007199254740992')).toThrowError(
        '9007199254740992 is greater than 9007199254740991.',
      );
      expect(() => bigintTransformer.from('9223372036854775807')).toThrowError(
        '9223372036854775807 is greater than 9007199254740991.',
      );
    });
    it('number型の場合はそのまま返す', () => {
      expect(bigintTransformer.from(0)).toBe(0);
      expect(bigintTransformer.from(1)).toBe(1);
      expect(bigintTransformer.from(1234567890)).toBe(1234567890);
      expect(bigintTransformer.from(-1)).toBe(-1);
    });
    it('nullの場合はそのまま返す', () => {
      expect(bigintTransformer.from(null)).toBe(null);
    });
    it('number型に変換できない場合はエラーとなる', () => {
      expect(() => bigintTransformer.from('a')).toThrowError('a is not int.');
      expect(() => bigintTransformer.from('')).toThrowError(' is not int.');
      expect(() => bigintTransformer.from(undefined)).toThrowError(
        'undefined is not string.',
      );
      expect(() => bigintTransformer.from({})).toThrowError(
        '[object Object] is not string.',
      );
    });
  });
});
