mercredi 30 octobre 2019

How to test functional component's local function with React test utils?

I have the following functional component:

import React, { useState } from 'react';
import logo from './logo.svg';
import './App.css';

export function App() {
  const [item, setItem] = useState(false);

  function handleChange(e) {
    console.log(e.target.value);
  }

  function handleClick() {
    setItem(!item);
  }

  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
        {item && <div id="saludo">hola</div>}
        <select
          name="select"
          id="selector"
          defaultValue="value2"
          onChange={handleChange}
        >
          <option value="value1">Value 1</option>
          <option value="value2">Value 2</option>
          <option value="value3">Value 3</option>
        </select>
        <button onClick={handleClick}>I'm a button</button>
      </header>
    </div>
  );
}

export default App;

I have the following test:

import React from 'react';
import { render, unmountComponentAtNode } from 'react-dom';
import { act, Simulate } from 'react-dom/test-utils';
import App from './App';

describe('test', () => {
  let container = null;
  beforeEach(() => {
    // setup a DOM element as a render target
    container = document.createElement('div');
    document.body.appendChild(container);
  });

  afterEach(() => {
    // cleanup on exiting
    unmountComponentAtNode(container);
    container.remove();
    container = null;
  });
  it('renders without crashing', () => {
    const div = document.createElement('div');
    render(<App />, div);
    unmountComponentAtNode(div);
  });

  it('should click the button', () => {
    act(() => {
      render(<App />, container);
    });

    act(() => {
      container
        .querySelector('button')
        .dispatchEvent(new MouseEvent('click', { bubbles: true }));
    });

    let element = document.getElementById('saludo');
    expect(element.innerHTML).toBe('hola');

    act(() => {
      container
        .querySelector('button')
        .dispatchEvent(new MouseEvent('click', { bubbles: true }));
    });

    element = document.getElementById('saludo');
    expect(element).toBeNull();

    act(() => {
      container
        .querySelector('button')
        .dispatchEvent(new MouseEvent('click', { bubbles: true }));
    });

    element = document.getElementById('saludo');
    expect(element.innerHTML).toBe('hola');
  });

  it('various test', () => {
    act(() => {
      render(<App />, container);
    });

    const button = container.querySelector('button');
    act(() => {
      Simulate.click(button);
    });

    let element = document.getElementById('saludo');
    expect(element.innerHTML).toBe('hola');

    act(() => {
      Simulate.click(button);
    });

    element = document.getElementById('saludo');
    expect(element).toBeNull();
    let selector = document.getElementById('selector');
    expect(selector.value).toBe('value2');

    selector.value = 'value3';
    Simulate.change(selector);

    expect(selector.value).toBe('value3');
  });
});

But now I want to test the internal funcion and the state and can't find a way to test the hanldeChange to have been called or the handleClick to have been called.

Aucun commentaire:

Enregistrer un commentaire