D
D
Denis Tsvetkov2019-11-25 11:07:08
Unit testing
Denis Tsvetkov, 2019-11-25 11:07:08

Jest, Enzyme. How to test refs?

I am testing the componentDidUpdate lifecycle method. I have such code

public componentDidUpdate(prevProps) {
    if (this.rowsIsUpdated) {
      this.setState({
        headers: this.updateWidthAndEditors()
      });

      const node = this._domNodeRef.current.querySelector('.react-grid-Canvas');
      node.scrollTo(0, node.scrollHeight);
      this.rowsIsUpdated = false;
    }
    // Если пришли данные размер которых не совпадает с тем что было, то устанавливаем новую длину данных
    if (this.props.rows.length !== prevProps.rows.length) {
      this.setState({ countRows: this.props.rows.length, height: this.props.height });
      // ставим флаг для последующего скролла вниз к добавленным строкам, после рендера
      this.rowsIsUpdated = true;
    }
  }

I wrote a test using setProps. to update props rows.
it('change component content', () => {
    const wrapper = mount(<DataGrid rows={rows} headers={fields} width={500} height={500} />) as any;

    wrapper.setProps({ rows: rows2 }, () => {
      expect(wrapper.instance().rowsIsUpdated).toBe(false);
    });

  });

The test stops at the node.scrollTo call, giving the error "node.scrollTo is not a function".
Has anyone encountered similar problems? I haven't been able to solve this problem for several days now.
I've also tried using shallow for the mount space in the test and calling setProps twice, but then I get a "maximum call stack size" error.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question