Skip to content

How to test click button #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
marlonmleite opened this issue Sep 18, 2019 · 1 comment
Closed

How to test click button #53

marlonmleite opened this issue Sep 18, 2019 · 1 comment

Comments

@marlonmleite
Copy link

The documentation is very simple and with few examples.

All of the click examples I saw, none clicks to see if the function is being called.

How to do this?

I try this, but isn't work:

import { act, fireEvent, render } from '@testing-library/svelte';
import Button from '../Button.svelte';

describe('Button', () => {
  test('should click button', async () => {
    const click = jest.fn();
    const { container } = render(Button, { props: { click } });
    const button = container.querySelector('button');

    const acting = act(() => {
      fireEvent.click(button);
    });

    await acting;

    expect(click).toBeCalled();

  });
});
@mihar-22
Copy link
Collaborator

mihar-22 commented Oct 8, 2019

Hey @marlonmleite, it's really hard to say without seeing your Button component. There's way more information about fireEvent over at this link https://testing-library.com/docs/dom-testing-library/api-events.

The testing library follows a different set of guiding principles. I think you have your unit testing hat on but you need to think more from a users perspective. I'd say in order to test that component you'd want to know what side effect pressing the button creates.

For example checkout this test in which clicking the button changes the text of the button, which is what the user would see. So we validate that the button text changed, not that the buttons handler got called (which you are indirectly testing anyway).

I'll try adding some more docs in soon!

Hope that helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants