首页 > 其他分享 >mocha + enzyme, 或者React Testing Library + Jest 的ajax测试

mocha + enzyme, 或者React Testing Library + Jest 的ajax测试

时间:2022-08-16 16:57:21浏览次数:56  
标签:res Testing ctx Library server ajax api rest Component

// app.test.js import { rest } from 'msw'; import { setupServer } from 'msw/node';
// 模拟请求,和返回数据 const server = setupServer(     rest.get('/api/os/platforms', (req, res, ctx) => {         return res(             ctx.json({                 code: '200',                 data: [],                 message: 'success'             })         );     }),     rest.post('/api/os/create', (req, res, ctx) => {         return res(             ctx.json({                 code: '200',                 data: '',                 message: 'success'             })         );     }) );  
// 生命周期监听 describe('test axios', () => {     beforeAll(() => server.listen());     afterEach(() => server.resetHandlers());     afterAll(() => server.close());     it('1. render Component', () => {         const wrapper = shallow(<Component{...props} />);     }); });   // Component.tsx class Component extends React.PureComponent{     async componentDidMount() {         let result = await api.getData(...);     } }

标签:res,Testing,ctx,Library,server,ajax,api,rest,Component
From: https://www.cnblogs.com/founderswitch/p/16592109.html

相关文章

  • 3 基于forms组件 & ajax实现注册功能 & 头像预览功能
    1基于forms组件设计注册页面register.html{%loadstatic%}<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title><......
  • Javaweb08-Ajax项目-分页条件查询 + 增删改
    1、登录1.1登录form表单<formaction="<%=request.getContextPath()%>/loginAnime"method="get"><tableborder="1px"align="center"width="40%"cellspacing="......
  • python爬取ajax
    importrequestsurl='https://api.bilibili.com/x/v2/reply/main?csrf=056718067a9e03b351569ee0294e4a1e&mode=3&next=2&oid=813963991&plat=1&type=1'header={......
  • 最普通的ajax交互
    本文主要是介绍前后端数据交互的方法。现在在软件开发的设计中大多采用前后端分离的方法,那就不可避免出现前后端数据交互的问题。同时我们在上网的过程中所使用的浏览器为......
  • Ajax
    9、Ajax9.1、简介AJAX=AsynchronousJavaScriptandXML(异步的JavaScript和XML)。AJAX是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术。Ajax不是......
  • Enterprise Library 5 系列之——Data Access Application
    DataAccessApplication实现任务TaskMethodsFillingaDataSetandupdatingthedatabasefromaDataSet.ExecuteDataSet.Creates,populates,andret......
  • Enterprise Library 5 系列之1——Application Block的使用
    ApplicationBlock的对象、原型(Facades)与工厂ApplicationBlockNon-staticInstanceorFactoryCachingICacheManagerCryptographyCryptographyMan......
  • AJAX
    Ajax原理Ajax的原理简单来说是在用户和服务器之间加了—个中间层(AJAX引擎),通过 XmlHttpRequest对象来向服务器发异步请求,从服务器获得数据,然后用javascript 来......