Skip to content

Commit

Permalink
moved function to another file to avoid moment intialization
Browse files Browse the repository at this point in the history
  • Loading branch information
kangzj committed Sep 16, 2024
1 parent 89f771d commit 9b5403c
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 67 deletions.
112 changes: 45 additions & 67 deletions client/components/date-range/test/add-date-to-range.js
Original file line number Diff line number Diff line change
@@ -1,88 +1,66 @@
import moment from 'moment';
import { addDayToRange } from '../date-range-picker';

// Mock Moment.js
jest.mock( 'moment', () => {
const mMoment = {
isSame: jest.fn(),
isBefore: jest.fn(),
isAfter: jest.fn(),
diff: jest.fn(),
default: { locale: jest.fn() },
};
return jest.fn( () => mMoment );
} );
import { addDayToRange } from '../utils';

describe( 'addDayToRange', () => {
let mockMoment;

beforeEach( () => {
mockMoment = moment();
jest.clearAllMocks();
test( 'should return the same range if day is invalid', () => {
const range = { from: moment( '2023-01-01' ), to: moment( '2023-01-05' ) };
const result = addDayToRange( null, range );
expect( result ).toEqual( range );
} );

test( 'should set start date when range is empty', () => {
const result = addDayToRange( mockMoment, { from: null, to: null } );
expect( result ).toEqual( { from: mockMoment, to: null } );
test( 'should set "from" to null if day is the same as "from"', () => {
const range = { from: moment( '2023-01-01' ), to: moment( '2023-01-05' ) };
const result = addDayToRange( moment( '2023-01-01' ), range );
expect( result.from ).toBeNull();
expect( result.to ).toEqual( range.to );
} );

test( 'should set end date when start date is set but end date is null', () => {
const mockFrom = moment();
const result = addDayToRange( mockMoment, { from: mockFrom, to: null } );
expect( result ).toEqual( { from: mockFrom, to: mockMoment } );
test( 'should set "to" to null if day is the same as "to"', () => {
const range = { from: moment( '2023-01-01' ), to: moment( '2023-01-05' ) };
const result = addDayToRange( moment( '2023-01-05' ), range );
expect( result.from ).toEqual( range.from );
expect( result.to ).toBeNull();
} );

test( 'should clear start date when day is same as start date', () => {
const mockFrom = moment();
mockFrom.isSame.mockReturnValue( true );
const result = addDayToRange( mockMoment, { from: mockFrom, to: null } );
expect( result ).toEqual( { from: null, to: null } );
test( 'should set "from" if it is null', () => {
const range = { from: null, to: moment( '2023-01-05' ) };
const result = addDayToRange( moment( '2023-01-01' ), range );
expect( result.from ).toEqual( moment( '2023-01-01' ) );
expect( result.to ).toEqual( range.to );
} );

test( 'should clear end date when day is same as end date', () => {
const mockFrom = moment();
const mockTo = moment();
mockTo.isSame.mockReturnValue( true );
const result = addDayToRange( mockMoment, { from: mockFrom, to: mockTo } );
expect( result ).toEqual( { from: mockFrom, to: null } );
test( 'should set "to" if it is null', () => {
const range = { from: moment( '2023-01-01' ), to: null };
const result = addDayToRange( moment( '2023-01-05' ), range );
expect( result.from ).toEqual( range.from );
expect( result.to ).toEqual( moment( '2023-01-05' ) );
} );

test( 'should set start date when day is before current range', () => {
const mockFrom = moment();
const mockTo = moment();
mockMoment.isBefore.mockReturnValue( true );
const result = addDayToRange( mockMoment, { from: mockFrom, to: mockTo } );
expect( result ).toEqual( { from: mockMoment, to: mockTo } );
test( 'should update "from" if day is before current "from"', () => {
const range = { from: moment( '2023-01-05' ), to: moment( '2023-01-10' ) };
const result = addDayToRange( moment( '2023-01-01' ), range );
expect( result.from ).toEqual( moment( '2023-01-01' ) );
expect( result.to ).toEqual( range.to );
} );

test( 'should set end date when day is after current range', () => {
const mockFrom = moment();
const mockTo = moment();
mockMoment.isBefore.mockReturnValue( false );
mockMoment.isAfter.mockReturnValue( true );
const result = addDayToRange( mockMoment, { from: mockFrom, to: mockTo } );
expect( result ).toEqual( { from: mockFrom, to: mockMoment } );
test( 'should update "to" if day is after current "to"', () => {
const range = { from: moment( '2023-01-01' ), to: moment( '2023-01-05' ) };
const result = addDayToRange( moment( '2023-01-10' ), range );
expect( result.from ).toEqual( range.from );
expect( result.to ).toEqual( moment( '2023-01-10' ) );
} );

test( 'should set start date when day is closer to start', () => {
const mockFrom = moment();
const mockTo = moment();
mockMoment.isBefore.mockReturnValue( false );
mockMoment.isAfter.mockReturnValue( false );
mockFrom.diff.mockReturnValue( 1 );
mockTo.diff.mockReturnValue( 2 );
const result = addDayToRange( mockMoment, { from: mockFrom, to: mockTo } );
expect( result ).toEqual( { from: mockMoment, to: mockTo } );
test( 'should update "from" if day is closer to "from" than "to"', () => {
const range = { from: moment( '2023-01-01' ), to: moment( '2023-01-10' ) };
const result = addDayToRange( moment( '2023-01-04' ), range );
expect( result.from ).toEqual( moment( '2023-01-04' ) );
expect( result.to ).toEqual( range.to );
} );

test( 'should set end date when day is closer to end', () => {
const mockFrom = moment();
const mockTo = moment();
mockMoment.isBefore.mockReturnValue( false );
mockMoment.isAfter.mockReturnValue( false );
mockFrom.diff.mockReturnValue( 2 );
mockTo.diff.mockReturnValue( 1 );
const result = addDayToRange( mockMoment, { from: mockFrom, to: mockTo } );
expect( result ).toEqual( { from: mockFrom, to: mockMoment } );
test( 'should update "to" if day is closer to "to" than "from"', () => {
const range = { from: moment( '2023-01-01' ), to: moment( '2023-01-10' ) };
const result = addDayToRange( moment( '2023-01-07' ), range );
expect( result.from ).toEqual( range.from );
expect( result.to ).toEqual( moment( '2023-01-07' ) );
} );
} );
42 changes: 42 additions & 0 deletions client/components/date-range/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Moment } from 'moment';

interface DateRange {
from: Moment | null;
to: Moment | null;
}

function addDayToRange( day: Moment, range: DateRange ): DateRange {
if ( ! day || ! day.isValid() ) {
return range;
}

const { from, to } = range;

if ( from?.isSame( day ) ) {
return { ...range, from: null };
}
if ( to?.isSame( day ) ) {
return { ...range, to: null };
}

if ( ! from ) {
return { ...range, from: day };
}
if ( ! to ) {
return { ...range, to: day };
}

if ( day.isBefore( from ) ) {
return { ...range, from: day };
}
if ( day.isAfter( to ) ) {
return { ...range, to: day };
}

const daysFromStart = Math.abs( from.diff( day, 'days' ) );
const daysFromEnd = Math.abs( to.diff( day, 'days' ) );

return daysFromStart < daysFromEnd ? { ...range, from: day } : { ...range, to: day };
}

export { addDayToRange };

0 comments on commit 9b5403c

Please sign in to comment.