Dialog.test.jsx 1013 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { h } from 'preact';
  2. import Dialog from '../Dialog';
  3. import { fireEvent, render, screen } from '@testing-library/preact';
  4. describe('Dialog', () => {
  5. let portal;
  6. beforeAll(() => {
  7. portal = document.createElement('div');
  8. portal.id = 'dialogs';
  9. document.body.appendChild(portal);
  10. });
  11. afterAll(() => {
  12. document.body.removeChild(portal);
  13. });
  14. test('renders to a portal', async () => {
  15. render(<Dialog title="Tacos" text="This is the dialog" />);
  16. expect(screen.getByText('Tacos')).toBeInTheDocument();
  17. expect(screen.getByRole('modal').closest('#dialogs')).not.toBeNull();
  18. });
  19. test('renders action buttons', async () => {
  20. const handleClick = jest.fn();
  21. render(
  22. <Dialog
  23. actions={[
  24. { color: 'red', text: 'Delete' },
  25. { text: 'Okay', onClick: handleClick },
  26. ]}
  27. title="Tacos"
  28. />
  29. );
  30. fireEvent.click(screen.getByRole('button', { name: 'Okay' }));
  31. expect(handleClick).toHaveBeenCalled();
  32. });
  33. });