| | |
| | |
| | |
| | |
| | |
| |
|
| | import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| | import { renderHook, act } from '@testing-library/react'; |
| | import { |
| | usePhraseCycler, |
| | WITTY_LOADING_PHRASES, |
| | PHRASE_CHANGE_INTERVAL_MS, |
| | } from './usePhraseCycler.js'; |
| |
|
| | describe('usePhraseCycler', () => { |
| | beforeEach(() => { |
| | vi.useFakeTimers(); |
| | }); |
| |
|
| | afterEach(() => { |
| | vi.restoreAllMocks(); |
| | }); |
| |
|
| | it('should initialize with the first witty phrase when not active and not waiting', () => { |
| | const { result } = renderHook(() => usePhraseCycler(false, false)); |
| | expect(result.current).toBe(WITTY_LOADING_PHRASES[0]); |
| | }); |
| |
|
| | it('should show "Waiting for user confirmation..." when isWaiting is true', () => { |
| | const { result, rerender } = renderHook( |
| | ({ isActive, isWaiting }) => usePhraseCycler(isActive, isWaiting), |
| | { initialProps: { isActive: true, isWaiting: false } }, |
| | ); |
| | rerender({ isActive: true, isWaiting: true }); |
| | expect(result.current).toBe('Waiting for user confirmation...'); |
| | }); |
| |
|
| | it('should not cycle phrases if isActive is false and not waiting', () => { |
| | const { result } = renderHook(() => usePhraseCycler(false, false)); |
| | act(() => { |
| | vi.advanceTimersByTime(PHRASE_CHANGE_INTERVAL_MS * 2); |
| | }); |
| | expect(result.current).toBe(WITTY_LOADING_PHRASES[0]); |
| | }); |
| |
|
| | it('should cycle through witty phrases when isActive is true and not waiting', () => { |
| | const { result } = renderHook(() => usePhraseCycler(true, false)); |
| | |
| | expect(WITTY_LOADING_PHRASES).toContain(result.current); |
| | const _initialPhrase = result.current; |
| |
|
| | act(() => { |
| | vi.advanceTimersByTime(PHRASE_CHANGE_INTERVAL_MS); |
| | }); |
| | |
| | expect(WITTY_LOADING_PHRASES).toContain(result.current); |
| |
|
| | const _secondPhrase = result.current; |
| | act(() => { |
| | vi.advanceTimersByTime(PHRASE_CHANGE_INTERVAL_MS); |
| | }); |
| | expect(WITTY_LOADING_PHRASES).toContain(result.current); |
| | }); |
| |
|
| | it('should reset to a witty phrase when isActive becomes true after being false (and not waiting)', () => { |
| | |
| | if (WITTY_LOADING_PHRASES.length < 2) { |
| | return; |
| | } |
| |
|
| | |
| | let callCount = 0; |
| | vi.spyOn(Math, 'random').mockImplementation(() => { |
| | |
| | const val = callCount % 2; |
| | callCount++; |
| | return val / WITTY_LOADING_PHRASES.length; |
| | }); |
| |
|
| | const { result, rerender } = renderHook( |
| | ({ isActive, isWaiting }) => usePhraseCycler(isActive, isWaiting), |
| | { initialProps: { isActive: false, isWaiting: false } }, |
| | ); |
| |
|
| | |
| | rerender({ isActive: true, isWaiting: false }); |
| | const firstActivePhrase = result.current; |
| | expect(WITTY_LOADING_PHRASES).toContain(firstActivePhrase); |
| | |
| | expect(firstActivePhrase).toBe(WITTY_LOADING_PHRASES[0]); |
| |
|
| | act(() => { |
| | vi.advanceTimersByTime(PHRASE_CHANGE_INTERVAL_MS); |
| | }); |
| |
|
| | |
| | expect(result.current).not.toBe(firstActivePhrase); |
| | expect(result.current).toBe(WITTY_LOADING_PHRASES[1]); |
| |
|
| | |
| | rerender({ isActive: false, isWaiting: false }); |
| | expect(result.current).toBe(WITTY_LOADING_PHRASES[0]); |
| |
|
| | |
| | act(() => { |
| | rerender({ isActive: true, isWaiting: false }); |
| | }); |
| | |
| | expect(result.current).toBe(WITTY_LOADING_PHRASES[0]); |
| | }); |
| |
|
| | it('should clear phrase interval on unmount when active', () => { |
| | const { unmount } = renderHook(() => usePhraseCycler(true, false)); |
| | const clearIntervalSpy = vi.spyOn(global, 'clearInterval'); |
| | unmount(); |
| | expect(clearIntervalSpy).toHaveBeenCalledOnce(); |
| | }); |
| |
|
| | it('should reset to a witty phrase when transitioning from waiting to active', () => { |
| | const { result, rerender } = renderHook( |
| | ({ isActive, isWaiting }) => usePhraseCycler(isActive, isWaiting), |
| | { initialProps: { isActive: true, isWaiting: false } }, |
| | ); |
| |
|
| | const _initialPhrase = result.current; |
| | expect(WITTY_LOADING_PHRASES).toContain(_initialPhrase); |
| |
|
| | |
| | act(() => { |
| | vi.advanceTimersByTime(PHRASE_CHANGE_INTERVAL_MS); |
| | }); |
| | if (WITTY_LOADING_PHRASES.length > 1) { |
| | |
| | } |
| | expect(WITTY_LOADING_PHRASES).toContain(result.current); |
| |
|
| | |
| | rerender({ isActive: false, isWaiting: true }); |
| | expect(result.current).toBe('Waiting for user confirmation...'); |
| |
|
| | |
| | rerender({ isActive: true, isWaiting: false }); |
| | expect(WITTY_LOADING_PHRASES).toContain(result.current); |
| | }); |
| | }); |
| |
|