Skip to content

Instantly share code, notes, and snippets.

@kouameYao
Created September 29, 2023 09:28
Show Gist options
  • Save kouameYao/6398bb53f06170876b30e0abf1054267 to your computer and use it in GitHub Desktop.
Save kouameYao/6398bb53f06170876b30e0abf1054267 to your computer and use it in GitHub Desktop.
Test unitaire
// Button.js
import React from 'react';
function Button(props) {
return (
<button onClick={props.onClick} disabled={props.disabled}>
{props.label}
</button>
);
}
export default Button;
// Button.test.js
import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import Button from './Button';
test('rend le bouton cliquable', () => {
const onClickMock = jest.fn();
const { getByText } = render(<Button label="Cliquez-moi" onClick={onClickMock} />);
const button = getByText('Cliquez-moi');
fireEvent.click(button);
expect(onClickMock).toHaveBeenCalled();
});
test('rend le bouton désactivé', () => {
const { getByText } = render(<Button label="Cliquez-moi" disabled />);
const button = getByText('Cliquez-moi');
expect(button).toBeDisabled();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment