|
| 1 | +import _ from 'lodash'; |
| 2 | +import React from 'react'; |
| 3 | +import {render /* , act, waitFor */} from '@testing-library/react-native'; |
| 4 | +import {Colors} from '../../../style'; |
| 5 | +import WheelPicker from '../index'; |
| 6 | +import {WheelPickerDriver} from '../WheelPicker.driver'; |
| 7 | +import {WheelPickerItemDriver} from '../WheelPickerItem.driver'; |
| 8 | + |
| 9 | +const ITEM_HEIGHT = 50; |
| 10 | +const NUM_OF_ROWS = 10; |
| 11 | +const testID = 'wheel'; |
| 12 | +const onChange = jest.fn(); |
| 13 | + |
| 14 | +const TestCase = props => { |
| 15 | + return ( |
| 16 | + <WheelPicker |
| 17 | + testID={testID} |
| 18 | + items={_.times(60, i => i).map(item => ({label: `item #${item}`, value: item, testID: `${item}`}))} |
| 19 | + initialValue={0} |
| 20 | + onChange={onChange} |
| 21 | + numberOfVisibleRows={NUM_OF_ROWS} |
| 22 | + itemHeight={ITEM_HEIGHT} |
| 23 | + activeTextColor={Colors.red30} |
| 24 | + inactiveTextColor={Colors.blue30} |
| 25 | + {...props} |
| 26 | + /> |
| 27 | + ); |
| 28 | +}; |
| 29 | + |
| 30 | +describe('WheelPicker', () => { |
| 31 | + beforeEach(() => { |
| 32 | + onChange.mockClear(); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('FlatList', () => { |
| 36 | + it('should present $NUM_OF_ROWS rows', () => { |
| 37 | + const renderTree = render(<TestCase/>); |
| 38 | + const driver = WheelPickerDriver({renderTree, testID}); |
| 39 | + expect(driver.getListHeight()).toBe(NUM_OF_ROWS * ITEM_HEIGHT); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should call onChange after scrolling ends with default itemHeight and numberOfRows', () => { |
| 43 | + const props = {itemHeight: undefined, numberOfVisibleRows: undefined}; |
| 44 | + const renderTree = render(<TestCase {...props}/>); |
| 45 | + const driver = WheelPickerDriver({renderTree, testID}); |
| 46 | + |
| 47 | + driver.moveToItem(4); |
| 48 | + expect(onChange).toHaveBeenCalledWith(4, 4); |
| 49 | + |
| 50 | + driver.moveToItem(7); |
| 51 | + expect(onChange).toHaveBeenCalledWith(7, 7); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should call onChange after scrolling ends', () => { |
| 55 | + const renderTree = render(<TestCase/>); |
| 56 | + const driver = WheelPickerDriver({renderTree, testID}); |
| 57 | + |
| 58 | + driver.moveToItem(4, ITEM_HEIGHT); |
| 59 | + expect(onChange).toHaveBeenCalledWith(4, 4); |
| 60 | + |
| 61 | + driver.moveToItem(7, ITEM_HEIGHT); |
| 62 | + expect(onChange).toHaveBeenCalledWith(7, 7); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe('initialValue', () => { |
| 67 | + it('should not call onChange when initialValue is updated', () => { |
| 68 | + const renderTree = render(<TestCase/>); |
| 69 | + renderTree.rerender(<TestCase initialValue={2}/>); |
| 70 | + expect(onChange).not.toHaveBeenCalled(); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('label', () => { |
| 75 | + it('should return label', () => { |
| 76 | + const label = 'Hours'; |
| 77 | + const renderTree = render(<TestCase label={label}/>); |
| 78 | + const driver = WheelPickerDriver({renderTree, testID}); |
| 79 | + expect(driver.getLabel()).toEqual(label); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('PickerItem', () => { |
| 84 | + it('should get first item\'s label', () => { |
| 85 | + const renderTree = render(<TestCase/>); |
| 86 | + const index = 0; |
| 87 | + const driver = WheelPickerItemDriver({renderTree, testID: `${index}`}); |
| 88 | + expect(driver.getLabel()).toEqual('item #0'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should get first item\'s text style when no active/inactive colors', () => { |
| 92 | + const renderTree = render(<TestCase textStyle={{color: Colors.green30}}/>); |
| 93 | + const index = 0; |
| 94 | + const driver = WheelPickerItemDriver({renderTree, testID: `${index}`}); |
| 95 | + expect(driver.getLabelStyle()?.color).toEqual(Colors.green30); |
| 96 | + }); |
| 97 | + |
| 98 | + //TODO: Fix these test's using AnimatedStyle mocking |
| 99 | + // it('should call onChange after second item is pressed', async () => { |
| 100 | + // const renderTree = render(<TestCase/>); |
| 101 | + // const index = 1; |
| 102 | + // const driver = WheelPickerItemDriver({renderTree, testID: `${index}`}); |
| 103 | + |
| 104 | + // driver.press(); |
| 105 | + |
| 106 | + // expect(await onChange).toHaveBeenCalledTimes(1); |
| 107 | + // expect(onChange).toHaveBeenCalledWith(1); |
| 108 | + // }); |
| 109 | + |
| 110 | + // it('should not call onChange after first item is pressed', async () => { |
| 111 | + // const renderTree = render(<TestCase/>); |
| 112 | + // const index = 0; |
| 113 | + // const driver = WheelPickerItemDriver({renderTree, testID: `${index}`}); |
| 114 | + |
| 115 | + // driver.press(); |
| 116 | + |
| 117 | + // expect(onChange).not.toHaveBeenCalledTimes(1); |
| 118 | + // }); |
| 119 | + }); |
| 120 | +}); |
0 commit comments