|
| 1 | +import React from 'react'; |
| 2 | +import CalendarList from '../index'; |
| 3 | +import {CalendarListDriver} from '../driver'; |
| 4 | +//@ts-expect-error |
| 5 | +import {getMonthTitle} from '../../testUtils'; |
| 6 | + |
| 7 | +const CURRENT = '2022-09-09'; |
| 8 | +const NEXT_MONTH = '2022-10-09'; |
| 9 | +const PREV_MONTH = '2022-08-09'; |
| 10 | +const nextMonthData = {dateString: '2022-10-09', day: 9, month: 10, timestamp: 1665273600000, year: 2022}; |
| 11 | +const prevMonthData = {dateString: '2022-08-09', day: 9, month: 8, timestamp: 1660003200000, year: 2022}; |
| 12 | + |
| 13 | +const testIdCalendarList = 'myCalendarList'; |
| 14 | +const onMonthChangeMock = jest.fn(); |
| 15 | +const onVisibleMonthsChangeMock = jest.fn(); |
| 16 | +const pastScrollRange = 10; |
| 17 | +const futureScrollRange = 5; |
| 18 | +// const initialVisibleItems = [ |
| 19 | +// { |
| 20 | +// "index": 50, |
| 21 | +// "isViewable": true, |
| 22 | +// "item": "2022-09-09T00:00:00.000Z", |
| 23 | +// "key": "50" |
| 24 | +// } |
| 25 | +// ]; |
| 26 | +// const changed = [ |
| 27 | +// { |
| 28 | +// "index": 51, |
| 29 | +// "isViewable": true, |
| 30 | +// "item": "2022-10-09T00:00:00.000Z", |
| 31 | +// "key": "51" |
| 32 | +// }, |
| 33 | +// { |
| 34 | +// "index": 50, |
| 35 | +// "isViewable": false, |
| 36 | +// "item": "2022-09-09T00:00:00.000Z", |
| 37 | +// "key":"50" |
| 38 | +// } |
| 39 | +// ]; |
| 40 | +// const visibleItems = [ |
| 41 | +// { |
| 42 | +// "index": 51, |
| 43 | +// "isViewable": true, |
| 44 | +// "item": "2022-10-09T00:00:00.000Z", |
| 45 | +// "key": "51" |
| 46 | +// } |
| 47 | +// ]; |
| 48 | + |
| 49 | +const defaultProps = { |
| 50 | + testID: testIdCalendarList, |
| 51 | + current: CURRENT, |
| 52 | + onMonthChange: onMonthChangeMock, |
| 53 | + onVisibleMonthsChange: onVisibleMonthsChangeMock |
| 54 | +}; |
| 55 | + |
| 56 | +const TestCase = props => { |
| 57 | + return <CalendarList {...defaultProps} {...props} />; |
| 58 | +}; |
| 59 | + |
| 60 | +describe('CalendarList', () => { |
| 61 | + describe('Props', () => { |
| 62 | + describe('past/futureScrollRange', () => { |
| 63 | + const driver = new CalendarListDriver( |
| 64 | + testIdCalendarList, |
| 65 | + <TestCase pastScrollRange={pastScrollRange} futureScrollRange={futureScrollRange} /> |
| 66 | + ); |
| 67 | + |
| 68 | + beforeEach(() => { |
| 69 | + jest.useFakeTimers(); |
| 70 | + driver.render(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should have correct number of list items', () => { |
| 74 | + expect(driver.getListProps().data.length).toBe(pastScrollRange + futureScrollRange + 1); |
| 75 | + }); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('Horizontal Mode', () => { |
| 80 | + const driver = new CalendarListDriver(testIdCalendarList, <TestCase horizontal={true} staticHeader={true} />); |
| 81 | + |
| 82 | + beforeEach(() => { |
| 83 | + jest.useFakeTimers(); |
| 84 | + driver.render(); |
| 85 | + |
| 86 | + onMonthChangeMock.mockClear(); |
| 87 | + onVisibleMonthsChangeMock.mockClear(); |
| 88 | + }); |
| 89 | + |
| 90 | + // afterEach(() => driver.clear()); |
| 91 | + |
| 92 | + describe('Init', () => { |
| 93 | + it('should display current month', () => { |
| 94 | + // static header |
| 95 | + expect(driver.getStaticHeaderTitle()).toBe(getMonthTitle(CURRENT)); |
| 96 | + |
| 97 | + // list |
| 98 | + expect(driver.getListProps().horizontal).toBe(true); |
| 99 | + expect(driver.getListProps().data.length).toBe(101); |
| 100 | + expect(driver.getListProps().initialScrollIndex).toBe(50); |
| 101 | + expect(driver.getListProps().initialNumToRender).toBe(1); |
| 102 | + |
| 103 | + // list items |
| 104 | + expect(driver.getListItem(CURRENT)).toBeDefined(); |
| 105 | + expect(driver.getListItemTitle(CURRENT)).toBeDefined(); |
| 106 | + |
| 107 | + // events |
| 108 | + expect(onMonthChangeMock).not.toHaveBeenCalled(); |
| 109 | + expect(onVisibleMonthsChangeMock).not.toHaveBeenCalled(); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe('Static Header Arrows', () => { |
| 114 | + it('should change month on right arrow press', () => { |
| 115 | + driver.pressRightArrow(); |
| 116 | + |
| 117 | + expect(onMonthChangeMock).toHaveBeenCalledWith(nextMonthData); |
| 118 | + expect(onVisibleMonthsChangeMock).toHaveBeenCalledWith([nextMonthData]); |
| 119 | + |
| 120 | + expect(driver.getStaticHeaderTitle()).toBe(getMonthTitle(NEXT_MONTH)); |
| 121 | + |
| 122 | + // NOTE: check visible list item - only first item is rendered and arrow press doesn't actually scrolls the list |
| 123 | + // expect(driver.getListItemTitle(NEXT_MONTH)).toBeDefined(); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should change month on left arrow press', () => { |
| 127 | + driver.pressLeftArrow(); |
| 128 | + |
| 129 | + expect(onMonthChangeMock).toHaveBeenCalledWith(prevMonthData); |
| 130 | + expect(onVisibleMonthsChangeMock).toHaveBeenCalledWith([prevMonthData]); |
| 131 | + |
| 132 | + expect(driver.getStaticHeaderTitle()).toBe(getMonthTitle(PREV_MONTH)); |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + // describe('List Scroll', () => { |
| 137 | + // it('scroll to next month', () => { |
| 138 | + // driver.fireOnViewableItemsChanged(changed, visibleItems); |
| 139 | + |
| 140 | + // expect(onMonthChangeMock).toHaveBeenCalled(); |
| 141 | + // expect(onMonthChangeMock).toHaveBeenCalledWith(nextMonthData); |
| 142 | + // expect(onVisibleMonthsChangeMock).toHaveBeenCalledWith([nextMonthData]); |
| 143 | + |
| 144 | + // expect(driver.getStaticHeaderTitle()).toBe(getMonthTitle(NEXT_MONTH)); |
| 145 | + // }); |
| 146 | + // }); |
| 147 | + }); |
| 148 | +}); |
0 commit comments