VirtualKeyboard.test.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * Regression for #2616. react-simple-keyboard ships as CommonJS; under the
  3. * bundler's CJS interop the default import can arrive as the module namespace
  4. * object rather than the Keyboard component, so rendering <Keyboard> throws
  5. * React #130 ("Element type is invalid ... got: object"). The on-screen keyboard
  6. * mounts on every SpoolBuddy screen the instant a text input is focused, so the
  7. * crash hit inventory search and the write-tag New Spool fields alike.
  8. */
  9. import { describe, it, expect, afterEach, beforeEach, vi } from 'vitest';
  10. import { render, screen, fireEvent, cleanup } from '@testing-library/react';
  11. import { VirtualKeyboard } from '../../components/VirtualKeyboard';
  12. // focusin schedules a 100ms scrollIntoView on the focused input; jsdom doesn't
  13. // implement it, so stub it or the deferred call throws an unhandled error after
  14. // the test completes.
  15. beforeEach(() => {
  16. Element.prototype.scrollIntoView = vi.fn();
  17. });
  18. afterEach(() => {
  19. cleanup();
  20. vi.restoreAllMocks();
  21. });
  22. describe('VirtualKeyboard (#2616)', () => {
  23. it('renders the keyboard when a text input is focused (no invalid-element-type crash)', () => {
  24. render(
  25. <div>
  26. <input type="text" placeholder="Search spools..." />
  27. <VirtualKeyboard />
  28. </div>,
  29. );
  30. const input = screen.getByPlaceholderText('Search spools...');
  31. // The shell listens on document focusin, so drive a real focus event.
  32. fireEvent.focusIn(input);
  33. // A key from the layout must be on screen — proves <Keyboard> resolved to a
  34. // real component instead of throwing on an object element type.
  35. expect(screen.getByText('q')).toBeInTheDocument();
  36. });
  37. });