代码之家  ›  专栏  ›  技术社区  ›  Learner

传递React浅酶测试用例的上下文-jest

  •  0
  • Learner  · 技术社区  · 6 年前

    import AppContext from '../../context/AppContext'
    import extractDatta from '../../helper';
    
    class App extends Component{
      static contextType = AppContext
    
      componentWillMount(){
       const resolvedData = extractData("/home",this.context)
      }
    
     render(){
       return(
      )
     }
    
    }
    

    助手/索引。js:

    const extractData = (path, context) => {
      // getting context as undefined 
    }
    

    应用程序。测验js:

    describe('App test suites', () => {
      let wrapper;
      let appContext;
      beforeEach(() => {
        appContext = {name: "Application"}
        wrapper = shallow(<Supplier />, { appContext })
      })
      it('Should render the App Component', () => {
        expect(wrapper).toMatchSnapshot();
      })
    })
    

    0 回复  |  直到 6 年前
        1
  •  2
  •   Dan Garland    6 年前

    这是我在酶团队完成工作时的一个棘手的解决方法[ https://github.com/airbnb/enzyme/issues/1553](React contextTypes 在组件类上,允许根据文档传递上下文。

    import PropTypes from "prop-types";
    
    describe('App test suites', () => {
      let wrapper;
      let appContext;
      beforeEach(() => {
        appContext = {name: "Application"}
    
        // Hack in the legacy context API just for tests. You'll get a warning, but it 
        // ought to work...
        Supplier.contextTypes = {
          name: PropTypes.string
        }
    
        wrapper = shallow(<Supplier />, { appContext })
      })
    
      it('Should render the App Component', () => {
        expect(wrapper).toMatchSnapshot();
      })
    })
    
        2
  •  0
  •   AntowaKartowa    5 年前

    这可能不会有什么帮助。但不应该用上下文参数名而不是appContext传递Options对象吗?

    wrapper = shallow(<Supplier />, { context: appContext })