User3000
User3000

Reputation: 65

React unit testing anonymous function

I am trying to test an onChange anonymous function, which changes a value in my textbox. I want to verify that if the function inside onChange gets called, the state is changed. The function itself is simple:

onChange={(e) => this.setState({someState: e.value})}

If the function had a name, I would do something like the following:

         test('myFunction should change someState value', () => {

                 const wrapper = shallow(<MyComponent/>);
                 const instance = wrapper.instance();

                const mockEvent = {
                    target:{
                        value: "some value in textbox"
                     }        
                   };

                const expectedValue = "some value in textbox";

                instance.myFunction(mockEvent);
                expect(wrapper.state().someState).toEqual(expectedValue)

              }); 

Since the function is anonymous, I don't know how to call it with instance. I'd prefer not to give the function a name because i don't want the testing to affect my code. What would be a good solution?

Upvotes: 2

Views: 1436

Answers (1)

Lin Du
Lin Du

Reputation: 102257

There are two ways to test this case. E.g.

index.tsx:

import React, { Component } from 'react';

export default class MyComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      someValue: '',
    };
  }
  render() {
    return (
      <>
        <input type="text" onChange={(e) => this.setState({ someValue: e.target.value })}></input>
      </>
    );
  }
}

index.test.tsx:

import MyComponent from './';
import { shallow } from 'enzyme';
import React from 'react';

describe('61597604', () => {
  it('should pass', () => {
    const wrapper = shallow(<MyComponent></MyComponent>);
    const mEvent = { target: { value: 'ok' } };
    wrapper.find('input').simulate('change', mEvent);
    expect(wrapper.state()).toEqual({ someValue: 'ok' });
  });

  it('should pass 2', () => {
    const wrapper = shallow(<MyComponent></MyComponent>);
    const input = wrapper.find('input').getElement();
    const mEvent = { target: { value: 'ok' } };
    input.props.onChange(mEvent);
    expect(wrapper.state()).toEqual({ someValue: 'ok' });
  });
});

unit test results with 100% coverage:

 PASS  stackoverflow/61597604/index.test.tsx (8.059s)
  61597604
    ✓ should pass (13ms)
    ✓ should pass 2 (1ms)

-----------|---------|----------|---------|---------|-------------------
File       | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
-----------|---------|----------|---------|---------|-------------------
All files  |     100 |      100 |     100 |     100 |                   
 index.tsx |     100 |      100 |     100 |     100 |                   
-----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        9.213s

Upvotes: 1

Related Questions