简体   繁体   English

如何测试条件渲染组件的状态转换

[英]How to test state transition of conditionally rendered components

I have a delete confirmation panel which is disabled by default when then component loads and is shown only when the delete button is clicked我有一个删除确认面板,它在加载组件时默认禁用,仅在单击删除按钮时显示

{this.state.deleteConfirmation && (
    <div id="confirmation">
        <Checkbox
            inputId="deleteBlogConfirmation"
            checked={this.state.confirmation}
            onChange={this.toggleConfirmation}
        ></Checkbox>
        <label
            htmlFor="deleteBlogConfirmation"
            className="p-checkbox-label"
        >
            Are You Sure?
        </label>
        <Button
            label="Yes"
            icon="pi pi-check"
            disabled={!this.state.confirmation}
            onClick={this.props.onDeleteConfirm}
            className="p-button-danger"
        />
        <Button
            label="No"
            icon="pi pi-times"
            onClick={this.hideDeleteConfirmation}
            className="p-button-secondary"
        />
    </div>
)}

the value is true when component loads组件加载时值为真

this.state = {
    confirmation: false,
    deleteConfirmation: false
};

the hideDeleteConformation method hides this panel if user clicks on "No" at confirmation如果用户在确认时单击“否”,hideDeleteConformation 方法将隐藏此面板

hideDeleteConfirmation() {
    this.setState({ deleteConfirmation: false });
}

The test fails when I assert deleteConfirmation to be false with error // , Received: undefined当我断言deleteConfirmation为 false 且错误// , Received: undefined时,测试失败

it("hides delete confirmation panel on clicking no button", () => {
    const mockDialogFn = jest.fn();
    const actionButtons = mount(
        <Router>
            <BlogActionButtons
                rowData={rowData}
                onDeleteConfirm={mockDialogFn}
            />
        </Router>
    );
    actionButtons.find(".p-button-danger").at(0).simulate('click');
    expect(actionButtons.props().deleteConfirmation).toBeTruthy(); // , Received: undefined at this line
    actionButtons.find('.p-button-secondary').at(0).simulate('click');
    expect(actionButtons.props().deleteConfirmation).toBeFalsy();
});

If I switch to如果我切换到

expect(actionButtons.state().deleteConfirmation).toBeTruthy();

I get the error TypeError: Cannot read property 'deleteConfirmation' of null for the same line.我收到错误TypeError: Cannot read property 'deleteConfirmation' of null for the same line。

How do I test that deleteConfirmation changes to true/false again on click of respective buttons.如何在单击相应按钮时再次测试deleteConfirmation更改为 true/false。

.props() gets values by their name, not the function that it calls. .props()通过名称获取值,而不是它调用的函数。 This is what you're looking for:这就是你要找的:

expect(actionButtons.prop('onClick')).toBeTruthy()

EDIT: In order to test, you first click and then assert whether or not the html element(s) actually exist in the DOM.编辑:为了测试,您首先单击然后断言 html 元素是否实际存在于 DOM 中。 Personally, I suggest find ing by component, as opposed to assigned IDs就个人而言,我建议按组件find ,而不是分配 ID

const cancelButton = actionButtons.find(Button).at(1) // might not be correct depending on the rest of your component
cancelButton.prop('onClick')()
const confirmationDomElement = actionButtons.find('#confirmation')
expect(confirmationDomElement.exists()).toEqual(false)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为有条件渲染的组件设置动画? - How to animate conditionally-rendered components? 如何有条件地链接两个反应组件的state - How to link the state of two react components conditionally 在 Array.map()(或有条件地)渲染的 React 组件的 Framer Motion 中未触发退出转换 - Exit transition not triggered in Framer Motion for React components rendered in Array.map() (or conditionally) 不确定如何为条件渲染的 ReactJS 组件编写测试用例 - Not sure how to write a test case for conditionally rendered ReactJS component 如何使用 React 测试库和 jest 测试条件渲染的字符串 - How to test conditionally rendered string using React testing library and jest 有条件地呈现的 Web 组件和样式表链接 - Conditionally Rendered Web Components and Stylesheet Links React 中有条件渲染的组件上的 TailwindCSS 动画 - TailwindCSS animations on conditionally rendered components in React 根据状态更改渲染的组件 - Change rendered components depending on state 如何测试有条件地调用的 fetch API 请求,其结果在 JSX 中有条件地呈现 - How to test conditionally called fetch API request whose results are conditionally rendered in JSX 在 React Js 中测试条件渲染组件 - Test Conditionally Rendered component in React Js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM