小编Srd*_*No1的帖子

使用 React Hooks 重新渲染太多

有人问过类似的问题,但我还没有找到针对这个特定问题的解决方案。我有一个组件可以渲染所有板,我正在使用自定义useFetch钩子来获取所有板。

const BoardsDashboard = () => {

  let [boards, setBoards] = useState([]);

  const { response } = useFetch(routes.BOARDS_INDEX_URL, {});

  setBoards(response);

  return (
    <main className="dashboard">
      <section className="board-group">
        <header>
          <div className="board-section-logo">
            <span className="person-logo"></span>
          </div>
          <h2>Personal Boards</h2>
        </header>

        <ul className="dashboard-board-tiles">
          {boards.map(board => (
            <BoardTile title={board.title} id={board.id} key={board.id} />
          ))}
          <CreateBoardTile />
        </ul>

      </section>
    </main>
  );
};

const useFetch = (url, options) => {
  const [response, setResponse] = useState([]);
  const [error, setError] = useState(null);
  useEffect(() => {
    const fetchData = async () …
Run Code Online (Sandbox Code Playgroud)

fetch reactjs react-hooks

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

`getByRole` 找不到相应文本框的名称

这是针对 的测试:

  it("changes the state when body input is changed", () => {
    render(<CommentForm />)
    // let input = screen.getByLabelText("Your Name");

    let input = screen.getByRole("textbox", { name: "Your Comment" });
    fireEvent.change(input, { target: { value: "MyComment" } });
    expect(input.value).toEqual("MyComment");
  });

Run Code Online (Sandbox Code Playgroud)

使用注释行它可以工作(当我使用 搜索时getByLabelText)。这是当我尝试使用以下命令找到它时得到的结果getByRole

 Unable to find an accessible element with the role "textbox" and name "Your Comment"

    Here are the accessible roles:

      document:

      Name "":
      <body />

      --------------------------------------------------
      generic:

      Name "":
      <div />

      Name "":
      <div
        class="input-group" …
Run Code Online (Sandbox Code Playgroud)

reactjs react-testing-library

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

如何使用 axios / jest 测试失败的请求

我创建了一个非常小的应用程序,如果您传递硬币和数量,它可以计算为某些加密货币支付的总价格。我想测试错误,但我总是收到“收到的承诺已解决而不是被拒绝”。我相信这是因为如果 url 错误 axios 仍然会解决承诺。

我遇到的第二个问题是,我尝试测试 url 是否正确,但我有一个问题,即标头是 url 的一部分,并且我无法弄清楚如何仅测试没有标头的 url 路径。

这些是测试 2 和 3(测试 1 有效)

// getTotalPrice.js
const axios = require("axios");

let symbol = process.argv[2];
let quantity = process.argv[3];

const API = `https://rest.coinapi.io/v1/exchangerate`;
const headers = {headers:{"X-CoinAPI-Key": "MY TOKEN"},};

const getTotalPrice = async (symbol = "BTC", quantity = 1) => {
  try {
  let res = await axios.get(`${API}/${symbol}/USD`, headers);
  let data = res.data;
  return Math.round(data.rate * quantity * 100) / 100;
  } catch(err) {
    console.log(err)
  }
}; …
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing jestjs axios

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

输入类型号有什么作用

我正在使用 React 测试库,按照它的约定,元素应该通过getByRole.

当我输入type="text"角色时textarea,这个角色很好。

现在我已经输入了type="number",错误消息建议我使用spinbutton有效的角色,但对我来说没有意义。

输入类型有不同的作用吗number

javascript accessibility reactjs react-testing-library

5
推荐指数
1
解决办法
5250
查看次数

为什么字符串中的字符没有变异?

def test_problem(str)
  str[3].upcase! # str[3] = str[3].upcase! works
  str
end

p test_problem("hello")
Run Code Online (Sandbox Code Playgroud)

我的问题是为什么String.upcase!哪个是mutating方法不会在上面的情况下改变字符串,但你需要在字符串中重新分配该字符?

ruby string

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

JS 类中的方法 - 为什么代码会抛出错误?

有谁知道为什么在下面的代码示例中,当 ES6 类只是构造函数和原型模式之上的语法糖时,JS 会抛出错误?

// Example 1
class Cat {
  meow() {
    console.log('Meow meow!')
  }
}

let kitty = {};
Object.assign(kitty, Cat.prototype);
kitty.meow(); // TypeError: kitty.meow is not a function
Run Code Online (Sandbox Code Playgroud)

为什么这段代码片段在功能上应该与上面的代码片段相同?

class Cat {

}

Cat.prototype.meow = function() {console.log('Meow meow!')}

let kitty = {};
Object.assign(kitty, Cat.prototype);
kitty.meow(); // Meow meow!
Run Code Online (Sandbox Code Playgroud)

javascript prototype class ecmascript-6

0
推荐指数
1
解决办法
39
查看次数