|
|
jest.mock( 'react', () => ( { |
|
|
...jest.requireActual( 'react' ), |
|
|
useCallback: jest.fn( ( fn ) => fn ), |
|
|
} ) ); |
|
|
jest.mock( 'calypso/components/localized-moment' ); |
|
|
jest.mock( 'calypso/lib/jetpack/hooks/use-date-with-offset' ); |
|
|
jest.mock( 'calypso/my-sites/backup/hooks', () => ( { |
|
|
...jest.requireActual( 'calypso/my-sites/backup/hooks' ), |
|
|
} ) ); |
|
|
|
|
|
import moment from 'moment'; |
|
|
import { useCallback } from 'react'; |
|
|
import { useLocalizedMoment } from 'calypso/components/localized-moment'; |
|
|
import useDateWithOffset from 'calypso/lib/jetpack/hooks/use-date-with-offset'; |
|
|
import { useCanGoToDate } from '../hooks'; |
|
|
|
|
|
describe( 'useCanGoToDate', () => { |
|
|
beforeEach( () => { |
|
|
jest.clearAllMocks(); |
|
|
|
|
|
useCallback.mockImplementation( ( fn ) => fn ); |
|
|
useLocalizedMoment.mockImplementation( () => moment ); |
|
|
useDateWithOffset.mockImplementation( ( date ) => date ); |
|
|
} ); |
|
|
|
|
|
test( 'Allows both forward and backward navigation between the oldest date and the present (inclusive)', () => { |
|
|
const today = moment(); |
|
|
const aDayAgo = moment( today ).subtract( 1, 'day' ); |
|
|
const aWeekAgo = moment( today ).subtract( 1, 'week' ); |
|
|
const threeMonthsAgo = moment( today ).subtract( 3, 'months' ); |
|
|
const aYearAgo = moment( today ).subtract( 1, 'year' ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const canGoToDate = useCanGoToDate( 0, aWeekAgo, aYearAgo ); |
|
|
|
|
|
expect( canGoToDate( today ) ).toEqual( true ); |
|
|
expect( canGoToDate( aDayAgo ) ).toEqual( true ); |
|
|
expect( canGoToDate( threeMonthsAgo ) ).toEqual( true ); |
|
|
expect( canGoToDate( aYearAgo ) ).toEqual( true ); |
|
|
} ); |
|
|
|
|
|
test( "Allows only backward navigation if we're in the future", () => { |
|
|
const oneWeekInTheFuture = moment().add( 1, 'week' ); |
|
|
|
|
|
const thePreviousDay = moment( oneWeekInTheFuture ).subtract( 1, 'day' ); |
|
|
const theNextDay = moment( oneWeekInTheFuture ).add( 1, 'day' ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const canGoToDate = useCanGoToDate( 0, oneWeekInTheFuture ); |
|
|
|
|
|
|
|
|
|
|
|
expect( canGoToDate( thePreviousDay ) ).toEqual( true ); |
|
|
expect( canGoToDate( theNextDay ) ).toEqual( false ); |
|
|
} ); |
|
|
|
|
|
test( "Allows only forward navigation if we're further back than the oldest date", () => { |
|
|
const oldestDate = moment( '2011-05-21' ); |
|
|
|
|
|
const aWeekBeforeThat = moment( oldestDate ).subtract( 1, 'week' ); |
|
|
const thePreviousDay = moment( aWeekBeforeThat ).subtract( 1, 'day' ); |
|
|
const theNextDay = moment( aWeekBeforeThat ).add( 1, 'day' ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const canGoToDate = useCanGoToDate( 0, aWeekBeforeThat, oldestDate ); |
|
|
|
|
|
|
|
|
|
|
|
expect( canGoToDate( thePreviousDay ) ).toEqual( false ); |
|
|
expect( canGoToDate( theNextDay ) ).toEqual( true ); |
|
|
} ); |
|
|
|
|
|
test( 'Does not allow forward navigation to dates in the future', () => { |
|
|
const anArbitraryDate = moment( '2011-05-21' ); |
|
|
|
|
|
const today = moment(); |
|
|
const tomorrow = moment( today ).add( 1, 'day' ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const canGoToDate = useCanGoToDate( 0, anArbitraryDate ); |
|
|
|
|
|
expect( canGoToDate( today ) ).toEqual( true ); |
|
|
expect( canGoToDate( tomorrow ) ).toEqual( false ); |
|
|
} ); |
|
|
|
|
|
test( 'Does not allow backward navigation prior to the oldest date available', () => { |
|
|
const today = moment(); |
|
|
const oneWeekAgo = moment( today ).subtract( 1, 'week' ); |
|
|
const aDayBeforeThat = moment( oneWeekAgo ).subtract( 1, 'day' ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const canGoToDate = useCanGoToDate( 0, today, oneWeekAgo ); |
|
|
|
|
|
expect( canGoToDate( oneWeekAgo ) ).toEqual( true ); |
|
|
expect( canGoToDate( aDayBeforeThat ) ).toEqual( false ); |
|
|
} ); |
|
|
|
|
|
test( 'Always allows backward navigation if no oldest date is known', () => { |
|
|
const theUnixEpoch = moment( 0 ); |
|
|
|
|
|
const canGoToDate = useCanGoToDate( 0, moment() ); |
|
|
|
|
|
expect( canGoToDate( theUnixEpoch ) ).toEqual( true ); |
|
|
} ); |
|
|
|
|
|
test( 'Allows backward navigation to one day past the number of visible days', () => { |
|
|
const today = moment().startOf( 'day' ); |
|
|
|
|
|
const canGoToDate = useCanGoToDate( 0, today ); |
|
|
|
|
|
const yesterday = moment( today ).subtract( 1, 'day' ); |
|
|
expect( canGoToDate( yesterday ) ).toEqual( true ); |
|
|
} ); |
|
|
} ); |
|
|
|