lundi 8 février 2021

react testing library: test onSubmit form

so I am trying to test that when the button is clicked the onSubmit function is getting triggered.

but I keep getting a test error saying that the onSubmit function isnt being called

the test

describe('RecipeSearch', () => {
    test('test clicking the button triggers the onSubmit function', () => {
        const onSubmit = jest.fn();
        render(<RecipeSearch onSubmit={onSubmit} />);
        userEvent.selectOptions(screen.getByRole('combobox'), 'Sour');
        userEvent.click(screen.getByRole('button', { name: 'Search' }));
        expect(onSubmit).toHaveBeenCalled();
    });
});

the Error

    expect(jest.fn()).toHaveBeenCalled()

    Expected number of calls: >= 1
    Received number of calls:    0

the function

const recipeSearch = (props) => {
    return (
        <form data-testid='onSubmitId' onSubmit={(e) => onSubmit(e)}>
            <div>
                <div>
                    <div>
                        <label htmlFor='flavor_type'>Choose Flavor</label>
                        <select
                            name='flavor_type'
                            onChange={(e) => onChange(e)}
                            value={flavor_type}
                        >
                            <option value='Sour'>Sour</option>
                            <option>Sweet</option>
                            <option>Salty</option>
                        </select>
                    </div>

                    <div>
                            <button type='submit'>Search</button> 
                    </div>
                </div>
            </div>
        </form>
    );
};

Aucun commentaire:

Enregistrer un commentaire