xgfe / react-native-datepicker

react native datePicker component for both Android and IOS, useing DatePikcerAndroid, TimePickerAndroid and DatePickerIOS
MIT License
2.12k stars 725 forks source link

simulate onDateChange in tests #203

Open mateen-hussain opened 6 years ago

mateen-hussain commented 6 years ago

Issue

I'm unable to simulate an onDateChange or onPress event to test my component.

Expected Behavior

Javascript

Component

<View style={styles.datePickerCard}>
        <Text h4 style={styles.cardHeader}>Pick a Date</Text>
        <DatePicker
            mode="date"
            placeholder="select date"
            date={selectedDateLocal}
            format="LL"
            minDate={new Date()}
            confirmBtnText="Confirm"
            cancelBtnText="Cancel"
            androidMode="calendar"
            onDateChange={(date) => { setSelectedDate(CentreId, date); }}
            customStyles={{
                btnTextConfirm: {
                    color: primaryColor,
                },
                btnTextCancel: {
                    color: secondaryColor,
                },
                dateInput: {
                    height: 40,
                    borderWidth: 0,
                    width: 500,
                },
            }}
            style={styles.datePicker}
        />
    </View>

Jest

      // act
        const tree = shallow(<DatePickerCard
            setSelectedDate={setSelectedDate}
            CentreId={CentreId}
        selectedDateLocal={selectedDateLocal}
        />);
      const datePicker = tree.find('DatePicker').simulate('onPressConfirm');
        expect(setSelectedDate).toHaveBeenCalledTimes(1);
    });

Environment

  1. react: "^16.0.0-beta.5"
  2. react-native -v: ^0.49.5
  3. enzyme: "^3.1.0"
feyy commented 6 years ago

I think simulate only trigger event. You can call the onPressConfirm function directly with the instance.