Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

add tests on formatISODateString

ThieryMichel645be185654e
diff --git a/src/components/atomic-crm/deals/DealShow.tsx b/src/components/atomic-crm/deals/DealShow.tsx
index 1c48a8c0..ea83d8d6 100644
--- a/src/components/atomic-crm/deals/DealShow.tsx
+++ b/src/components/atomic-crm/deals/DealShow.tsx
@@ -1,5 +1,5 @@
import { useMutation } from "@tanstack/react-query";
-import { format, isValid } from "date-fns";
+import { isValid } from "date-fns";
import { Archive, ArchiveRestore } from "lucide-react";
import {
ShowBase,
@@ -27,6 +27,7 @@ import { useConfigurationContext } from "../root/ConfigurationContext";
import type { Deal } from "../types";
import { ContactList } from "./ContactList";
import { findDealLabel } from "./deal";
+import { formatISODateString } from "./dealUtils";
export const DealShow = ({ open, id }: { open: boolean; id?: string }) => {
const redirect = useRedirect();
@@ -47,20 +48,6 @@ export const DealShow = ({ open, id }: { open: boolean; id?: string }) => {
);
};
-const isoDateStringRegex = /^\d{4}-\d{2}-\d{2}$/;
-
-export function formatISODateString(dateString: string) {
- if (!isoDateStringRegex.test(dateString)) {
- throw new Error("Invalid date format. Expected YYYY-MM-DD.");
- }
- // Some browsers Will consider a date in the format YYYY-MM-DD as UTC, which can cause off-by-one-day issues depending on the user's timezone.
- // To avoid this, we can parse the date components manually and create a date object in the local timezone.
- const [year, month, day] = dateString.split("-").map(Number);
- const date = new Date(year, month - 1, day);
-
- return format(date, "PP");
-}
-
const DealShowContent = () => {
const { dealStages, dealCategories } = useConfigurationContext();
const record = useRecordContext<Deal>();
diff --git a/src/components/atomic-crm/deals/dealUtils.spec.ts b/src/components/atomic-crm/deals/dealUtils.spec.ts
new file mode 100644
index 00000000..6d89f378
--- /dev/null
+++ b/src/components/atomic-crm/deals/dealUtils.spec.ts
@@ -0,0 +1,35 @@
+import { formatISODateString } from "./dealUtils";
+
+describe("formatISODateString", () => {
+ it("formats a valid ISO date string correctly", () => {
+ const isoDate = "2024-06-15";
+ const formattedDate = formatISODateString(isoDate);
+ expect(formattedDate).toBe("Jun 15, 2024");
+ });
+
+ it("should not shift the date for different timezone", () => {
+ const originalTZ = process.env.TZ;
+ process.env.TZ = "America/New_York";
+ const isoDate = "2024-06-15";
+ expect(formatISODateString(isoDate)).toBe("Jun 15, 2024");
+ process.env.TZ = "Asia/Tokyo";
+ expect(formatISODateString(isoDate)).toBe("Jun 15, 2024");
+ process.env.TZ = "UTC";
+ expect(formatISODateString(isoDate)).toBe("Jun 15, 2024");
+ process.env.TZ = originalTZ; // Reset timezone after test
+ });
+
+ it("throw for an invalid date string", () => {
+ const invalidDate = "invalid-date";
+ expect(() => formatISODateString(invalidDate)).toThrow(
+ "Invalid date format. Expected YYYY-MM-DD.",
+ );
+ });
+
+ it("throw for a date string with wrong format", () => {
+ const invalidDate = "15-06-2024";
+ expect(() => formatISODateString(invalidDate)).toThrow(
+ "Invalid date format. Expected YYYY-MM-DD.",
+ );
+ });
+});
diff --git a/src/components/atomic-crm/deals/dealUtils.ts b/src/components/atomic-crm/deals/dealUtils.ts
index 2371f8ef..462e20d9 100644
--- a/src/components/atomic-crm/deals/dealUtils.ts
+++ b/src/components/atomic-crm/deals/dealUtils.ts
@@ -1,3 +1,5 @@
+import { format } from "date-fns";
+
export function getRelativeTimeString(dateString: string): string {
const date = new Date(dateString);
date.setHours(0, 0, 0, 0);
@@ -24,3 +26,17 @@ export function getRelativeTimeString(dateString: string): string {
function ucFirst(str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1);
}
+
+const isoDateStringRegex = /^\d{4}-\d{2}-\d{2}$/;
+
+export function formatISODateString(dateString: string) {
+ if (!isoDateStringRegex.test(dateString)) {
+ throw new Error("Invalid date format. Expected YYYY-MM-DD.");
+ }
+ // Some browsers Will consider a date in the format YYYY-MM-DD as UTC, which can cause off-by-one-day issues depending on the user's timezone.
+ // To avoid this, we can parse the date components manually and create a date object in the local timezone.
+ const [year, month, day] = dateString.split("-").map(Number);
+ const date = new Date(year, month - 1, day);
+
+ return format(date, "PP");
+}