-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved function to another file to avoid moment intialization
- Loading branch information
Showing
2 changed files
with
87 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ) ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |