使用酶将测试重定向到同一路由

时间:2019-03-06 14:07:55

标签: reactjs jestjs enzyme

我有一个登录页面,目前正在使用以下代码通过JEST / Enzyme测试:

import React from 'react';
import {BrowserRouter as Router} from 'react-router-dom';

import { Button, FormControl } from 'react-bootstrap';

import Adapter from 'enzyme-adapter-react-16'
import { configure, mount } from 'enzyme';

import LoginScreen from'./index';

it('login page renders without crashing', () => {
  configure({adapter: new Adapter()});
  let page =
  <Router>
    <LoginScreen />  
  </Router>;
  let mountedpage = mount(page);

  let btn = mountedpage.find(Button);
  let loginscreen;
  let frm = mountedpage.find(FormControl);

  frm.forEach(async (node) => {
    node.simulate('change', {value: 'e'});
    node.simulate('keypress', {key: 'e'});
    node.simulate('keypress', {key: 'Enter'});
  });

  loginscreen = mountedpage.find(LoginScreen);
  loginscreen.setState({mail: "test", pass: "123"});
  btn = mountedpage.find(Button);
  btn.simulate('click');
  });

调用btn.simulate('click')时,登录成功,并重定向到“ /”。通常,登录页面位于“ / login”,因此没有问题。 但是酶似乎在URL“ /”处模拟了页面,所以我得到警告:

console.error node_modules/warning/warning.js:34
Warning: You tried to redirect to the same route you're currently on: "/"

有什么办法可以告诉酶默认情况下它应该模拟为“ / login”?

0 个答案:

没有答案