Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.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

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 在存在的div中找不到文本_Javascript_Reactjs_Jestjs_Material Ui_Enzyme - Fatal编程技术网

Javascript 在存在的div中找不到文本

Javascript 在存在的div中找不到文本,javascript,reactjs,jestjs,material-ui,enzyme,Javascript,Reactjs,Jestjs,Material Ui,Enzyme,我有一个测试,其中我有一些文本呈现在一系列材质UI卡中。我为JSON数组中的每个卡提供数据,其中每个对象如下所示 { projectName: 'Project1', crc: 11305, dateCreated: '1/1/2020', createdBy: 'A. Bloggs', lastModified: '22/2/2020' } 在我的酶测试中,我找到了所有的卡片,然后确保每个JSON数据都用

我有一个测试,其中我有一些文本呈现在一系列材质UI卡中。我为JSON数组中的每个卡提供数据,其中每个对象如下所示

  {
        projectName: 'Project1',
        crc: 11305,
        dateCreated: '1/1/2020',
        createdBy: 'A. Bloggs',
        lastModified: '22/2/2020'
  }
在我的酶测试中,我找到了所有的卡片,然后确保每个JSON数据都用下面的代码片段呈现在
卡片中

const verifyThatAllCardDataIsRenderedCorrectly = ( cardsFound, dataItemsSupplied) => {
    dataItemsSupplied.forEach((dataItem, currCardIndex) => {
        for(let key in dataItem){
            if(key !== 'id'){
                console.log("Comparing CARD CONTENT ===>"+cardsFound.at(currCardIndex).debug() + " WITH =====> "+dataItem[key]);
                expect(cardsFound.at(currCardIndex).contains(dataItem[key])).toEqual(true);
            }
        }
    });
};
当测试失败时,我查看
cardsFound.at(currCardIndex.debug()
卡的相关部分的调试输出,它如下所示:

 <div className="makeStyles-projectPropertiesGridContainer-348">
    <div className="makeStyles-projectPropertyName-349">
      CRC
    </div>
    <div className="makeStyles-projectPropertyValue-350">
      11305
    </div>
</div>

华润
11305
dataItem[key]
的值是
11305
,那么为什么断言
expect(cardsFound.at(currCardIndex).contains(dataItem[key])).toEqual(true)
fail


projectName
属性的上一个断言匹配并通过

由于您只希望检查元素中是否存在文本,因此可以将
text()
toContain一起使用

const verifyThatAllCardDataIsRenderedCorrectly = ( cardsFound, dataItemsSupplied) => {
    dataItemsSupplied.forEach((dataItem, currCardIndex) => {
        for(let key in dataItem){
            if(key !== 'id'){
                console.log("Comparing CARD CONTENT ===>"+cardsFound.at(currCardIndex).debug() + " WITH =====> "+dataItem[key]);
                expect(cardsFound.at(currCardIndex).text()).toContain(dataItem[key])
            }
        }
    });
};

expect(cardsFound.at(currCardIndex.text()).toContain(dataItem[key])
应该工作!非常感谢你。也许你可以把你的评论作为一个答案,这样我就可以把它标记为被接受的答案了?