{"size": 7083, "ext": "js", "max_stars_repo_path": "src/__tests__/PositiveInt.test.js", "max_stars_repo_name": "LeapLabs/graphql-scalars", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/__tests__/PositiveInt.test.js", "max_issues_repo_name": "LeapLabs/graphql-scalars", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/__tests__/PositiveInt.test.js", "max_forks_repo_name": "LeapLabs/graphql-scalars", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.66796875, "max_line_length": 99, "alphanum_fraction": 0.473528166}
/* global describe, test, expect */

import { Kind } from 'graphql/language';

import { PositiveInt } from '..';

describe('PositiveInt', () => {
  describe('valid', () => {
    describe('as int', () => {
      test('serialize', () => {
        expect(PositiveInt.serialize(123)).toBe(123);
      });

      test('parseValue', () => {
        expect(PositiveInt.parseValue(123)).toBe(123);
      });

      test('parseLiteral', () => {
        expect(PositiveInt.parseLiteral({ value: 123, kind: Kind.INT })).toBe(
          123,
        );
      });
    });

    describe('as string', () => {
      test('serialize', () => {
        expect(PositiveInt.serialize('123')).toBe(123);
      });

      test('parseValue', () => {
        expect(PositiveInt.parseValue('123')).toBe(123);
      });

      test('parseLiteral', () => {
        expect(PositiveInt.parseLiteral({ value: '123', kind: Kind.INT })).toBe(
          123,
        );
      });
    });
  });

  describe('invalid', () => {
    describe('null', () => {
      test('serialize', () => {
        expect(() => PositiveInt.serialize(null)).toThrow(
          /Value is not a number: null/,
        );
      });

      test('parseValue', () => {
        expect(() => PositiveInt.parseValue(null)).toThrow(
          /Value is not a number/,
        );
      });

      test('parseLiteral', () => {
        expect(() =>
          PositiveInt.parseLiteral({ value: null, kind: Kind.INT }),
        ).toThrow(/Value is not a number: null/);
      });
    });

    describe('undefined', () => {
      test('serialize', () => {
        expect(() => PositiveInt.serialize(undefined)).toThrow(
          /Value is not a number: undefined/,
        );
      });

      // FIXME: Does nothing. No throw. Call doesn't even seem to get to the parseValue() function.
      // test('parseValue', () => {
      //   expect(() => PositiveInt.parseValue(undefined)).toThrow(
      //     /Value is not a number/,
      //   );
      // });

      test('parseLiteral', () => {
        expect(() =>
          PositiveInt.parseLiteral({ value: undefined, kind: Kind.INT }),
        ).toThrow(/Value is not a number: undefined/);
      });
    });

    describe('unsafe integer', () => {
      test('serialize', () => {
        expect(() => PositiveInt.serialize(2 ** 53)).toThrow(
          /Value is not a safe integer/,
        );
      });

      test('parseValue', () => {
        expect(() => PositiveInt.parseValue(2 ** 53)).toThrow(
          /Value is not a safe integer/,
        );
      });

      test('parseLiteral', () => {
        expect(() =>
          PositiveInt.parseLiteral({ value: 2 ** 53, kind: Kind.INT }),
        ).toThrow(/Value is not a safe integer/);
      });
    });

    describe('zero', () => {
      describe('as int', () => {
        test('serialize', () => {
          expect(() => PositiveInt.serialize(0)).toThrow(
            /Value is not a positive number/,
          );
        });

        test('parseValue', () => {
          expect(() => PositiveInt.parseValue(0)).toThrow(
            /Value is not a positive number/,
          );
        });

        test('parseLiteral', () => {
          expect(() =>
            PositiveInt.parseLiteral({ value: 0, kind: Kind.INT }),
          ).toThrow(/Value is not a positive number/);
        });
      });

      describe('as string', () => {
        test('serialize', () => {
          expect(() => PositiveInt.serialize('0')).toThrow(
            /Value is not a positive number/,
          );
        });

        test('parseValue', () => {
          expect(() => PositiveInt.parseValue('0')).toThrow(
            /Value is not a positive number/,
          );
        });

        test('parseLiteral', () => {
          expect(() =>
            PositiveInt.parseLiteral({ value: '0', kind: Kind.INT }),
          ).toThrow(/Value is not a positive number/);
        });
      });
    });

    describe('less than zero', () => {
      describe('as int', () => {
        test('serialize', () => {
          expect(() => PositiveInt.serialize(-1)).toThrow(
            /Value is not a positive number/,
          );
        });

        test('parseValue', () => {
          expect(() => PositiveInt.parseValue(-1)).toThrow(
            /Value is not a positive number/,
          );
        });

        test('parseLiteral', () => {
          expect(() =>
            PositiveInt.parseLiteral({ value: -1, kind: Kind.INT }),
          ).toThrow(/Value is not a positive number/);
        });
      });

      describe('as string', () => {
        test('serialize', () => {
          expect(() => PositiveInt.serialize('-1')).toThrow(
            /Value is not a positive number/,
          );
        });

        test('parseValue', () => {
          expect(() => PositiveInt.parseValue('-1')).toThrow(
            /Value is not a positive number/,
          );
        });

        test('parseLiteral', () => {
          expect(() =>
            PositiveInt.parseLiteral({ value: '-1', kind: Kind.INT }),
          ).toThrow(/Value is not a positive number/);
        });
      });
    });

    describe('infinity', () => {
      test('serialize', () => {
        expect(() => PositiveInt.serialize(Number.POSITIVE_INFINITY)).toThrow(
          /Value is not a finite number/,
        );
      });

      test('parseValue', () => {
        expect(() => PositiveInt.parseValue(Number.POSITIVE_INFINITY)).toThrow(
          /Value is not a finite number/,
        );
      });

      test('parseLiteral', () => {
        expect(() =>
          PositiveInt.parseLiteral({
            value: Number.POSITIVE_INFINITY,
            kind: Kind.INT,
          }),
        ).toThrow(/Value is not a finite number/);
      });
    });

    describe('not a number', () => {
      test('serialize', () => {
        expect(() => PositiveInt.serialize('not a number')).toThrow(
          /Value is not a number/,
        );
      });

      test('parseValue', () => {
        expect(() => PositiveInt.parseValue('not a number')).toThrow(
          /Value is not a number/,
        );
      });

      test('parseLiteral', () => {
        expect(() =>
          PositiveInt.parseLiteral({
            value: 'not a number',
            kind: Kind.STRING,
          }),
        ).toThrow(/Can only validate integers as positive integers but got a/);
      });
    });

    describe('NaN', () => {
      test('serialize', () => {
        expect(() => PositiveInt.serialize(Number.NaN)).toThrow(
          /Value is not a number/,
        );
      });

      // FIXME: Does nothing. No throw. Call doesn't even seem to get to the parseValue() function.
      // test('parseValue', () => {
      //   expect(() => PositiveInt.parseValue(Number.NaN)).toThrow(
      //     /Value is not a number/,
      //   );
      // });

      test('parseLiteral', () => {
        expect(() =>
          PositiveInt.parseLiteral({ value: Number.NaN, kind: Kind.STRING }),
        ).toThrow(/Can only validate integers as positive integers but got a/);
      });
    });
  });
});
