Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在React中为测试设置QueryString参数_Javascript_Reactjs_React Redux - Fatal编程技术网

Javascript 如何在React中为测试设置QueryString参数

Javascript 如何在React中为测试设置QueryString参数,javascript,reactjs,react-redux,Javascript,Reactjs,React Redux,我有一个文件-utils.js,其中包含以下函数: export const getLocationPage = () => { const locationPage = QueryString.parse(location.search).page; return locationPage ? locationPage : "undefined"; }; utils.js不是一个组件,而是其他组件使用的一个简单的.js文件 如何测试此函数的逻辑?我的意思是,如何在测试中设置qu

我有一个文件-utils.js,其中包含以下函数:

export const getLocationPage = () => {
  const locationPage = QueryString.parse(location.search).page;
  return locationPage ? locationPage : "undefined";
};
utils.js不是一个组件,而是其他组件使用的一个简单的.js文件


如何测试此函数的逻辑?我的意思是,如何在测试中设置queryString的值?

下面是一个如何设置queryString值的示例:

// Testing Claim Details component 
describe('<ClaimDetails />', () => {
  let wrapper

  beforeEach(() => {
    wrapper = shallow(
     // Add initail props and values
      <ClaimDetails
        loading={false}
        locationPage={"some string"}
      />
    )
  })

  it('ClaimDetails: If Claim is null component should not be rendered', () => {
 // *** Here you can setProps value of Query string in SetProps{}
    wrapper.setProps({
      locationPage: "Some String Value",
    })
    expect(wrapper.html()).toBeNull()
  })
//测试索赔详细信息组件
描述(“”,()=>{
让包装器
在每个之前(()=>{
包装纸=浅(
//添加初始道具和值
)
})
它('ClaimDetails:如果声明为空,则不应呈现组件',()=>{
//***这里您可以在setProps{}中设置查询字符串的setProps值
包装器.setProps({
locationPage:“一些字符串值”,
})
expect(wrapper.html()).toBeNull()
})

以下是一个示例,说明如何设置QueryString的值:

// Testing Claim Details component 
describe('<ClaimDetails />', () => {
  let wrapper

  beforeEach(() => {
    wrapper = shallow(
     // Add initail props and values
      <ClaimDetails
        loading={false}
        locationPage={"some string"}
      />
    )
  })

  it('ClaimDetails: If Claim is null component should not be rendered', () => {
 // *** Here you can setProps value of Query string in SetProps{}
    wrapper.setProps({
      locationPage: "Some String Value",
    })
    expect(wrapper.html()).toBeNull()
  })
//测试索赔详细信息组件
描述(“”,()=>{
让包装器
在每个之前(()=>{
包装纸=浅(
//添加初始道具和值
)
})
它('ClaimDetails:如果声明为空,则不应呈现组件',()=>{
//***这里您可以在setProps{}中设置查询字符串的setProps值
包装器.setProps({
locationPage:“一些字符串值”,
})
expect(wrapper.html()).toBeNull()
})

Thank@Nikola Trajkovic!ClaimDetails是您测试的函数所在的类的名称吗?因为我没有什么要舍弃的,因为它不是导出类,我拥有的只是一个包含一些函数的文件,我如何舍弃它?Thank@Nikola Trajkovic!ClaimDetails是您测试的函数所在的类的名称吗?因为我没有什么要浅显的,因为它不是一个导出类,我有的只是一个带有一些函数的文件,我怎么能浅显它呢?