import { Percentage } from "./Percentage";

describe("Percentage", () => {
  test("should create an instance with default values", () => {
    const percentage = Percentage.create();
    expect(percentage.isSuccess).toBe(true);
    expect(percentage.object.amount).toBe(0);
    expect(percentage.object.scale).toBe(2);
  });

  test("should create an instance with specific values", () => {
    const percentage = Percentage.create({ amount: 50, scale: 1 });
    expect(percentage.isSuccess).toBe(true);
    expect(percentage.object.amount).toBe(50);
    expect(percentage.object.scale).toBe(1);
  });

  test("should fail to create an instance with invalid values", () => {
    const percentage = Percentage.create({ amount: 15000, scale: 2 });
    expect(percentage.isFailure).toBe(true);
  });

  test("should convert scale and check range", () => {
    const percentage = Percentage.create({ amount: 9999, scale: 2 }).object;

    const converted = percentage.convertScale(0);
    expect(converted.amount).toBe(100);
    expect(converted.scale).toBe(0);

    expect(() => {
      percentage.convertScale(3);
    }).toThrow("Scale out of range");
  });

  test("should return primitive value", () => {
    const percentage = Percentage.create({ amount: 9234, scale: 2 }).object;
    expect(percentage.toPrimitive()).toBe(9234);

    const convertedHalfUp = percentage.convertScale(1);
    expect(convertedHalfUp.toPrimitive()).toBe(9230);
  });
});
