updates on the minor changes like zone updates and datepicker fix
This commit is contained in:
@@ -122,3 +122,72 @@ describe('DatePicker — the popover after a choice', () => {
|
||||
expect(screen.queryByText('This Month')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* rangeSelect (Orders Summary): the first calendar click used to commit a
|
||||
* single day and close the popover, so an operator could never pick a from
|
||||
* AND a to date. With rangeSelect the first click only marks the start.
|
||||
*/
|
||||
describe('DatePicker — rangeSelect', () => {
|
||||
// A month well in the past, so every day in it is selectable under maxDate.
|
||||
const month = today.subtract(2, 'month').startOf('month');
|
||||
const day = (n) => month.date(n).format('YYYY-MM-DD');
|
||||
const dayButton = (n) =>
|
||||
screen.getAllByRole('button', { name: String(n) }).find((b) => !b.className.includes('text-ink-4/40'));
|
||||
|
||||
const openRangePicker = (onChange, extra = {}) => {
|
||||
render(<DatePicker value={{ from: day(1), to: day(1) }} onChange={onChange} rangeSelect maxDate={todayStr} {...extra} />);
|
||||
openCalendar();
|
||||
};
|
||||
|
||||
it('keeps the calendar open after the first click and emits nothing yet', () => {
|
||||
const onChange = jest.fn();
|
||||
openRangePicker(onChange);
|
||||
fireEvent.click(dayButton(5));
|
||||
expect(onChange).not.toHaveBeenCalled();
|
||||
expect(screen.getByText(/now pick the end date/i)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('emits { from, to } on the second click and closes', () => {
|
||||
const onChange = jest.fn();
|
||||
openRangePicker(onChange);
|
||||
fireEvent.click(dayButton(5));
|
||||
fireEvent.click(dayButton(12));
|
||||
expect(onChange).toHaveBeenCalledTimes(1);
|
||||
expect(onChange).toHaveBeenCalledWith({ from: day(5), to: day(12) });
|
||||
expect(screen.queryByText(/pick the end date/i)).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('orders the two clicks when the end is picked before the start', () => {
|
||||
const onChange = jest.fn();
|
||||
openRangePicker(onChange);
|
||||
fireEvent.click(dayButton(20));
|
||||
fireEvent.click(dayButton(3));
|
||||
expect(onChange).toHaveBeenCalledWith({ from: day(3), to: day(20) });
|
||||
});
|
||||
|
||||
it('greys out and ignores days after maxDate', () => {
|
||||
const onChange = jest.fn();
|
||||
render(<DatePicker value={todayStr} onChange={onChange} rangeSelect maxDate={todayStr} />);
|
||||
openCalendar();
|
||||
const tomorrow = today.add(1, 'day');
|
||||
// Only assert when tomorrow is visible in this month's grid.
|
||||
const btn = screen
|
||||
.getAllByRole('button', { name: String(tomorrow.date()) })
|
||||
.find((b) => b.disabled);
|
||||
if (tomorrow.month() === today.month()) {
|
||||
expect(btn).toBeDefined();
|
||||
fireEvent.click(btn);
|
||||
fireEvent.click(btn);
|
||||
expect(onChange).not.toHaveBeenCalled();
|
||||
}
|
||||
});
|
||||
|
||||
it('leaves the default (single-click) pickers unchanged', () => {
|
||||
const onChange = jest.fn();
|
||||
render(<DatePicker value={todayStr} onChange={onChange} />);
|
||||
openCalendar();
|
||||
fireEvent.click(screen.getByText('Yesterday'));
|
||||
expect(onChange).toHaveBeenCalledWith(today.subtract(1, 'day').format('YYYY-MM-DD'));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -347,10 +347,19 @@ describe('ZoneContext', () => {
|
||||
expect(result.current.matchesZone({ milerprofileid: 11, hubid: '2' })).toBe(true);
|
||||
});
|
||||
|
||||
it('should match a rider by their app location', () => {
|
||||
it('should match a rider by their app location (city id against city id)', () => {
|
||||
localStorage.setItem(ZONE_STORAGE_KEY, '2');
|
||||
const { result } = setup();
|
||||
expect(result.current.matchesZone({ milerprofileid: 11, applocationid: '2' })).toBe(true);
|
||||
const { result } = setup({ hubs: [{ ...KORAMANGALA, applocationid: '3' }] });
|
||||
expect(result.current.matchesZone({ milerprofileid: 11, applocationid: '3' })).toBe(true);
|
||||
});
|
||||
|
||||
// Regression: a rider's applocationid (a CITY id) was also compared with the
|
||||
// selected HUB id. Coimbatore Neptune Hub is hubid 2 in city 1, Hyderabad is
|
||||
// city 2 — so every Hyderabad rider matched the Coimbatore zone.
|
||||
it('should not match a rider whose city id merely equals the hub id', () => {
|
||||
localStorage.setItem(ZONE_STORAGE_KEY, '2');
|
||||
const { result } = setup({ hubs: [{ ...KORAMANGALA, applocationid: '1' }] });
|
||||
expect(result.current.matchesZone({ milerprofileid: 11, applocationid: '2', hubid: null })).toBe(false);
|
||||
});
|
||||
|
||||
it('should match a rider by city when they carry no hub id', () => {
|
||||
|
||||
@@ -52,3 +52,33 @@ describe('toDateRange', () => {
|
||||
expect(toDateRange(true)).toEqual({ from: '', to: '' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('toReportRange — DatePicker value → report { from, to }', () => {
|
||||
const { toReportRange } = require('@/lib/dateRange');
|
||||
const dayjsLib = require('dayjs');
|
||||
const todayStr = dayjsLib().format('YYYY-MM-DD');
|
||||
const past = (n) => dayjsLib().subtract(n, 'day').format('YYYY-MM-DD');
|
||||
const future = (n) => dayjsLib().add(n, 'day').format('YYYY-MM-DD');
|
||||
|
||||
it('turns a single day into a one-day range', () => {
|
||||
expect(toReportRange(past(3))).toEqual({ from: past(3), to: past(3) });
|
||||
});
|
||||
|
||||
it('keeps a valid range as it is', () => {
|
||||
expect(toReportRange({ from: past(7), to: past(1) })).toEqual({ from: past(7), to: past(1) });
|
||||
});
|
||||
|
||||
it('puts an inverted range in order', () => {
|
||||
expect(toReportRange({ from: past(1), to: past(7) })).toEqual({ from: past(7), to: past(1) });
|
||||
});
|
||||
|
||||
it('never goes past today', () => {
|
||||
expect(toReportRange(future(2))).toEqual({ from: todayStr, to: todayStr });
|
||||
expect(toReportRange({ from: past(2), to: future(5) })).toEqual({ from: past(2), to: todayStr });
|
||||
});
|
||||
|
||||
it('falls back to today for "all" or nothing', () => {
|
||||
expect(toReportRange('all')).toEqual({ from: todayStr, to: todayStr });
|
||||
expect(toReportRange(undefined)).toEqual({ from: todayStr, to: todayStr });
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user