如何测试 React Material UI“选择”与柏树
我正在映射 Carsdata,它是 json。无法在 cypress 中测试这一点。
尝试过:
cy.get(#any-make-dropdown).select('chevroletMalibu')
还有其他选择。
<FormControl sx={{ m: 1,width: 300, bgcolor: 'whitesmoke' }}>
<InputLabel id="demo-simple-select-label">Any Make</InputLabel>
<Select
id="any-make-dropdown"
value={value}
label="Any Make"
onChange={handleChange}
>
{Carsdata.map((c) => (
<MenuItem key={c.Id} value={c.Name}>
{c.Name}
</MenuItem>
))}
</Select>
</FormControl>
// Carsdata.json
[
{
"Id": 1,
"Name":"chevroletMalibu",
"Miles_per_Gallon":18,
"Cylinders":8,
"Displacement":307,
"Horsepower":130,
"Weight_in_lbs":3504,
"Acceleration":12,
"Year":"1970-01-01",
"Origin":"USA"
},
{
"Id": 2,
"Name":"buickSkylark",
"Miles_per_Gallon":15,
"Cylinders":8,
"Displacement":350,
"Horsepower":165,
"Weight_in_lbs":3693,
"Acceleration":11.5,
"Year":"1972-01-01",
"Origin":"USA"
},
{
"Id": 3,
"Name":"plymouthSatellite",
"Miles_per_Gallon":18,
"Cylinders":8,
"Displacement":318,
"Horsepower":150,
"Weight_in_lbs":3436,
"Acceleration":11,
"Year":"1973-01-01",
"Origin":"USA"
},
{
"Id": 4,
"Name":"amcRebel",
"Miles_per_Gallon":16,
"Cylinders":8,
"Displacement":304,
"Horsepower":150,
"Weight_in_lbs":3433,
"Acceleration":12,
"Year":"1974-01-01",
"Origin":"USA"
},
{
"Id": 5,
"Name":"ford torino",
"Miles_per_Gallon":17,
"Cylinders":8,
"Displacement":302,
"Horsepower":140,
"Weight_in_lbs":3449,
"Acceleration":10.5,
"Year":"1975-01-01",
"Origin":"USA"
}
]
I am mapping the Carsdata which is json. Unable to test this in cypress.
Tried:
cy.get(#any-make-dropdown).select('chevroletMalibu')
and also other options.
<FormControl sx={{ m: 1,width: 300, bgcolor: 'whitesmoke' }}>
<InputLabel id="demo-simple-select-label">Any Make</InputLabel>
<Select
id="any-make-dropdown"
value={value}
label="Any Make"
onChange={handleChange}
>
{Carsdata.map((c) => (
<MenuItem key={c.Id} value={c.Name}>
{c.Name}
</MenuItem>
))}
</Select>
</FormControl>
// Carsdata.json
[
{
"Id": 1,
"Name":"chevroletMalibu",
"Miles_per_Gallon":18,
"Cylinders":8,
"Displacement":307,
"Horsepower":130,
"Weight_in_lbs":3504,
"Acceleration":12,
"Year":"1970-01-01",
"Origin":"USA"
},
{
"Id": 2,
"Name":"buickSkylark",
"Miles_per_Gallon":15,
"Cylinders":8,
"Displacement":350,
"Horsepower":165,
"Weight_in_lbs":3693,
"Acceleration":11.5,
"Year":"1972-01-01",
"Origin":"USA"
},
{
"Id": 3,
"Name":"plymouthSatellite",
"Miles_per_Gallon":18,
"Cylinders":8,
"Displacement":318,
"Horsepower":150,
"Weight_in_lbs":3436,
"Acceleration":11,
"Year":"1973-01-01",
"Origin":"USA"
},
{
"Id": 4,
"Name":"amcRebel",
"Miles_per_Gallon":16,
"Cylinders":8,
"Displacement":304,
"Horsepower":150,
"Weight_in_lbs":3433,
"Acceleration":12,
"Year":"1974-01-01",
"Origin":"USA"
},
{
"Id": 5,
"Name":"ford torino",
"Miles_per_Gallon":17,
"Cylinders":8,
"Displacement":302,
"Horsepower":140,
"Weight_in_lbs":3449,
"Acceleration":10.5,
"Year":"1975-01-01",
"Origin":"USA"
}
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
React 正在将标记转换为
而不是
通过点击,
React is transforming the markup into
<div>
instead of<select>
socy.select()
doesn't work.By click,
该解决方案类似于我的此处的答案,但适用于 Material-UI v5。
赛普拉斯文档建议分配一个唯一的
数据- cy
属性是 Cypress 应与之交互的元素,因此您应该对 Select、其inputProps
及其每个MenuItem
执行此操作。组件:
Cypress 测试:
The solution is similar to my answer here, but adapted to Material-UI v5.
Cypress documentation recommends to assign a unique
data-cy
prop to the elements that Cypress should interact with, so you should do that for both the Select, itsinputProps
and each of itsMenuItem
's.The component:
The Cypress test:
]
]