Jest 测试打开 react-select 菜单

Jest test open react-select menu

我有一个使用 react-select 的组件。我想测试传递给它的正确道具,以及正确显示的道具。有没有强制菜单在 enzyme/jest 中打开?

您可以强制更改内部 StateManager 组件的状态

const tree = mount(<MyComponent />);
tree.find('Select').find('StateManager').instance().setState({ menuIsOpen: true });
tree.update();

或者,更好的方法是在不打开 Select 菜单的情况下检查组件的属性。这让您可以在测试中更好地抽象出 react-select。

const tree = mount(<MyComponent />);
// Run tests against options prop of Select
// expect(tree.find('Select').props('options')).toHaveLength(10);