interopDefault.test.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * Unit tests for resolveInteropDefault (#2616).
  3. *
  4. * The browser build resolved react-simple-keyboard's CommonJS default import to
  5. * the module namespace object ({ KeyboardReact, default }) instead of the
  6. * component, so <Keyboard> threw React #130 ("got: object"). vitest's own interop
  7. * happens to hand back the component, so a render test can't catch the
  8. * regression — these assert the resolver directly against both shapes.
  9. */
  10. import { describe, it, expect } from 'vitest';
  11. import { resolveInteropDefault } from '../../utils/interopDefault';
  12. const Comp = function Keyboard() {
  13. return null;
  14. };
  15. describe('resolveInteropDefault', () => {
  16. it('returns a bare function component unchanged', () => {
  17. expect(resolveInteropDefault(Comp)).toBe(Comp);
  18. });
  19. it('unwraps the CJS interop namespace object via .default (the #2616 shape)', () => {
  20. const moduleObject = { default: Comp, KeyboardReact: Comp };
  21. expect(resolveInteropDefault(moduleObject, ['KeyboardReact'])).toBe(Comp);
  22. });
  23. it('falls back to a named export when there is no .default', () => {
  24. const moduleObject = { KeyboardReact: Comp };
  25. expect(resolveInteropDefault(moduleObject, ['KeyboardReact'])).toBe(Comp);
  26. });
  27. it('leaves a forwardRef/memo object (with $$typeof) untouched', () => {
  28. const forwardRefLike = { $$typeof: Symbol.for('react.forward_ref'), render: Comp };
  29. expect(resolveInteropDefault(forwardRefLike)).toBe(forwardRefLike);
  30. });
  31. it('returns a string tag unchanged', () => {
  32. expect(resolveInteropDefault('div')).toBe('div');
  33. });
  34. it('returns the value unchanged when nothing usable is found', () => {
  35. const opaque = { something: 1 };
  36. expect(resolveInteropDefault(opaque, ['KeyboardReact'])).toBe(opaque);
  37. });
  38. });