Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/elements/public/ReportForm/ReportForm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@ import { ReportForm as Form } from './ReportForm';
import { InternalSelectControl } from '../../internal/InternalSelectControl/InternalSelectControl';
import { InternalNativeDateControl } from '../../internal/InternalNativeDateControl/InternalNativeDateControl';
import { InternalSwitchControl } from '../../internal/InternalSwitchControl/InternalSwitchControl';
import { getCurrentMonth, toAPIDateTime } from './utils';
import {
getCurrentMonth,
getCurrentQuarter,
getCurrentYear,
getLast30Days,
getLast365Days,
getPreviousMonth,
getPreviousQuarter,
getPreviousYear,
toAPIDateTime,
} from './utils';
import { getTestData } from '../../../testgen/getTestData';
import { stub } from 'sinon';

Expand Down Expand Up @@ -156,6 +166,31 @@ describe('ReportForm', () => {
expect(control.getValue()).to.equal('4'); // 'this_month' preset
});

it('uses calendar-aligned boundaries for all report date range presets', () => {
const now = new Date(2026, 3, 17, 12, 34, 56);
const cases = [
{ range: getPreviousQuarter(now), start: '2026-01-01T00:00:00', end: '2026-03-31T23:59:59' },
{ range: getCurrentQuarter(now), start: '2026-04-01T00:00:00', end: '2026-06-30T23:59:59' },
{ range: getPreviousMonth(now), start: '2026-03-01T00:00:00', end: '2026-03-31T23:59:59' },
{ range: getCurrentMonth(now), start: '2026-04-01T00:00:00', end: '2026-04-30T23:59:59' },
{ range: getPreviousYear(now), start: '2025-01-01T00:00:00', end: '2025-12-31T23:59:59' },
{ range: getCurrentYear(now), start: '2026-01-01T00:00:00', end: '2026-12-31T23:59:59' },
{ range: getLast365Days(now), start: '2025-04-17T00:00:00', end: '2026-04-17T23:59:59' },
{ range: getLast30Days(now), start: '2026-03-18T00:00:00', end: '2026-04-17T23:59:59' },
];

cases.forEach(({ range, start, end }) => {
expect(toAPIDateTime(range.start)).to.equal(start);
expect(toAPIDateTime(range.end)).to.equal(end);
});
});

it('uses previous year when previous quarter crosses year boundary', () => {
const { start, end } = getPreviousQuarter(new Date(2026, 0, 7, 9, 0, 0));
expect(toAPIDateTime(start)).to.equal('2025-10-01T00:00:00');
expect(toAPIDateTime(end)).to.equal('2025-12-31T23:59:59');
});

it('uses custom setValue for preset control to set datetime_start and datetime_end', async () => {
const element = await fixture<Form>(html`<foxy-report-form></foxy-report-form>`);
const control = element.renderRoot.querySelector<InternalSelectControl>('[infer="preset"]')!;
Expand Down
24 changes: 12 additions & 12 deletions src/elements/public/ReportForm/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ export function getPreviousQuarter(now = new Date()): Range {
const previousQuarterYear = now.getFullYear() - (currentQuarter === 0 ? 1 : 0);

const previousQuarterStart = quarters[previousQuarter][0];
const start = safeDate(previousQuarterYear, previousQuarterStart);
const start = safeDate(previousQuarterYear, previousQuarterStart, 1);

const previousQuarterEnd = quarters[previousQuarter][2];
const end = new Date(new Date(previousQuarterYear, previousQuarterEnd + 1).getTime() - 1);
const end = new Date(safeDate(previousQuarterYear, previousQuarterEnd + 1, 1).getTime() - 1);

return { start, end };
}
Expand All @@ -31,38 +31,38 @@ export function getCurrentQuarter(now = new Date()): Range {
const currentQuarter = quarters.findIndex(months => months.includes(currentMonth));

const currentQuarterStart = quarters[currentQuarter][0];
const start = safeDate(now.getFullYear(), currentQuarterStart);
const start = safeDate(now.getFullYear(), currentQuarterStart, 1);

const currentQuarterEnd = quarters[currentQuarter][2];
const end = new Date(safeDate(now.getFullYear(), currentQuarterEnd + 1).getTime() - 1);
const end = new Date(safeDate(now.getFullYear(), currentQuarterEnd + 1, 1).getTime() - 1);

return { start, end };
}

export function getPreviousMonth(now = new Date()): Range {
const start = safeDate(now.getFullYear(), now.getMonth() - 1);
const end = new Date(safeDate(now.getFullYear(), now.getMonth()).getTime() - 1);
const start = safeDate(now.getFullYear(), now.getMonth() - 1, 1);
const end = new Date(safeDate(now.getFullYear(), now.getMonth(), 1).getTime() - 1);

return { start, end };
}

export function getCurrentMonth(now = new Date()): Range {
const start = safeDate(now.getFullYear(), now.getMonth());
const end = new Date(safeDate(now.getFullYear(), now.getMonth() + 1).getTime() - 1);
const start = safeDate(now.getFullYear(), now.getMonth(), 1);
const end = new Date(safeDate(now.getFullYear(), now.getMonth() + 1, 1).getTime() - 1);

return { start, end };
}

export function getPreviousYear(now = new Date()): Range {
const start = safeDate(now.getFullYear() - 1, 0);
const end = new Date(safeDate(now.getFullYear(), 0).getTime() - 1);
const start = safeDate(now.getFullYear() - 1, 0, 1);
const end = new Date(safeDate(now.getFullYear(), 0, 1).getTime() - 1);

return { start, end };
}

export function getCurrentYear(now = new Date()): Range {
const start = safeDate(now.getFullYear(), 0);
const end = new Date(safeDate(now.getFullYear() + 1, 0).getTime() - 1);
const start = safeDate(now.getFullYear(), 0, 1);
const end = new Date(safeDate(now.getFullYear() + 1, 0, 1).getTime() - 1);

return { start, end };
}
Expand Down
Loading