我正在尝试使用测试库在 fireEvent.click 之后检查 DOM 元素。我知道我需要在 fireEvent 之后等待,但不知道为什么简单地使用 await 不起作用?下面是用两种方式编写的相同测试——第一个失败,第二个通过。我不明白为什么第一个失败......非常感谢任何见解!
ps——我知道wait已被弃用,waitFor是首选,但是由于一些限制,我目前无法更新版本:(
失败的测试
// This test fails with the following error and warning:
// Error: Unable to find an element by: [data-test="name_wrapper"]
// Warning: An update to OnlinePaymentModule inside a test was not wrapped in act(...).
it('this is a failing test...why', async () => {
const { getByText, getByTestId } = render(<Modal {...props} />);
const button = getByText('open modal');
fireEvent.click(button);
const nameWrapper = await getByTestId('name_wrapper');
expect(
nameWrapper.getElementsByTagName('output')[0].textContent
).toBe('Jon Doe');
const numberWrapper = …Run Code Online (Sandbox Code Playgroud)我可以在 knex 查询中使用变量吗?select usr_vote.vote where usr_vote.user.id = ${loggedInUserId}db.raw( )有什么问题吗?其他一切都很好。
在现在不起作用的 db.raw 中,我尝试使用变量(loggedInUserId)来获取登录用户对该问题的投票历史记录(他们可以投票/反对,因此该值是 -1 或 1 或无效的)。预先感谢您的任何帮助!
有 4 个表,如下所示:
Askify_用户
询问问题
Askify_answers
Askify_问题_投票
getAllQuestions(db, loggedInUserId) {
return db
.from('askify_questions AS q')
.select(
'q.id AS question_id',
'q.title AS question_title',
'q.body AS question_body',
'q.date_created AS date_created',
'q.tags',
db.raw(
`count(DISTINCT ans) AS number_of_answers`
),
db.raw(
`SUM(DISTINCT usr_vote.vote) AS sum_of_votes`
), …Run Code Online (Sandbox Code Playgroud)