最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How can I test a debounce function with JestEnzyme? - Stack Overflow

matteradmin5PV0评论

I have this ponent where test coverage says I need to test lines 24 and 25:

class TableToolbarComp extends Component {
  state = {
    shipmentId: '',
  };

  debouncedSetFilters = debounce(() => {
    const { applyFilters } = this.props; // LINE 24
    applyFilters(this.state);            // LINE 25
  }, 750);

  updateShipmentId = ev => {
    this.setState(
      {
        shipmentId: ev.target.value,
      },
      this.debouncedSetFilters,
    );
  };

  render() {...}
}

And the test:

  beforeEach(() => {
    applyFilters: k => k,
  });

...

  it('should trigger button click', () => {
    const wrapper = shallow(<TableToolbarComp {...props} />);

    wrapper.instance().debouncedSetFilters(750);
    wrapper.instance().updateShipmentId({ target: { shipmentId: '124' } });
    wrapper.instance().props.applyFilters({ shipmentId: '124' });
  });

And I am not getting any errors, it just says those 2 lines need coverage.

I already attempted to called debouncedSetFilters and applyFilters on the test but it's still returning those 2 lines as uncover.

What am I missing?

I have this ponent where test coverage says I need to test lines 24 and 25:

class TableToolbarComp extends Component {
  state = {
    shipmentId: '',
  };

  debouncedSetFilters = debounce(() => {
    const { applyFilters } = this.props; // LINE 24
    applyFilters(this.state);            // LINE 25
  }, 750);

  updateShipmentId = ev => {
    this.setState(
      {
        shipmentId: ev.target.value,
      },
      this.debouncedSetFilters,
    );
  };

  render() {...}
}

And the test:

  beforeEach(() => {
    applyFilters: k => k,
  });

...

  it('should trigger button click', () => {
    const wrapper = shallow(<TableToolbarComp {...props} />);

    wrapper.instance().debouncedSetFilters(750);
    wrapper.instance().updateShipmentId({ target: { shipmentId: '124' } });
    wrapper.instance().props.applyFilters({ shipmentId: '124' });
  });

And I am not getting any errors, it just says those 2 lines need coverage.

I already attempted to called debouncedSetFilters and applyFilters on the test but it's still returning those 2 lines as uncover.

What am I missing?

Share Improve this question asked Jan 23, 2019 at 1:44 ReactingReacting 6,1378 gold badges42 silver badges95 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Function calls cannot be tested efficiently without spies. It should be:

  beforeEach(() => {
    applyFilters = jest.fn();
  });

In order to test asynchronous time-sensitive function, timer mocks should be applied:

jest.useFakeTimers();

const wrapper = shallow(<TableToolbarComp applyFilters={applyFilters} />);

wrapper.instance().debouncedSetFilters();
wrapper.instance().debouncedSetFilters();
expect(applyFilters).not.toHaveBeenCalled();
jest.advanceTimersByTime(750);
expect(applyFilters).toHaveBeenCalledTimes(1);

Then debouncedSetFilters can be stubbed in updateShipmentId test.

Post a comment

comment list (0)

  1. No comments so far