From ee506baf3c484fa2b047a9a28e4ab81c97289145 Mon Sep 17 00:00:00 2001 From: Sam Mayer Date: Wed, 15 Jan 2025 16:42:16 -0600 Subject: [PATCH] chore: remove max-statements warning in `schedule.test.ts` (#1671) ## Description This PR refactors a large test to use parameterized testing. ## Related Issue Relates to #1247 ## Type of change - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [x] Other (security config, docs update, etc) ## Checklist before merging - [x] Unit, [Journey](https://github.com/defenseunicorns/pepr/tree/main/journey), [E2E Tests](https://github.com/defenseunicorns/pepr-excellent-examples), [docs](https://github.com/defenseunicorns/pepr/tree/main/docs), [adr](https://github.com/defenseunicorns/pepr/tree/main/adr) added or updated as needed - [x] [Contributor Guide Steps](https://docs.pepr.dev/main/contribute/#submitting-a-pull-request) followed --- src/lib/core/schedule.test.ts | 101 ++++++++++++++++++++-------------- 1 file changed, 61 insertions(+), 40 deletions(-) diff --git a/src/lib/core/schedule.test.ts b/src/lib/core/schedule.test.ts index ca84784a4..7d43413af 100644 --- a/src/lib/core/schedule.test.ts +++ b/src/lib/core/schedule.test.ts @@ -2,7 +2,7 @@ // SPDX-FileCopyrightText: 2023-Present The Pepr Authors import { beforeEach, describe, expect, it, jest, afterEach } from "@jest/globals"; -import { OnSchedule, Schedule } from "./schedule"; +import { OnSchedule, Schedule, Unit } from "./schedule"; import { Unsubscribe } from "./storage"; export class MockStorage { @@ -121,45 +121,66 @@ describe("OnSchedule", () => { jest.clearAllTimers(); }); - it("should getDuration", () => { - // test second - mockSchedule.every = 10; - mockSchedule.unit = "seconds"; - const onSchedule = new OnSchedule(mockSchedule); - onSchedule.getDuration(); - expect(onSchedule.duration).toBe(10000); - - // test second error - mockSchedule.every = 8; - try { - onSchedule.getDuration(); - } catch (e) { - expect(e).toEqual(new Error("10 Seconds in the smallest interval allowed")); - } - - // test minute(s) - onSchedule.unit = "minutes"; - onSchedule.getDuration(); - expect(onSchedule.duration).toBe(600000); - onSchedule.unit = "minute"; - onSchedule.getDuration(); - expect(onSchedule.duration).toBe(600000); - - // test hour(s) - onSchedule.unit = "hours"; - onSchedule.getDuration(); - expect(onSchedule.duration).toBe(36000000); - onSchedule.unit = "hour"; - onSchedule.getDuration(); - expect(onSchedule.duration).toBe(36000000); - - // test invalid unit - onSchedule.unit = "second"; - try { - onSchedule.getDuration(); - } catch (e) { - expect(e).toEqual(new Error("Invalid time unit")); - } + describe("getDuration handles supported inputs", () => { + it.each([ + { + description: "test seconds", + setup: { every: 10, unit: "seconds" }, + expectedDuration: 10000, + shouldThrow: false, + }, + { + description: "test second error", + setup: { every: 8, unit: "seconds" }, + expectedError: new Error("10 Seconds in the smallest interval allowed"), + shouldThrow: true, + }, + { + description: "test minutes", + setup: { every: 10, unit: "minutes" }, + expectedDuration: 600000, + shouldThrow: false, + }, + { + description: "test singular minute", + setup: { every: 10, unit: "minute" }, + expectedDuration: 600000, + shouldThrow: false, + }, + { + description: "test hours", + setup: { every: 10, unit: "hours" }, + expectedDuration: 36000000, + shouldThrow: false, + }, + { + description: "test singular hour", + setup: { every: 10, unit: "hour" }, + expectedDuration: 36000000, + shouldThrow: false, + }, + { + description: "test invalid unit", + setup: { every: 10, unit: "second" }, + expectedError: new Error("Invalid time unit"), + shouldThrow: true, + }, + ])("%p", ({ description, setup, expectedDuration, expectedError, shouldThrow }) => { + const mockSchedule: Schedule = { + name: description, + every: setup.every, + unit: setup.unit as Unit, + run: jest.fn(), + }; + const onSchedule = new OnSchedule(mockSchedule); + + if (shouldThrow) { + expect(() => onSchedule.getDuration()).toThrow(expectedError); + } else { + onSchedule.getDuration(); + expect(onSchedule.duration).toBe(expectedDuration); + } + }); }); it("should setupInterval", () => {