如何测试柏树中的反应材料UI抽屉组件属性值

发布于 2025-02-07 14:50:17 字数 2219 浏览 3 评论 0原文

鉴于我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

真心难拥有 2025-02-14 14:50:17

实际上,在运行时,页面中不存在打开属性。

您可以尝试使用它是“对称的”,

cy.get('.MuiDrawer-root > .MuiPaper-root').should('be.not.visible');
cy.get('.MuiToolbar-root > :nth-child(1) > .MuiButtonBase-root').click()
cy.get('.MuiDrawer-root > .MuiPaper-root').should('be.visible');

但我认为抽屉实际上是添加/删除的,而不是使可见/看不见的,因此

cy.get('.MuiDrawer-root > .MuiPaper-root').should('not.exist');
cy.get('.MuiToolbar-root > :nth-child(1) > .MuiButtonBase-root').click()
cy.get('.MuiDrawer-root > .MuiPaper-root').should('be.visible');  // existence is implied

检查opensidedrawer

您可以添加自己的属性opensidedrawer

<Drawer
  name="permDrawer"
  open={openSideDrawer}
  data-cy-open={openSideDrawer}
cy.get('[name="permDrawer"]').should('have.attr','data-cy-open','false');

Indeed the open attribute in the source is not present in the page at runtime.

You might try this to be "symmetrical"

cy.get('.MuiDrawer-root > .MuiPaper-root').should('be.not.visible');
cy.get('.MuiToolbar-root > :nth-child(1) > .MuiButtonBase-root').click()
cy.get('.MuiDrawer-root > .MuiPaper-root').should('be.visible');

but I think the drawer is actually added/removed rather than made visible/invisible, so

cy.get('.MuiDrawer-root > .MuiPaper-root').should('not.exist');
cy.get('.MuiToolbar-root > :nth-child(1) > .MuiButtonBase-root').click()
cy.get('.MuiDrawer-root > .MuiPaper-root').should('be.visible');  // existence is implied

Checking openSideDrawer

You can add your own attribute for openSideDrawer

<Drawer
  name="permDrawer"
  open={openSideDrawer}
  data-cy-open={openSideDrawer}
cy.get('[name="permDrawer"]').should('have.attr','data-cy-open','false');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文