首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用React-Testing库模拟ArrowRight事件

用React-Testing库模拟ArrowRight事件
EN

Stack Overflow用户
提问于 2020-12-04 18:35:35
回答 1查看 152关注 0票数 0

我坚持使用这个React-Testing-Library。我有两个组件: HeroesScreen和HeroesDiamondBar,当stateVariable currentStep大于0时,它会在HeroesScreen中呈现。我想在我的测试中模拟arrowRight,以便至少显示一个DiamondBar。

Heroes.jsx:

代码语言:javascript
复制
const HeroesScreen = ({ data, backgroundImage }) => {
 const [element, setElement] = useState();
 const [currentStep, setCurrentStep] = useState(0);

 const screenEl = useRef();

 const handler = (e) => {
   const nextScreen = e.key === 'PageDown' || e.key === 'ArrowRight';
   const prevScreen = e.key === 'PageUp' || e.key === 'ArrowLeft';

   if (nextScreen && currentStep < LAST_STEP) {
    e.stopPropagation();
    setCurrentStep(currentStep + 1);
  } else if (prevScreen && currentStep > 0) {
    e.stopPropagation();
    setCurrentStep(currentStep - 1);
  } else {
    setCurrentStep(0);
  }
};

useEffect(() => {
  screenEl.current.focus();
  setElement(screenEl.current);
}, []);

useKeys(element, handler);

return (
  <div
  className="heroes"
  style={{ backgroundImage: `url(${backgroundImage})` }}
  tabIndex="2"
  ref={screenEl}
>
  <div className="heroes__cover" data-testid="heroes">
    {heroesConfig.map((item, index) => {
      const cssIndex = index + 1;
      const { title, step1, dataId, filterProperty } = item;

      return (
        <Fragment key={`${title}-${cssIndex}`}>
          {currentStep >= step1 && (
            <HeroesDiamondBar
              photo={data[dataId][0].photo}
              name={data[dataId][0].name}
              points={data[dataId][0][filterProperty]}
              cssIndex={cssIndex}
              currentStep={currentStep}
              {...item}
            />
          )}
        </Fragment>
      );
    })}

   </div>
 </div>
);
};

HeroesScreen.propTypes = {
 data: PropTypes.object.isRequired,
 backgroundImage: PropTypes.string.isRequired,
};

export default HeroesScreen;

HeroesScreen.test.js:

代码语言:javascript
复制
import React from 'react';
import { render, screen, cleanup } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import HeroesScreen from '@screens/HeroesScreen/HeroesScreen';
import HeroesDiamondBarMock from '@components/HeroesDiamondBar/HeroesDiamondBar';

afterEach(cleanup);

jest.mock('@components/HeroesDiamondBar/HeroesDiamondBar', () => {
 let id = 0;
 return jest.fn(() => {
   return (
    <div data-testid={`heroes-diamond-bar-${++id}`}>Heroes Diamond Bar</div>
  );
 });
});

const props = {
 data: {
  rare: [
  {
    name: 'Name 1',
    points: '4500',
    photo: 'img url',
  },
]
 },
 backgroundImage:
  'bg img',
};

describe('<HeroesScreen />', () => {
 test('renders a <HeroesDiamondBar /> component with data props', async () => {
   const { getAllByTestId, getByTestId } = render(<HeroesScreen {...props} />);

   await userEvent.type(getByTestId('heroes'), '{arrowright}');

   expect(getAllByTestId(/heroes-diamond-bar/)).not.toBeNull();
 });
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-04 21:41:31

解决方案是将data-testid="heroes"移动到父div,然后测试中的模拟工作

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65141999

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档