From a0746e407273b35907b458aea935661bc3d9c47b Mon Sep 17 00:00:00 2001 From: Giovanni Baratta Date: Sun, 19 May 2024 11:20:43 +0200 Subject: [PATCH] Add tests for isPeriodBetweenStartAndEnd Signed-off-by: Giovanni Baratta --- src/utils/date.test.ts | 107 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/src/utils/date.test.ts b/src/utils/date.test.ts index eca0292..61b3453 100644 --- a/src/utils/date.test.ts +++ b/src/utils/date.test.ts @@ -1,5 +1,6 @@ import {isSamePeriod} from "./date.ts" import {Period} from "../model/monthly-report.ts" +import {isPeriodBetweenStartAndEnd} from "./date" describe("isSamePeriod", () => { it("should return true if the dates are in the same period", () => { @@ -48,3 +49,109 @@ describe("isSamePeriod", () => { expect(result).toBe(false) }) }) + +describe("isPeriodBetweenStartAndEnd", () => { + it("should return true if the period is between the start and end periods (inclusive)", () => { + // Given + const period = {year: 2023, month: 2} + const start = {year: 2023, month: 1} + const end = {year: 2023, month: 3} + + // When + const result = isPeriodBetweenStartAndEnd(period, start, end) + + // Expect + expect(result).toBe(true) + }) + + it("should return true if the period is equal to the start period (inclusive)", () => { + // Given + const period = {year: 2023, month: 1} + const start = {year: 2023, month: 1} + const end = {year: 2023, month: 3} + + // When + const result = isPeriodBetweenStartAndEnd(period, start, end, {includeStart: true}) + + // Expect + expect(result).toBe(true) + }) + + it("should return true if the period is equal to the end period (inclusive)", () => { + // Given + const period = {year: 2023, month: 3} + const start = {year: 2023, month: 1} + const end = {year: 2023, month: 3} + + // When + const result = isPeriodBetweenStartAndEnd(period, start, end, {includeEnd: true}) + + // Expect + expect(result).toBe(true) + }) + + it("should return false if the period is before the start period", () => { + // Given + const period = {year: 2023, month: 1} + const start = {year: 2023, month: 2} + const end = {year: 2023, month: 3} + + // When + const result = isPeriodBetweenStartAndEnd(period, start, end) + + // Expect + expect(result).toBe(false) + }) + + it("should return false if the period is after the end period", () => { + // Given + const period = {year: 2023, month: 4} + const start = {year: 2023, month: 1} + const end = {year: 2023, month: 3} + + // When + const result = isPeriodBetweenStartAndEnd(period, start, end) + + // Expect + expect(result).toBe(false) + }) + + it("should handle different years correctly", () => { + // Given + const period = {year: 2024, month: 1} + const start = {year: 2023, month: 12} + const end = {year: 2024, month: 2} + + // When + const result = isPeriodBetweenStartAndEnd(period, start, end) + + // Expect + expect(result).toBe(true) + }) + + it("should return false if the period is equal to the start period but includeStart is false", () => { + // Given + const period = {year: 2023, month: 1} + const start = {year: 2023, month: 1} + const end = {year: 2023, month: 3} + + // When + const result = isPeriodBetweenStartAndEnd(period, start, end, {includeStart: false}) + + // Expect + expect(result).toBe(false) + }) + + it("should return false if the period is equal to the end period but includeEnd is false", () => { + // Given + const period = {year: 2023, month: 3} + const start = {year: 2023, month: 1} + const end = {year: 2023, month: 3} + + // When + const result = isPeriodBetweenStartAndEnd(period, start, end, {includeEnd: false}) + + // Expect + expect(result).toBe(false) + }) +})