反应ui-router测试状态的变化

时间:2018-05-23 16:11:33

标签: reactjs angular-ui-router jestjs enzyme

我需要使用Jest和酶编写一个测试,该测试涵盖了我的应用程序中react / ui-router的基本实现。它需要做的就是断言在编程状态转换发生后,组件被加载到DOM中。

我一直在挖掘文档,而在这里,但是无法找到实现这一目标的方法。

我有一个Home组件的简单状态设置

export const states = [
  { 
  url: '/home',
  name: 'home',
  component: Home,
  }
];

这是我试图完成的测试:

import React from 'react';
import { Transition } from '@uirouter/react';
import { mount } from 'enzyme';
import { Home, Page } from './components/Layout';

describe('UIRouter', () => {
  it('go to home state', () => {
    Transition.router.stateService.go('home');

        const wrapper = mount(
           <UIRouter router={router} >
             <Page /> //component that contains <UIView />
           </UIRouter>\
         );
    expect(wrapper.find(<Home />).length).toBe(1);
  });
});

当我尝试此代码时,我收到错误:

TypeError: Cannot read property 'stateService' of undefined

的package.json:

@uirouter/react": "^0.8.0",
"react": "^16.3.2",
"react-dom": "^16.3.2",
"react-scripts": "1.1.4"
"enzyme": "^3.3.0",

非常感谢任何见解!

0 个答案:

没有答案