如何测试柏树中的反应材料UI抽屉组件属性值
鉴于我的React组件中的此MUI实现:
<Box sx={{ display: 'flex' }}>
<Drawer
name="permDrawer"
open={openSideDrawer}
style={{ zIndex: -1000 }}
variant="permanent"
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
>
<Toolbar />
<Box>
<List
sx={{
[`& .active, & .${listItemClasses.root}:hover`]: {
color: '#2196f3',
fontWeight: 'bold',
fontSize: '30'
}
}}
>
<ListItem>
<ListItemButton
sx={{
minHeight: 48,
justifyContent: open ? 'initial' : 'center',
px: 2.5,
paddingLeft: open ? 1 : 5
}}
>
<ListItemIcon>
<HomeOutlined />
</ListItemIcon>
<ListItemText primary="Dashboard" sx={{ opeacity: open ? 0 : 1 }} />
</ListItemButton>
</ListItem>
</List>
</Box>
</Drawer>
</Box>
IM试图测试抽屉是否打开,我可以成功开启Click事件,并且我在柏树测试跑者中看到了预期的行为。但是,我想检查抽屉组件的开放属性,因为它从false变为true,因为这是一个更准确的测试。但是,我无法访问给定的开放属性:
describe('When testing sideMenu', () => {
it('it should open sideMenu', () => {
cy.readFile('./secrets/swb-shell/cypress-e2e.json').then((testConfig) => {
cy.visit('/')
cy.get('[data-automation-id="uname"]').type(testConfig.users[0].userid, { log: false })
cy.get('[data-automation-id="pwd"]').type(testConfig.users[0].password, { log: false })
cy.get('[data-automation-id="loginBtn"]').click()
cy.get('[name="permDrawer"]').should('have.attr','open','false');
cy.get('.MuiToolbar-root > :nth-child(1) > .MuiButtonBase-root').click()
cy.get('.MuiDrawer-root > .MuiPaper-root').should('be.visible');
})
})
}),
因为我了解它会使标记混淆,因此建议使用其他库来正确测试。我是否错过了测试中的依赖性,或者有其他方法应该解决这个问题吗?这是我第一次在赛普拉斯(Cypress)中进行测试,我还没有在3年内与React合作,因此ID非常感谢您的某些背景和帮助! t!
Given this MUI implementation within my react component:
<Box sx={{ display: 'flex' }}>
<Drawer
name="permDrawer"
open={openSideDrawer}
style={{ zIndex: -1000 }}
variant="permanent"
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
>
<Toolbar />
<Box>
<List
sx={{
[`& .active, & .${listItemClasses.root}:hover`]: {
color: '#2196f3',
fontWeight: 'bold',
fontSize: '30'
}
}}
>
<ListItem>
<ListItemButton
sx={{
minHeight: 48,
justifyContent: open ? 'initial' : 'center',
px: 2.5,
paddingLeft: open ? 1 : 5
}}
>
<ListItemIcon>
<HomeOutlined />
</ListItemIcon>
<ListItemText primary="Dashboard" sx={{ opeacity: open ? 0 : 1 }} />
</ListItemButton>
</ListItem>
</List>
</Box>
</Drawer>
</Box>
Im trying to test if the Drawer opens or not and I can successfully fire the click event and I see the expected behavior in my Cypress Test Runner. However I want to check the open attribute of the Drawer component as it changes from false to true because this is a more accurate test. But I am not able to get access to the open attribute given :
describe('When testing sideMenu', () => {
it('it should open sideMenu', () => {
cy.readFile('./secrets/swb-shell/cypress-e2e.json').then((testConfig) => {
cy.visit('/')
cy.get('[data-automation-id="uname"]').type(testConfig.users[0].userid, { log: false })
cy.get('[data-automation-id="pwd"]').type(testConfig.users[0].password, { log: false })
cy.get('[data-automation-id="loginBtn"]').click()
cy.get('[name="permDrawer"]').should('have.attr','open','false');
cy.get('.MuiToolbar-root > :nth-child(1) > .MuiButtonBase-root').click()
cy.get('.MuiDrawer-root > .MuiPaper-root').should('be.visible');
})
})
})
As I understand it MUI obfuscates the markup and it is recommended to use additional libraries in order to properly test. Am I missing a dependency in my testing or is there another way I should go about this? This is my first time testing within cypress and I havent worked with react in 3 years so Id really appreciate some context and help! Ty !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,在运行时,页面中不存在
打开
属性。您可以尝试使用它是“对称的”,
但我认为抽屉实际上是添加/删除的,而不是使可见/看不见的,因此
检查
opensidedrawer
您可以添加自己的属性
opensidedrawer
Indeed the
open
attribute in the source is not present in the page at runtime.You might try this to be "symmetrical"
but I think the drawer is actually added/removed rather than made visible/invisible, so
Checking
openSideDrawer
You can add your own attribute for
openSideDrawer