无法通过MobileDialog使用Material UI访问React组件中的方法

时间:2019-06-05 15:50:47

标签: reactjs jestjs material-ui

当我将MobileDialog注入React组件时,无法访问该组件的成员。

我尝试过潜水,材料界面变浅,安装,酶变浅,无济于事。

无需注入MobileDialog,我就可以毫无问题地访问成员。

组件:

class Blog extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      open: false
    };
    this.postCount = 5;
    this.posts = this.getPosts();
  }

  setOpen = () => {
    if (this.showPosts()) {
      this.setState({ open: true });
    }
  };


  render() {
    return (
      ...
    );
  }
}

export default compose(
  observer,
  withStyles(styles),
  withMobileDialog()
)(Blog);

测试:

import React from 'react';
//import { mount } from 'enzyme';
import Blog from '../blog';
import { Switch, Redirect } from 'react-router-dom';
import sinon from 'sinon';
import blog from '../blog';
import { createShallow, createMount } from '@material-ui/core/test-utils';

describe('Blog', () => {
  let blogWrapper, shallow, mount;
  beforeEach(() => {
    shallow = createShallow(); 
    mount = createMount();
  });

  it('should render private page with its router', () => {



    blogWrapper = shallow(
      <Blog

      />
    );

    // setOpen is undefined
    const toggleSpy = jest.spyOn(blogWrapper.instance(), 'setOpen');

    expect(blogWrapper.state.open).toBeTrue();
  });

});

0 个答案:

没有答案