最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - React userEvent.type not typing - Stack Overflow

programmeradmin3浏览0评论

I can't figure out why userEvent.type is not actually typing. I've used async/await. I even tried to use ".then()" but it just skips that code block. The actual application works fine. I can type into the field and state is updated.

I'm trying to test a MaterialUI TextField:

<TextField
     data-testid="productName-field"
     label="Product Name"
     style={styles.textField}
     required
     id="productName"
     onChange={n => setName(n.target.value)}
     value={name}
/>

My test is here:

it("Should allow typing in the name field", async () => {
    nameField = screen.getByTestId("productName-field");
    const yourName = "your name here";
    await userEvent.type(nameField,yourName);
    expect(nameField.value).toBe(yourName);
});

from the console:

    expect(received).toBe(expected) // Object.is equality                                                                                                                                                               
                                                                                                                                                                                                                        
    Expected: "your name here"                                                                                                                                                                                          
    Received: undefined                                                                                                                                                                                                 
                                                                                                                                                                                                                        
      54 |         const yourName = "your name here";                                                                                                                                                                   
      55 |         await userEvent.type(nameField,yourName);
    > 56 |         expect(nameField.value).toBe(yourName);
         |                                 ^
      57 |     });

I've got a similar TextField in two other components and the same tests in different files and they are all passing.

发布评论

评论列表(0)

  1. 暂无评论