import { toPositiveNumberWithDefault } from "..";

describe("toNumber", () => {
  const DEFAULT = 5;

  it("return number if input is positive number", () => {
    const inputs = [0, 1, 2, 100, 200.12, Infinity];
    expect.assertions(inputs.length);

    inputs.forEach((input) => {
      const output = toPositiveNumberWithDefault(DEFAULT, input);
      expect(output).toBe(input);
    });
  });

  it("return number if input is positive number as string", () => {
    const inputs = ["0", "1", "2", "100", "200.12"];
    expect.assertions(inputs.length);

    inputs.forEach((input) => {
      const output = toPositiveNumberWithDefault(DEFAULT, input);
      expect(output).toBe(Number(input));
    });
  });

  it("return default number if input is negative number", () => {
    const inputs = [-1, -2, -100, -Infinity];
    expect.assertions(inputs.length);

    inputs.forEach((input) => {
      const output = toPositiveNumberWithDefault(DEFAULT, input);
      expect(output).toBe(DEFAULT);
    });
  });

  it("return default number if input is negative number as string", () => {
    const inputs = ["-1", "-2", "-100"];
    expect.assertions(inputs.length);

    inputs.forEach((input) => {
      const output = toPositiveNumberWithDefault(DEFAULT, input);
      expect(output).toBe(DEFAULT);
    });
  });

  it("return default if input is not a number or infinite", () => {
    const inputs = [
      "vfdvf",
      null,
      undefined,
      NaN,
      "",
      {},
      { test: 1 },
      [],
      [1],
    ];

    expect.assertions(inputs.length);

    inputs.forEach((input) => {
      const output = toPositiveNumberWithDefault(DEFAULT, input);
      expect(output).toBe(DEFAULT);
    });
  });
});
