使用反应测试库测试到达路由器 [英] Testing reach router with react-testing library

查看:62
本文介绍了使用反应测试库测试到达路由器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将React测试库与到达路由器一起使用 https://testing-library.com/docs/example-reach-router

Going through using react testing library with reach-router https://testing-library.com/docs/example-reach-router

function renderWithRouter(
  ui,
  { route = '/', history = createHistory(createMemorySource(route)) } = {}
) 

该函数的第二个参数(可疑对象是{}).但是使用'='代替':'表示其不是名称/值对.那是什么?

Second argument to the function , suspect is an object, {} . But '=' is used instead of ':' means its not a name-value pair. So what is it?

另外,两个对象之间的赋值运算符的用途是什么

Also, what is the purpose of the assignment operator between two objects

{ route = '/', history = createHistory(createMemorySource(route)) } = {}

推荐答案

此语法称为看看这个例子:

function drawChart({size = 'big', coords = {x: 0, y: 0}, radius = 25} = {}) {
  console.log(size, coords, radius);
  // do some chart drawing
}

drawChart({
  coords: {x: 18, y: 30},
  radius: 30
});

在上面drawChart的功能签名中,已分解的左侧分配给右侧的空对象文字:{size = 'big', coords = {x: 0, y: 0}, radius = 25} = {}.您也可以在没有右侧分配的情况下编写该函数.但是,如果省略了右侧分配,则该函数将在调用时寻找至少一个要提供的参数,而以当前形式,您可以简单地调用drawChart()而不提供任何参数.如果您希望能够在不提供任何参数的情况下调用该函数,则当前设计很有用;当您要确保将对象传递给该函数时,另一个设计也将很有用.

In the function signature for drawChart above, the destructured left-hand side is assigned to an empty object literal on the right-hand side: {size = 'big', coords = {x: 0, y: 0}, radius = 25} = {}. You could have also written the function without the right-hand side assignment. However, if you leave out the right-hand side assignment, the function will look for at least one argument to be supplied when invoked, whereas in its current form, you can simply call drawChart() without supplying any parameters. The current design is useful if you want to be able to call the function without supplying any parameters, the other can be useful when you want to ensure an object is passed to the function.

返回renderWithRouter函数示例

function renderWithRouter(ui, { route = "/", history = function(){} } = {}) {
  console.log(route, history);
}

console.log(renderWithRouter({})) //output: / ƒ (){}

这篇关于使用反应测试库测试到达路由器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆