From 5270f5e0c515b778b321919dcd3ba9590326685d Mon Sep 17 00:00:00 2001 From: MelvinBot Date: Sun, 1 Mar 2026 21:58:14 +0000 Subject: [PATCH] Fix flaky date preset tests in SearchUIUtilsTest The two date preset intersection tests used LAST_MONTH preset which resolves relative to the real system clock via getDateRangeForPreset(). The hardcoded expected dates assumed LAST_MONTH = January 2026, which was only true when run in February 2026. Freeze the clock with jest.useFakeTimers/setSystemTime to make these tests deterministic. Co-authored-by: Krishna --- tests/unit/Search/SearchUIUtilsTest.ts | 107 ++++++++++++++----------- 1 file changed, 60 insertions(+), 47 deletions(-) diff --git a/tests/unit/Search/SearchUIUtilsTest.ts b/tests/unit/Search/SearchUIUtilsTest.ts index dfa05ffaacfc8..f643e4a9820ac 100644 --- a/tests/unit/Search/SearchUIUtilsTest.ts +++ b/tests/unit/Search/SearchUIUtilsTest.ts @@ -3124,58 +3124,71 @@ describe('SearchUIUtils', () => { expect(result.end).toBe('2025-06-20'); }); - it('should intersect date preset with additional constraints instead of overwriting', () => { - // Test that when combining a date preset (EQUAL_TO) with additional constraints, - // we intersect the ranges (take max for start, min for end) rather than overwriting - const yearDateRange = DateUtils.getYearDateRange(2026); - const dateFilter = { - key: CONST.SEARCH.SYNTAX_FILTER_KEYS.DATE, - filters: [ - { - operator: CONST.SEARCH.SYNTAX_OPERATORS.EQUAL_TO, - value: CONST.SEARCH.DATE_PRESETS.LAST_MONTH, // e.g., January 2026: 2026-01-01 to 2026-01-31 - }, - { - operator: CONST.SEARCH.SYNTAX_OPERATORS.GREATER_THAN_OR_EQUAL_TO, - value: '2025-04-01', // Earlier than preset start - }, - ], - }; + // These tests use LAST_MONTH date preset which resolves relative to the current date, + // so we freeze the clock to February 2026 to ensure LAST_MONTH always means January 2026. + describe('date preset intersection with frozen clock', () => { + beforeEach(() => { + jest.useFakeTimers(); + jest.setSystemTime(new Date('2026-02-15T12:00:00Z')); + }); - const result = SearchUIUtils.adjustTimeRangeToDateFilters(yearDateRange, [dateFilter]); + afterEach(() => { + jest.useRealTimers(); + }); - // Should intersect: max(preset start, constraint start) = max(2026-01-01, 2025-04-01) = 2026-01-01 - // The preset start should be preserved, not overwritten by the earlier constraint - expect(result.start).toBe('2026-01-01'); - // End should remain the preset end (2026-01-31) - expect(result.end).toBe('2026-01-31'); - }); + it('should intersect date preset with additional constraints instead of overwriting', () => { + // Test that when combining a date preset (EQUAL_TO) with additional constraints, + // we intersect the ranges (take max for start, min for end) rather than overwriting + const yearDateRange = DateUtils.getYearDateRange(2026); + const dateFilter = { + key: CONST.SEARCH.SYNTAX_FILTER_KEYS.DATE, + filters: [ + { + operator: CONST.SEARCH.SYNTAX_OPERATORS.EQUAL_TO, + value: CONST.SEARCH.DATE_PRESETS.LAST_MONTH, // January 2026: 2026-01-01 to 2026-01-31 + }, + { + operator: CONST.SEARCH.SYNTAX_OPERATORS.GREATER_THAN_OR_EQUAL_TO, + value: '2025-04-01', // Earlier than preset start + }, + ], + }; - it('should intersect date preset end limit with additional constraints', () => { - // Test that when combining a date preset with an end constraint, - // we take the minimum (earliest) end date to intersect ranges - const yearDateRange = DateUtils.getYearDateRange(2026); - const dateFilter = { - key: CONST.SEARCH.SYNTAX_FILTER_KEYS.DATE, - filters: [ - { - operator: CONST.SEARCH.SYNTAX_OPERATORS.EQUAL_TO, - value: CONST.SEARCH.DATE_PRESETS.LAST_MONTH, // e.g., January 2026: 2026-01-01 to 2026-01-31 - }, - { - operator: CONST.SEARCH.SYNTAX_OPERATORS.LOWER_THAN_OR_EQUAL_TO, - value: '2026-01-15', // Earlier than preset end - }, - ], - }; + const result = SearchUIUtils.adjustTimeRangeToDateFilters(yearDateRange, [dateFilter]); - const result = SearchUIUtils.adjustTimeRangeToDateFilters(yearDateRange, [dateFilter]); + // Should intersect: max(preset start, constraint start) = max(2026-01-01, 2025-04-01) = 2026-01-01 + // The preset start should be preserved, not overwritten by the earlier constraint + expect(result.start).toBe('2026-01-01'); + // End should remain the preset end (2026-01-31) + expect(result.end).toBe('2026-01-31'); + }); - // Start should remain the preset start (2026-01-01) - expect(result.start).toBe('2026-01-01'); - // Should intersect: min(preset end, constraint end) = min(2026-01-31, 2026-01-15) = 2026-01-15 - // The constraint end should be used (earlier date) - expect(result.end).toBe('2026-01-15'); + it('should intersect date preset end limit with additional constraints', () => { + // Test that when combining a date preset with an end constraint, + // we take the minimum (earliest) end date to intersect ranges + const yearDateRange = DateUtils.getYearDateRange(2026); + const dateFilter = { + key: CONST.SEARCH.SYNTAX_FILTER_KEYS.DATE, + filters: [ + { + operator: CONST.SEARCH.SYNTAX_OPERATORS.EQUAL_TO, + value: CONST.SEARCH.DATE_PRESETS.LAST_MONTH, // January 2026: 2026-01-01 to 2026-01-31 + }, + { + operator: CONST.SEARCH.SYNTAX_OPERATORS.LOWER_THAN_OR_EQUAL_TO, + value: '2026-01-15', // Earlier than preset end + }, + ], + }; + + const result = SearchUIUtils.adjustTimeRangeToDateFilters(yearDateRange, [dateFilter]); + + // Start should remain the preset start (2026-01-01) + expect(result.start).toBe('2026-01-01'); + // Should intersect: min(preset end, constraint end) = min(2026-01-31, 2026-01-15) = 2026-01-15 + // The constraint end should be used (earlier date) + expect(result.end).toBe('2026-01-15'); + }); }); it('should correctly intersect multiple date filters (GREATER_THAN and LOWER_THAN) when expanding quarter groups', () => {