Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

reactjs - How to test screen transition by Link component in react

I'm writing test code for my react app. I want to write test code that tests screen transition from Page A to Page B.

However, somehow the event to make user move to Page B from Page A doesn't work in my test code.

This is the test code.

  • Test code
import { fireEvent, render, screen } from '@testing-library/react';
import React from 'react';
import { StaticRouter } from 'react-router-dom';
import Routes from '../src/rouer';

const App = () => {
  return (
    <StaticRouter>
      <Routes />
    </StaticRouter>
  );
};

describe('Test screen transition', () => {
  it('Test screen transition', () => {
    render(<App />);
    screen.debug();

    // I want this line makes the click event fired, but it doesn't
    fireEvent.click(screen.getByText('Go to Page B from Page A'));
    screen.debug();
  });
});
  • This is my Page A component
import * as React from 'react';
import { Link } from 'react-router-dom';

const A = () => {
  return (
    <>
      <div>
        <Link to="/b">Go to Page B from Page A</Link>
      </div>
    </>
  );
};

export default A;
  • This is my Page B component
import * as React from 'react';
import { Link } from 'react-router-dom';

const B = () => {
  return (
    <>
      <div>
        This is Page B
      </div>
    </>
  );
};

export default B;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...