Day 16 偷偷摸鱼放个waitFor

import { render, screen, waitFor } from '@testing-library/react';
import { rest } from 'msw';
import News from '../News';
import { server } from '../../../mocks/server';

describe('<Users />', () => {
  test('should component show error message', async () => {
    server.resetHandlers(
      rest.get('http://localhost:5000/news', (req, res, ctx) => {
        return res(ctx.status(400));
      })
    );

    render(<News />);

    await waitFor(async () => {
      const error = await screen.findByText(/SERVER ERROR/i);
      expect(error).toBeInTheDocument();
    });
  });
});

我们可以使用waitFor的方式等待所有非同步事件完成并触发callback,解释如下

waitFor may run the callback a number of times until the timeout is reached. Note that the number of calls is constrained by the timeout and interval options.

This can be useful if you have a unit test that mocks API calls and you need to wait for your mock promises to all resolve.


<<:  甘特图

>>:  Angular建立专案(一)(Day16)

【Day14】[资料结构]-二元树走访Binary Tree Traversal

二元树走访或称二元树遍历,简单来说就是走访树中各节点,转化为线性关系。 主要分成两种策略方式 深度优...

Day 12: ML APIs | Google Cloud

Practice Quest-Integrate with Machine Learning API...

[Day 3] 取得台股资料(基本篇)

一、前言 想要进行资料分析,要做的第一件事当然是收集资料,所幸现在是2021,我们不需要为了股票资料...

浅谈Web应用系统安全 - 骇客攻防战

跨站脚本攻击(XSS) 攻击 XXS就是透过网页没有适当筛选、处理文字造成的漏洞 例如有用户将<...

Linux 移动档案夹

使用 mv 来更改档案名称 mv test1.txt test2.txt 使用 mv 来移动整个资料...