import { beforeAll, describe, expect, it } from "@jest/globals"

import { getValidFromDate, getValidToDate } from "./getValidDates"

const NOW = new Date("2020-10-01T00:00:00Z")

describe("getValidFromDate", () => {
  beforeAll(() => {
    jest.useFakeTimers({ now: NOW })
  })

  afterAll(() => {
    jest.useRealTimers()
  })

  describe("getValidFromDate", () => {
    it("returns today when empty string is provided", () => {
      const actual = getValidFromDate("")
      expect(actual.toISOString()).toBe("2020-10-01T00:00:00.000Z")
    })

    it("returns today when undefined is provided", () => {
      const actual = getValidFromDate(undefined)
      expect(actual.toISOString()).toBe("2020-10-01T00:00:00.000Z")
    })

    it("returns given date in utc", () => {
      const actual = getValidFromDate("2024-01-01")
      expect(actual.toISOString()).toBe("2024-01-01T00:00:00.000Z")
    })
  })

  describe("getValidToDate", () => {
    it("returns day after fromDate when empty string is provided", () => {
      const actual = getValidToDate("", NOW)
      expect(actual.toISOString()).toBe("2020-10-02T00:00:00.000Z")
    })

    it("returns day after fromDate when undefined is provided", () => {
      const actual = getValidToDate(undefined, NOW)
      expect(actual.toISOString()).toBe("2020-10-02T00:00:00.000Z")
    })

    it("returns given date in utc", () => {
      const actual = getValidToDate("2024-01-01", NOW)
      expect(actual.toISOString()).toBe("2024-01-01T00:00:00.000Z")
    })

    it("fallsback to day after fromDate when given date is before fromDate", () => {
      const actual = getValidToDate("2020-09-30", NOW)
      expect(actual.toISOString()).toBe("2020-10-02T00:00:00.000Z")
    })
  })
})
