import { RenderResult } from '@testing-library/react-hooks' ; import es from 'date-fns/locale/es' ; import { customRenderHook } from 'test/render' ; import { freezeBeforeAll } from 'test/utils' ; import { CaptionLayout } from 'components/Caption' ; import { DayPickerContextValue , useDayPicker } from 'contexts/DayPicker' ; import { getDefaultContextValue } from 'contexts/DayPicker/defaultContextValue' ; import { Components , DayPickerProps } from 'types/DayPicker' ; import { Formatters } from 'types/Formatters' ; import { Labels } from 'types/Labels' ; import { DayModifiers , ModifiersClassNames } from 'types/Modifiers' ; import { ClassNames , Styles } from 'types/Styles' ; const today = new Date ( 2022 , 5 , 13 ) ; const defaults = getDefaultContextValue ( ) ; freezeBeforeAll ( today ) ; let renderResult ; function setup ( dayPickerProps ? ) { const { result } = customRenderHook ( ( ) => useDayPicker ( ) , dayPickerProps ) ; renderResult = result ; } describe ( 'when passing rendered without props' , ( ) => { type PropName = 'captionLayout' | 'classNames' | 'components' | 'formatters' | 'labels' | 'locale' | 'modifiersClassNames' | 'modifierPrefix' | 'modifiers' | 'numberOfMonths' | 'styles' | 'today' ; const testPropNames = [ 'captionLayout' , 'classNames' , 'components' , 'formatters' , 'labels' , 'locale' , 'modifiersClassNames' , 'modifierPrefix' , 'modifiers' , 'numberOfMonths' , 'styles' ] ; beforeAll ( ( ) => { setup ( ) ; } ) ; test . each ( testPropNames ) ( 'should use the %s default value' , ( propName ) => { expect ( renderResult . current [ propName ] ) . toEqual ( defaults [ propName ] ) ; } ) ; } ) ; describe ( 'when passing "locale" from props' , ( ) => { const locale = es ; const dayPickerProps = { locale } ; beforeEach ( ( ) => { setup ( dayPickerProps ) ; } ) ; test ( 'should override the default locale' , ( ) => { expect ( renderResult . current . locale ) . not . toBe ( defaults . locale ) ; } ) ; test ( 'should return the custom locale' , ( ) => { expect ( renderResult . current . locale ) . toBe ( locale ) ; } ) ; } ) ; describe ( 'when passing "numberOfMonths" from props' , ( ) => { const numberOfMonths = 4 ; const dayPickerProps = { numberOfMonths } ; beforeEach ( ( ) => { setup ( dayPickerProps ) ; } ) ; test ( 'should override the default numberOfMonths' , ( ) => { expect ( renderResult . current . numberOfMonths ) . not . toBe ( defaults . numberOfMonths ) ; } ) ; test ( 'should return the custom numberOfMonths' , ( ) => { expect ( renderResult . current . numberOfMonths ) . toBe ( 4 ) ; } ) ; } ) ; describe ( 'when passing "today" from props' , ( ) => { const today = new Date ( 2010 , 9 , 11 ) ; const dayPickerProps = { today } ; beforeEach ( ( ) => { setup ( dayPickerProps ) ; } ) ; test ( 'should override the default "today"' , ( ) => { expect ( renderResult . current . today ) . not . toBe ( defaults . today ) ; } ) ; test ( 'should return the custom "today"' , ( ) => { expect ( renderResult . current . today ) . toBe ( today ) ; } ) ; } ) ; describe ( 'when passing "captionLayout" from props' , ( ) => { const captionLayout = 'dropdown' ; const fromYear = 2000 ; const toYear = 2010 ; const dayPickerProps = { captionLayout , fromYear , toYear } ; beforeEach ( ( ) => { setup ( dayPickerProps ) ; } ) ; test ( 'should override the default "captionLayout"' , ( ) => { expect ( renderResult . current . captionLayout ) . not . toBe ( defaults . captionLayout ) ; } ) ; test ( 'should return the custom "captionLayout"' , ( ) => { expect ( renderResult . current . captionLayout ) . toBe ( captionLayout ) ; } ) ; } ) ; describe ( 'when "fromDate" and "toDate" are undefined' , ( ) => { const fromDate = undefined ; const toDate = undefined ; describe ( 'when using "dropdown" as "captionLayout"' , ( ) => { const captionLayout = 'dropdown' ; beforeEach ( ( ) => { setup ( { fromDate , toDate , captionLayout } ) ; } ) ; test ( 'should return "buttons" as "captionLayout"' , ( ) => { expect ( renderResult . current . captionLayout ) . toBe ( 'buttons' ) ; } ) ; } ) ; } ) ; describe ( 'when "fromDate" is undefined, but not "toDate"' , ( ) => { const fromDate = undefined ; const toDate = new Date ( ) ; describe ( 'when using "dropdown" as "captionLayout"' , ( ) => { const captionLayout = 'dropdown' ; beforeEach ( ( ) => { setup ( { fromDate , toDate , captionLayout } ) ; } ) ; test ( 'should return "buttons" as "captionLayout"' , ( ) => { expect ( renderResult . current . captionLayout ) . toBe ( 'buttons' ) ; } ) ; } ) ; } ) ; describe ( 'when "toDate" is undefined, but not "fromDate"' , ( ) => { const fromDate = new Date ( ) ; const toDate = undefined ; describe ( 'when using "dropdown" as "captionLayout"' , ( ) => { const captionLayout = 'dropdown' ; beforeEach ( ( ) => { setup ( { fromDate , toDate , captionLayout } ) ; } ) ; test ( 'should return "buttons" as "captionLayout"' , ( ) => { expect ( renderResult . current . captionLayout ) . toBe ( 'buttons' ) ; } ) ; } ) ; } ) ; describe ( 'when using "dropdown" as "captionLayout"' , ( ) => { const captionLayout = 'dropdown' ; const fromYear = 2000 ; const toYear = 2010 ; const dayPickerProps = { captionLayout , fromYear , toYear } ; beforeEach ( ( ) => { setup ( dayPickerProps ) ; } ) ; test ( 'should override the default "captionLayout"' , ( ) => { expect ( renderResult . current . captionLayout ) . not . toBe ( defaults . captionLayout ) ; } ) ; test ( 'should return the custom "captionLayout"' , ( ) => { expect ( renderResult . current . captionLayout ) . toBe ( captionLayout ) ; } ) ; } ) ; describe ( 'when passing "modifierPrefix" from props' , ( ) => { const modifierPrefix = 'foo-' ; const dayPickerProps = { modifierPrefix } ; beforeEach ( ( ) => { setup ( dayPickerProps ) ; } ) ; test ( 'should override the default "modifierPrefix"' , ( ) => { expect ( renderResult . current . modifierPrefix ) . not . toBe ( defaults . modifierPrefix ) ; } ) ; test ( 'should return the custom "modifierPrefix"' , ( ) => { expect ( renderResult . current . modifierPrefix ) . toBe ( modifierPrefix ) ; } ) ; } ) ; describe ( 'when passing "modifiers" from props' , ( ) => { const modifiers = { foo : new Date ( ) } ; const dayPickerProps = { modifiers } ; beforeEach ( ( ) => { setup ( dayPickerProps ) ; } ) ; test ( 'should override the default "modifiers"' , ( ) => { expect ( renderResult . current . modifiers ) . not . toBe ( defaults . modifiers ) ; } ) ; test ( 'should return the custom "modifiers"' , ( ) => { expect ( renderResult . current . modifiers ) . toStrictEqual ( modifiers ) ; } ) ; } ) ; describe ( 'when passing "modifiersClassNames" from props' , ( ) => { const modifiersClassNames = { foo : 'bar' } ; const dayPickerProps = { modifiersClassNames } ; beforeEach ( ( ) => { setup ( dayPickerProps ) ; } ) ; test ( 'should override the default "modifiersClassNames"' , ( ) => { expect ( renderResult . current . modifiersClassNames ) . not . toBe ( defaults . modifiersClassNames ) ; } ) ; test ( 'should return the custom "modifiersClassNames"' , ( ) => { expect ( renderResult . current . modifiersClassNames ) . toStrictEqual ( modifiersClassNames ) ; } ) ; } ) ; describe ( 'when passing "styles" from props' , ( ) => { const styles = { caption : { color : 'red ' } } ; const dayPickerProps = { styles } ; beforeEach ( ( ) => { setup ( dayPickerProps ) ; } ) ; test ( 'should override the default "styles"' , ( ) => { expect ( renderResult . current . styles ) . not . toBe ( defaults . styles ) ; } ) ; test ( 'should include the custom "styles"' , ( ) => { expect ( renderResult . current . styles ) . toStrictEqual ( { ... defaults . styles , ... styles } ) ; } ) ; } ) ; describe ( 'when passing "classNames" from props' , ( ) => { const classNames = { caption : 'foo' } ; const dayPickerProps = { classNames } ; beforeEach ( ( ) => { setup ( dayPickerProps ) ; } ) ; test ( 'should override the default "classNames"' , ( ) => { expect ( renderResult . current . classNames ) . not . toBe ( defaults . classNames ) ; } ) ; test ( 'should include the custom "classNames"' , ( ) => { expect ( renderResult . current . classNames ) . toStrictEqual ( { ... defaults . classNames , ... classNames } ) ; } ) ; } ) ; describe ( 'when passing "formatters" from props' , ( ) => { const formatters = { formatCaption : jest . fn ( ) } ; const dayPickerProps = { formatters } ; beforeEach ( ( ) => { setup ( dayPickerProps ) ; } ) ; test ( 'should override the default "formatters"' , ( ) => { expect ( renderResult . current . formatters ) . not . toBe ( defaults . formatters ) ; } ) ; test ( 'should include the custom "formatters"' , ( ) => { expect ( renderResult . current . formatters ) . toStrictEqual ( { ... defaults . formatters , ... formatters } ) ; } ) ; } ) ; describe ( 'when passing "labels" from props' , ( ) => { const labels = { labelDay : jest . fn ( ) } ; const dayPickerProps = { labels } ; beforeEach ( ( ) => { setup ( dayPickerProps ) ; } ) ; test ( 'should override the default "labels"' , ( ) => { expect ( renderResult . current . labels ) . not . toBe ( defaults . labels ) ; } ) ; test ( 'should include the custom "labels"' , ( ) => { expect ( renderResult . current . labels ) . toStrictEqual ( { ... defaults . labels , ... labels } ) ; } ) ; } ) ; describe ( 'when passing "components" from props' , ( ) => { const components = { Day : jest . fn ( ) } ; const dayPickerProps = { components } ; beforeEach ( ( ) => { setup ( dayPickerProps ) ; } ) ; test ( 'should override the default "components"' , ( ) => { expect ( renderResult . current . components ) . not . toBe ( defaults . components ) ; } ) ; test ( 'should include the custom "components"' , ( ) => { expect ( renderResult . current . components ) . toStrictEqual ( { ... defaults . components , ... components } ) ; } ) ; } ) ;