×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Mike Rourke
Added: Nov 6, 2017 3:14 PM
Modified: Nov 6, 2017 3:34 PM
Views: 3
Tags: jest react spy
Spy on React class component instance methods using prototype.
  1. describe('Event Validation', () => {
  2.     let spies = {};
  3.  
  4.     beforeAll(() => {
  5.       const { prototype } = Component;
  6.       spies = {
  7.         onChangeValue: jest.spyOn(prototype, 'onChangeValue'),
  8.       };
  9.     });
  10.  
  11.     afterAll(() => {
  12.       spies.forEach(spy => spy.mockClear());
  13.     });
  14.  
  15.     it('fires instance actions when value is changed', () => {
  16.       const { wrapper } = setup();
  17.       wrapper.setState({ value: 25 });
  18.       expect(spies.onChangeValue).toHaveBeenCalled();
  19.     });
  20. });