小编Jav*_*kie的帖子

React 测试库不更新状态

我的组件:

import React from 'react'

const TestAsync = () => {
  const [counter, setCounter] = React.useState(0)

  const delayCount = () => (
    setTimeout(() => {
      setCounter(counter + 1)
    }, 500)
  )
  
return (
  <>
    <h1 data-testid="counter">{ counter }</h1>
    <button data-testid="button-up" onClick={delayCount}> Up</button>
    <button data-testid="button-down" onClick={() => setCounter(counter - 1)}>Down</button>
 </>
    )
  }
  
  export default TestAsync
Run Code Online (Sandbox Code Playgroud)

我的测试文件:

describe("Test async", () => {
  it("increments counter after 0.5s", async () => {
    const { getByTestId, getByText } = render(<TestAsync />);

    fireEvent.click(getByTestId("button-up"));

    const counter …
Run Code Online (Sandbox Code Playgroud)

unit-testing reactjs react-testing-library react-hooks

4
推荐指数
1
解决办法
2万
查看次数

findAndCountAll count 得到的数字比实际返回的数据大

我正在使用 SequelizefindAndCountAll()作为我网站的分页。

返回findAndCountAll()的总项数( 3990)大于实际返回的数据数( 3770)。

有谁知道如何使findAndCountAll()返回的总项目数与实际返回的数据相同?

我使用的查询表findAndCountAll()包括另外两个表,如下面的代码:

const patientModel: IIncludeOptions = {
    model: Patient,
    attributes: ["deviceId", "firstName", "lastName"],
};

const deviceModel: IIncludeOptions = {
    model: Device,
    required: true,
    attributes: ["deviceId", "deviceSerialNumber"],
    include: [patientModel],
};

const eventCodeModel: IIncludeOptions = {
    model: EventCode,
    required: true,
    attributes: ["name"],
};

const opts: IFindOptions = {
    ...pageSizeLimit,
    offset : offsetNum,
    attributes: ["id", "deviceId", "eventId", "dispatchedAt", "message", "createdAt"],
    include: [
        deviceModel,
        eventCodeModel,
    ],
    order: sortBy,
}; …
Run Code Online (Sandbox Code Playgroud)

sequelize.js

3
推荐指数
1
解决办法
2725
查看次数