38
loading...
This website collects cookies to deliver better user experience
import React from 'react';
import {render} from '@testing-library/react';
describe('Confirmation component', () => {
it('should render', () => {
const {getByRole} = render(<Confirmation />);
expect(getByRole('dialog')).toBeInTheDocument();
});
});
import React from 'react';
const Confirmation = () => {
return <div role="dialog"></div>;
};
export default Confirmation;
it('should have a title saying "Confirmation"', () => {
const {getByText} = render(<Confirmation />);
expect(getByText('Confirmation')).toBeInTheDocument();
});
import React from 'react';
const Confirmation = () => {
return (
<div role="dialog">
<h1>Confirmation</h1>
</div>
);
};
export default Confirmation;
it('should have a dynamic confirmation question', () => {
const question = 'Do you confirm?';
const {getByText} = render(<Confirmation>{question}</Confirmation>);
expect(getByText(question)).toBeInTheDocument();
});
import React from 'react';
const Confirmation = ({children}) => {
return (
<div role="dialog">
<h1>Confirmation</h1>
<div>{children}</div>
</div>
);
};
export default Confirmation;
it('should have an "OK" button', () => {
const {getByRole} = render(<Confirmation />);
expect(getByRole('button', {name: 'OK'})).toBeInTheDocument();
});
import React from 'react';
const Confirmation = ({children}) => {
return (
<div role="dialog">
<h1>Confirmation</h1>
<div>{children}</div>
<button>OK</button>
</div>
);
};
export default Confirmation;
it('should have an "Cancel" button', () => {
const {getByRole} = render(<Confirmation />);
expect(getByRole('button', {name: 'Cancel'})).toBeInTheDocument();
});
import React from 'react';
const Confirmation = ({children}) => {
return (
<div role="dialog">
<h1>Confirmation</h1>
<div>{children}</div>
<button>OK</button>
<button>Cancel</button>
</div>
);
};
export default Confirmation;
it('should be able to receive a handler for the "OK" button and execute it upon click', () => {
const onConfirmationHandler = jest.fn();
const {getByRole} = render(<Confirmation onConfirmation={onConfirmationHandler} />);
const okButton = getByRole('button', {name: 'OK'});
fireEvent.click(okButton);
expect(onConfirmationHandler).toHaveBeenCalled();
});
import React from 'react';
const Confirmation = ({children, onConfirmation}) => {
return (
<div role="dialog">
<h1>Confirmation</h1>
<div>{children}</div>
<button onClick={onConfirmation}>
OK
</button>
<button>Cancel</button>
</div>
);
};
export default Confirmation;
it('should be able to receive a handler for the "Cancel" button and execute it upon click', () => {
const onCancellationHandler = jest.fn();
const {getByRole} = render(<Confirmation onCancellation={onCancellationHandler} />);
const cancelButton = getByRole('button', {name: 'Cancel'});
fireEvent.click(cancelButton);
expect(onCancellationHandler).toHaveBeenCalled();
});
import React from 'react';
const Confirmation = ({children, onConfirmation, onCancellation}) => {
return (
<div role="dialog">
<h1>Confirmation</h1>
<div>{children}</div>
<button onClick={onConfirmation}>
OK
</button>
<button onClick={onCancellation}>
Cancel
</button>
</div>
);
};
export default Confirmation;
import React from 'react';
import {render, fireEvent} from '@testing-library/react';
import Confirmation from '.';
describe('Confirmation component', () => {
it('should render', () => {
const {getByRole} = render(<Confirmation />);
expect(getByRole('dialog')).toBeInTheDocument();
});
it('should have a title saying "Confirmation"', () => {
const {getByText} = render(<Confirmation />);
expect(getByText('Confirmation')).toBeInTheDocument();
});
it('should have a dynamic confirmation question', () => {
const question = 'Do you confirm?';
const {getByText} = render(<Confirmation>{question}</Confirmation>);
expect(getByText(question)).toBeInTheDocument();
});
it('should have an "OK" button', () => {
const {getByRole} = render(<Confirmation />);
expect(getByRole('button', {name: 'OK'})).toBeInTheDocument();
});
it('should have an "Cancel" button', () => {
const {getByRole} = render(<Confirmation />);
expect(getByRole('button', {name: 'Cancel'})).toBeInTheDocument();
});
it('should be able to receive a handler for the "OK" button and execute it upon click', () => {
const onConfirmationHandler = jest.fn();
const {getByRole} = render(<Confirmation onConfirmation={onConfirmationHandler} />);
const okButton = getByRole('button', {name: 'OK'});
fireEvent.click(okButton);
expect(onConfirmationHandler).toHaveBeenCalled();
});
it('should be able to receive a handler for the "Cancel" button and execute it upon click', () => {
const onCancellationHandler = jest.fn();
const {getByRole} = render(<Confirmation onCancellation={onCancellationHandler} />);
const cancelButton = getByRole('button', {name: 'Cancel'});
fireEvent.click(cancelButton);
expect(onCancellationHandler).toHaveBeenCalled();
});
});