【问题标题】:How to do testing with Relay modern with jest and enzyme?如何用 Jest 和酵素用 Relay modern 进行测试?
【发布时间】:2019-08-30 08:24:47
【问题描述】:

如何用玩笑和酶来测试这种文件...

   

class App extends React.Component{
    constructor(){
        super()
    }

    render(){
        const { viewer, children, isLoading } = this.props
        return(
          <div>
              <div id="container">
                {children}
              </div>
              {isLoading && <Loading />}
          </div>  
        );
    }
}

export default createFragmentContainer(
    App,
    graphql`
        fragment App_viewer on User{
            id
            email   
        }
    `
)

【问题讨论】:

    标签: reactjs graphql jestjs enzyme relaymodern


    【解决方案1】:

    你可以这样做

    const React = require('React');
    const App = require('App.react');
    jest.mock('react-relay', () => ({
      createFragmentContainer: App =>
        App,
    }));
    
    const {shallow} = require('enzyme');
    const {shallowToJson} = require('enzyme-to-json');
    
    describe('App', () => {
      it('renders the dashboard section correctly', () => {
        const wrapper = shallow(
          < App
            title="Test Dashboard Section"
            charts={[]}
          />,
        );
        expect(shallowToJson(wrapper)).toMatchSnapshot();
      });
    

    我们需要模拟 react-relay 。正如刚才提到的。

    【讨论】:

      猜你喜欢
      • 2023-04-10
      • 2021-07-06
      • 2020-08-12
      • 1970-01-01
      • 1970-01-01
      • 2018-07-31
      • 2021-11-22
      • 2017-10-15
      • 1970-01-01
      相关资源
      最近更新 更多