我的组件有两个类似的表单(两个带有用户名/密码字段的表单)和两个Submit带有文本“提交”的按钮。为了测试提交处理程序,我模拟了表单字段的类型事件,然后单击提交按钮,如下所示:
it('test submit handler', async () => {
const myProvider = ({children}) => <Provider store={store}>{children}</Provider>;
render(<MyComponent />, {wrapper: myProvider});
expect(await screen.findByText('username')).toBeTruthy();
expect(await screen.findByText('password')).toBeTruthy();
await userEvent.type(screen.getByLabelText('username'), 'someusername');
await userEvent.type(screen.getByLabelText('password'), 'somepassword');
fireEvent.click(screen.getByText('Submit'));
await waitFor(() => expect(onSubmit).toBeCalledTimes(1));
});
});
Run Code Online (Sandbox Code Playgroud)
这样做会导致错误TestingLibraryElementError: Found multiple elements with the text: Submit。我查找了这些getBy*方法的文档,看起来它们要么解释了找到的第一个元素,要么在存在多个元素的情况下,它们会抛出错误,这就是这里发生的情况。这两种表单具有不同的处理函数,我想测试这两个函数是否在提交时被调用。访问此Submit按钮第一次出现的好方法是什么?
我尝试用 替换getByText,findByText但这会导致返回Promise { <pending> }, waiting ,这会导致上面提到的相同TestingLibraryElementError错误。
我正在从头开始编写一个简单的 Express 应用程序。我已经使用 mongoose 建立了数据库连接。我现在想要实现的只是简单地使数据库显示在 mongo GUI 上(我同时使用 mongo compass 和 Robo 3T)。这是我的server.js:
import express from 'express'
import mongoose from 'mongoose'
require('dotenv').config()
const server = express()
server.get('/', (req, res) => {
res.send("Hello World");
});
const port = process.env.PORT || 5000;
mongoose.connect(process.env.DB_URL, { useNewUrlParser: true });
mongoose.connection.on('open', function(){
console.log("Mongoose default connection is open to ", process.env.DB_URL)
});
server.listen(port, () => {
console.log(`server running on port ${port}`)
});
Run Code Online (Sandbox Code Playgroud)
看起来DB_URL像这样:DB_URL=mongodb://localhost:27017/my-test-db
正如预期的那样,我得到的输出是 - Mongoose default connection is open to …