C
C
colorkid2019-09-08 16:21:17
JavaScript
colorkid, 2019-09-08 16:21:17

Can't test React component method, why?

Hello.
Unable to test methods in class react component. Here is the method:

testSum(a, b) {
    return a + b;

In tests I write the following:
const wrapper = shallow(<МойКомпонент/>);
 console.log(wrapper.instance().testSum(2,4).toBe(6));

but it draws an error that they say wrapper.instance(...).testSum is not a function
to the wrapper.instance() console, and there, apart from the wired React methods, nothing, not one of my methods, but only
Connect {
      props: {},
      context: {},
      refs: {},
      updater:
       Updater {
         _renderer:
          ReactShallowRenderer {
            _context: {},
            _element: [Object],
            _instance: [Circular],
            _newState: null,
            _rendered: [Object],
            _rendering: false,
            _forcedUpdate: false,
            _updater: [Circular],
            _dispatcher: [Object],
            _workInProgressHook: null,
            _firstWorkInProgressHook: null,
            _isReRender: false,
            _didScheduleRenderPhaseUpdate: false,
            _renderPhaseUpdates: null,
            _numberOfReRenders: 0 },
         _callbacks: [] },
      selectDerivedProps: [Function: selectDerivedProps],
      selectChildElement: [Function: selectChildElement],
      indirectRenderWrappedComponent: [Function: bound indirectRenderWrappedComponent],
      state: null,
      setState: [Function] }

what am I doing wrong, I want to test methods on components?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yustas Alexu, 2019-09-08
@colorkid

Connect {
      props: {},
      context: {},

The word Connect at the very beginning, as it were, hints that the component is apparently wrapped in connect and naturally there will be no testSum method there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question