ToastContext.test.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * Tests for ToastContext's post-unmount safety guards.
  3. *
  4. * Regression: a login response handler calling showToast AFTER the provider
  5. * had already been unmounted by Vitest's afterEach scheduled a 3s setTimeout
  6. * that fired during test teardown. The callback's setToasts then tried to
  7. * schedule a React update against a torn-down jsdom, producing
  8. * "window is not defined" as an uncaught exception.
  9. *
  10. * The provider now gates every setToasts call on an isMountedRef and
  11. * re-checks inside the auto-dismiss setTimeout callback so stale async
  12. * paths no-op instead of crashing.
  13. */
  14. import { describe, it, expect, beforeEach, vi } from 'vitest';
  15. import { act, render, renderHook } from '@testing-library/react';
  16. import { type ReactNode } from 'react';
  17. import { ToastProvider, useToast } from '../../contexts/ToastContext';
  18. function Wrapper({ children }: { children: ReactNode }) {
  19. return <ToastProvider>{children}</ToastProvider>;
  20. }
  21. describe('ToastContext post-unmount safety', () => {
  22. beforeEach(() => {
  23. vi.useRealTimers();
  24. });
  25. it('does not crash when showToast is called after unmount', () => {
  26. const { result, unmount } = renderHook(() => useToast(), { wrapper: Wrapper });
  27. // Capture the callbacks BEFORE unmount — a real stale-closure scenario.
  28. // (Async handlers that kicked off before unmount keep their captured
  29. // context value and will invoke this function after we tear down.)
  30. const { showToast } = result.current;
  31. unmount();
  32. // Post-unmount invocation is now a no-op; must not throw.
  33. expect(() => showToast('delayed error message', 'error')).not.toThrow();
  34. });
  35. it('does not invoke setToasts when the auto-dismiss timer fires after unmount', async () => {
  36. vi.useFakeTimers();
  37. const { result, unmount } = renderHook(() => useToast(), { wrapper: Wrapper });
  38. act(() => {
  39. result.current.showToast('will outlive the provider', 'error');
  40. });
  41. // Unmount BEFORE the 3s timer fires — the unmount effect clears pending
  42. // timers, but a belt-and-braces check inside the timer callback (for
  43. // cases where the timer was scheduled post-unmount) must also hold.
  44. unmount();
  45. // Advance past the 3s auto-dismiss window. If the guard isn't in place
  46. // this would throw "window is not defined" in a torn-down jsdom; we
  47. // simulate by asserting no error propagates.
  48. expect(() => {
  49. vi.advanceTimersByTime(5000);
  50. }).not.toThrow();
  51. vi.useRealTimers();
  52. });
  53. it('post-unmount showPersistentToast and dismissToast are no-ops', () => {
  54. const { result, unmount } = renderHook(() => useToast(), { wrapper: Wrapper });
  55. const { showPersistentToast, dismissToast } = result.current;
  56. unmount();
  57. // Both must short-circuit rather than attempt setState on a dead tree.
  58. expect(() => showPersistentToast('orphan', 'still here', 'info')).not.toThrow();
  59. expect(() => dismissToast('orphan')).not.toThrow();
  60. });
  61. it('normal showToast flow still displays and auto-dismisses while mounted', () => {
  62. vi.useFakeTimers();
  63. const { result } = renderHook(() => useToast(), { wrapper: Wrapper });
  64. act(() => {
  65. result.current.showToast('mounted path works', 'success');
  66. });
  67. // No easy way to read toast DOM from the hook alone; assert the timer
  68. // ran without throwing — that proves the isMountedRef guard didn't
  69. // incorrectly short-circuit the mounted path.
  70. expect(() => {
  71. act(() => {
  72. vi.advanceTimersByTime(3500);
  73. });
  74. }).not.toThrow();
  75. vi.useRealTimers();
  76. });
  77. });
  78. describe('ToastContext viewport suppression', () => {
  79. // The kiosk layout flips setViewportSuppressed(true) on mount so the
  80. // SpoolBuddy display stays free of main-app toasts (login flows, etc.).
  81. // Verify the gate hides the visible viewport
  82. // without affecting the underlying state machine.
  83. function ViewportProbe() {
  84. const { showToast, setViewportSuppressed } = useToast();
  85. return (
  86. <>
  87. <button data-testid="show-toast" onClick={() => showToast('hello', 'success')} />
  88. <button data-testid="suppress-on" onClick={() => setViewportSuppressed(true)} />
  89. <button data-testid="suppress-off" onClick={() => setViewportSuppressed(false)} />
  90. </>
  91. );
  92. }
  93. it('hides the visible toast viewport when suppressed but keeps state alive', () => {
  94. const { container, getByTestId } = render(
  95. <ToastProvider>
  96. <ViewportProbe />
  97. </ToastProvider>
  98. );
  99. // Toast viewport is the fixed-position container with bottom-4 right-20.
  100. const findViewport = () => container.querySelector('div.fixed.bottom-4.right-20');
  101. expect(findViewport()?.className).not.toContain('hidden');
  102. act(() => {
  103. getByTestId('suppress-on').click();
  104. });
  105. expect(findViewport()?.className).toContain('hidden');
  106. // State is unaffected — emitting a toast while suppressed is fine; the
  107. // state container exists, just hidden.
  108. act(() => {
  109. getByTestId('show-toast').click();
  110. });
  111. expect(findViewport()?.className).toContain('hidden');
  112. // Restore on unmount of the kiosk layout (or via the setter directly).
  113. act(() => {
  114. getByTestId('suppress-off').click();
  115. });
  116. expect(findViewport()?.className).not.toContain('hidden');
  117. });
  118. });