【问题标题】:Get the selected value of Native Base Select component in test获取测试中 Native Base Select 组件的选中值
【发布时间】:2022-12-15 16:28:22
【问题描述】:

我正在尝试从 Native Base 测试我的 Select 组件,在测试中我从列表中选择一个项目或直接触发带有值的 onValueChange 道具然后断言所选值,但我找不到道具保存选定的值,我试过调试、记录组件,但我看不到哪个道具保存该值,它应该是 selectedValue,但我无法获取它。

我选择的一个简短的 sn-p 由 React Hook Forms 管理,我的测试使用 React Native Testing Library 和 Jest

 <Controller
              control={control}
              render={({ field: { onChange, onBlur, value } }) => (
                <Select
                  selectedValue={value}
                  onValueChange={onChange}
                  minWidth='200'
                  accessibilityLabel={t('typePlaceholder')}
                  placeholder={t('typePlaceholder')}
                  _selectedItem={{
                    bg: 'teal.600',
                    endIcon: <CheckIcon size={5} />,
                  }}
                  mt='1'
                  testID='type-select'
                >
                  {FUTURE_EXPENSE_TYPE_LIST.map(({ id, label, type }) => (
                    <Select.Item
                      label={t(label)}
                      value={type}
                      key={id}
                      testID={`${label}-id`}
                    />
                  ))}
                </Select>
              )}
              name='type'
            />

我的测试:

 it.only('test', async () => {
    const futureExpensesAndGoalsPage = defaultNativeBaseWrapper(
      <FutureExpensesAndGoals />
    );
    fireEvent.press(
      futureExpensesAndGoalsPage.getByText(
        'enableFutureExpenseAndGoalsFormButtonLabel'
      )
    );

    fireEvent(
      futureExpensesAndGoalsPage.getByPlaceholderText('typePlaceholder'),
      'onValueChange',
      'goals'
    );

    expect(
      futureExpensesAndGoalsPage.getByPlaceholderText('typePlaceholder').props
        .value
    ).toBe('goals');

    futureExpensesAndGoalsPage.debug();

    console.log(
      'test',
      futureExpensesAndGoalsPage.getByTestId('debt-id').props
    );
  });

【问题讨论】:

    标签: react-native jestjs expo native-base react-native-testing-library


    【解决方案1】:

    你有没有尝试过:

    onValueChange={(value) =&gt; onChange(value)}

    【讨论】:

      猜你喜欢
      • 2018-05-04
      • 1970-01-01
      • 1970-01-01
      • 2019-04-16
      • 1970-01-01
      • 1970-01-01
      • 2019-01-15
      • 2021-11-10
      • 2021-05-23
      相关资源
      最近更新 更多