How to write jest test cases for navigation in react

只愿长相守 提交于 2019-12-22 08:12:02

问题


I am new to Jest test cases writing, Trying to write the test case for checking navigation in an react application.

Currently I have written test case which works like below: I have Login page which is having register link on it, which redirects user to the Register page. So what I am checking is 1. Loading the login page 2. Triggered Register link click event 3. So user moved to the Register page

but I am not able to check if the Register page is loaded or not? Is there any way to check the snapshot of the "Register" page and link in URL.

I have used jest, enzyme and react-test-render as shown in below code.

Login Page Html

<React.Fragment>       
    <BodyContainer noDrawer fullSpan>
      <Grid container spacing={0} className={classes.gridContainer}>
        <Grid item xs={12} md={6} lg={7} className="login--form">
          <div className="login--form--container">
            <img src={LogoPurple} alt="ABL" className="onboarding-logo" />
            <h3>Welcome back! Please login to your account.</h3>
              {error ? <p>{error}</p> : null}
              <Field name="email" component={renderTextField} label="Email" style={styles.formField}/>
              <Field name="password" component={renderTextField} label="Password" style={styles.formField} />
              <Field name="rememberMe" component={renderCheckbox} label="Remember Me" />
              <div className="has-spacing">
                <Button variant="raised" size="large" color="primary" className="extraLarge" type="submit" onClick={this.handleSubmit}>
                  Login
                </Button>
              </div>

              <Link to="forgot-password">Forgot Password</Link>
                <p className="need-account">
                  Need an account?
                  <Link to="/register">
                    <strong> Sign up</strong>
                </p>
              </Link>
            <LegalLinks />
          </div>
        </Grid>
      </Grid>
    </BodyContainer>
  </React.Fragment>

In above code register is the link used to navigate.

thanks in advance...

来源:https://stackoverflow.com/questions/49728333/how-to-write-jest-test-cases-for-navigation-in-react

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!