@@ -47,13 +47,15 @@ import {
4747 quarterDisabledAfter ,
4848 getWeek ,
4949 safeDateRangeFormat ,
50+ safeDateFormat ,
5051 getHolidaysMap ,
5152 arraysAreEqual ,
5253 startOfMinute ,
5354 isDateBefore ,
5455 getMidnightDate ,
5556 registerLocale ,
5657 isMonthYearDisabled ,
58+ getDefaultLocale ,
5759} from "../date_utils" ;
5860
5961registerLocale ( "pt-BR" , ptBR ) ;
@@ -1477,4 +1479,165 @@ describe("date_utils", () => {
14771479 expect ( isMonthYearDisabled ( date ) ) . toBe ( false ) ;
14781480 } ) ;
14791481 } ) ;
1482+
1483+ describe ( "safeDateFormat critical coverage" , ( ) => {
1484+ it ( "warns when locale object is not found" , ( ) => {
1485+ const consoleWarnSpy = jest . spyOn ( console , "warn" ) . mockImplementation ( ) ;
1486+ const testDate = new Date ( "2024-01-15T10:00:00" ) ;
1487+
1488+ safeDateFormat ( testDate , {
1489+ dateFormat : "PP" ,
1490+ locale : "invalid-locale-xyz" ,
1491+ } ) ;
1492+
1493+ expect ( consoleWarnSpy ) . toHaveBeenCalledWith (
1494+ expect . stringContaining (
1495+ 'A locale object was not found for the provided string ["invalid-locale-xyz"]' ,
1496+ ) ,
1497+ ) ;
1498+
1499+ consoleWarnSpy . mockRestore ( ) ;
1500+ } ) ;
1501+
1502+ it ( "does not warn when valid locale is provided" , ( ) => {
1503+ const consoleWarnSpy = jest . spyOn ( console , "warn" ) . mockImplementation ( ) ;
1504+ const testDate = new Date ( "2024-01-15T10:00:00" ) ;
1505+
1506+ safeDateFormat ( testDate , {
1507+ dateFormat : "PP" ,
1508+ locale : getDefaultLocale ( ) ,
1509+ } ) ;
1510+
1511+ expect ( consoleWarnSpy ) . not . toHaveBeenCalled ( ) ;
1512+ consoleWarnSpy . mockRestore ( ) ;
1513+ } ) ;
1514+
1515+ it ( "falls back to default locale for invalid locale values" , ( ) => {
1516+ const consoleWarnSpy = jest . spyOn ( console , "warn" ) . mockImplementation ( ) ;
1517+ const testDate = new Date ( "2024-01-15T10:00:00" ) ;
1518+
1519+ const result = safeDateFormat ( testDate , {
1520+ dateFormat : "yyyy-MM-dd" ,
1521+ locale : "invalid-locale" ,
1522+ } ) ;
1523+
1524+ expect ( result ) . toBeTruthy ( ) ;
1525+ expect ( typeof result ) . toBe ( "string" ) ;
1526+
1527+ consoleWarnSpy . mockRestore ( ) ;
1528+ } ) ;
1529+
1530+ it ( "handles very old dates" , ( ) => {
1531+ const formatted = safeDateFormat ( new Date ( "1900-01-01" ) , {
1532+ dateFormat : "yyyy-MM-dd" ,
1533+ } ) ;
1534+
1535+ expect ( formatted ) . toContain ( "1900" ) ;
1536+ } ) ;
1537+
1538+ it ( "handles far future dates" , ( ) => {
1539+ const formatted = safeDateFormat ( new Date ( "2099-12-31" ) , {
1540+ dateFormat : "yyyy-MM-dd" ,
1541+ } ) ;
1542+
1543+ expect ( formatted ) . toContain ( "2099" ) ;
1544+ } ) ;
1545+
1546+ it ( "handles leap year dates" , ( ) => {
1547+ const formatted = safeDateFormat ( new Date ( "2024-02-29" ) , {
1548+ dateFormat : "yyyy-MM-dd" ,
1549+ } ) ;
1550+
1551+ expect ( formatted ) . toContain ( "2024-02-29" ) ;
1552+ } ) ;
1553+
1554+ it ( "handles daylight saving time transitions" , ( ) => {
1555+ const formatted = safeDateFormat ( new Date ( "2024-03-10T02:30:00" ) , {
1556+ dateFormat : "yyyy-MM-dd HH:mm" ,
1557+ } ) ;
1558+
1559+ expect ( formatted ) . toBeTruthy ( ) ;
1560+ expect ( typeof formatted ) . toBe ( "string" ) ;
1561+ } ) ;
1562+
1563+ it ( "supports time tokens in the format string" , ( ) => {
1564+ const formatted = safeDateFormat ( new Date ( "2024-01-15T14:30:45" ) , {
1565+ dateFormat : "yyyy-MM-dd HH:mm:ss" ,
1566+ } ) ;
1567+
1568+ expect ( formatted ) . toContain ( "2024-01-15" ) ;
1569+ expect ( formatted ) . toContain ( "14:30:45" ) ;
1570+ } ) ;
1571+
1572+ it ( "supports localized patterns" , ( ) => {
1573+ const formatted = safeDateFormat ( new Date ( "2024-01-15" ) , {
1574+ dateFormat : "PPP" ,
1575+ } ) ;
1576+
1577+ expect ( formatted ) . toBeTruthy ( ) ;
1578+ expect ( typeof formatted ) . toBe ( "string" ) ;
1579+ } ) ;
1580+ } ) ;
1581+
1582+ describe ( "isDayInRange error handling" , ( ) => {
1583+ it ( "returns false when isWithinInterval throws" , ( ) => {
1584+ const testDate = new Date ( "2024-01-15" ) ;
1585+ const invalidStartDate = new Date ( "invalid" ) ;
1586+ const invalidEndDate = new Date ( "also-invalid" ) ;
1587+
1588+ const result = isDayInRange ( testDate , invalidStartDate , invalidEndDate ) ;
1589+
1590+ expect ( result ) . toBe ( false ) ;
1591+ } ) ;
1592+
1593+ it ( "returns true for dates inside a valid range" , ( ) => {
1594+ const result = isDayInRange (
1595+ new Date ( "2024-01-15" ) ,
1596+ new Date ( "2024-01-10" ) ,
1597+ new Date ( "2024-01-20" ) ,
1598+ ) ;
1599+
1600+ expect ( result ) . toBe ( true ) ;
1601+ } ) ;
1602+
1603+ it ( "returns false for dates outside a valid range" , ( ) => {
1604+ const result = isDayInRange (
1605+ new Date ( "2024-01-25" ) ,
1606+ new Date ( "2024-01-10" ) ,
1607+ new Date ( "2024-01-20" ) ,
1608+ ) ;
1609+
1610+ expect ( result ) . toBe ( false ) ;
1611+ } ) ;
1612+
1613+ it ( "handles the edge case where start and end dates are equal" , ( ) => {
1614+ const testDate = new Date ( "2024-01-15" ) ;
1615+ const startDate = new Date ( "2024-01-15" ) ;
1616+ const endDate = new Date ( "2024-01-15" ) ;
1617+
1618+ const result = isDayInRange ( testDate , startDate , endDate ) ;
1619+
1620+ expect ( result ) . toBe ( true ) ;
1621+ } ) ;
1622+
1623+ it ( "handles null start date inputs" , ( ) => {
1624+ const result = isDayInRange (
1625+ new Date ( "2024-01-15" ) ,
1626+ null as unknown as Date ,
1627+ new Date ( "2024-01-20" ) ,
1628+ ) ;
1629+
1630+ expect ( typeof result ) . toBe ( "boolean" ) ;
1631+ } ) ;
1632+
1633+ it ( "handles null end date inputs" , ( ) => {
1634+ const result = isDayInRange (
1635+ new Date ( "2024-01-15" ) ,
1636+ new Date ( "2024-01-10" ) ,
1637+ null as unknown as Date ,
1638+ ) ;
1639+
1640+ expect ( typeof result ) . toBe ( "boolean" ) ;
1641+ } ) ;
1642+ } ) ;
14801643} ) ;
0 commit comments