AlertModal.test.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * Tests for AlertModal — the acknowledge-only error modal used to surface
  3. * slice failures (and other must-read errors) that a toast would auto-dismiss
  4. * before they can be read.
  5. */
  6. import React from 'react';
  7. import { describe, it, expect, vi } from 'vitest';
  8. import { render, screen, fireEvent } from '@testing-library/react';
  9. import { I18nextProvider } from 'react-i18next';
  10. import i18n from '../../i18n';
  11. import { AlertModal } from '../../components/AlertModal';
  12. function renderModal(props?: Partial<Parameters<typeof AlertModal>[0]>) {
  13. const onClose = vi.fn();
  14. render(
  15. <I18nextProvider i18n={i18n}>
  16. <AlertModal
  17. title="Slicing failed"
  18. subtitle="Mecha Mewtwo.3mf"
  19. message="Some objects are located over the boundary of the heated bed."
  20. onClose={onClose}
  21. {...props}
  22. />
  23. </I18nextProvider>,
  24. );
  25. return { onClose };
  26. }
  27. describe('AlertModal', () => {
  28. it('renders the title, subtitle and message', () => {
  29. renderModal();
  30. expect(screen.getByText('Slicing failed')).toBeInTheDocument();
  31. expect(screen.getByText('Mecha Mewtwo.3mf')).toBeInTheDocument();
  32. expect(
  33. screen.getByText('Some objects are located over the boundary of the heated bed.'),
  34. ).toBeInTheDocument();
  35. });
  36. it('calls onClose when the Close button is clicked', () => {
  37. const { onClose } = renderModal();
  38. fireEvent.click(screen.getByRole('button', { name: /close/i }));
  39. expect(onClose).toHaveBeenCalledTimes(1);
  40. });
  41. it('calls onClose when Escape is pressed', () => {
  42. const { onClose } = renderModal();
  43. fireEvent.keyDown(window, { key: 'Escape' });
  44. expect(onClose).toHaveBeenCalledTimes(1);
  45. });
  46. it('omits the subtitle line when no subtitle is given', () => {
  47. renderModal({ subtitle: undefined });
  48. expect(screen.queryByText('Mecha Mewtwo.3mf')).not.toBeInTheDocument();
  49. expect(screen.getByText('Slicing failed')).toBeInTheDocument();
  50. });
  51. });