|
| 1 | +import { render, screen, fireEvent } from '@testing-library/preact'; |
| 2 | +import { beforeEach, describe, expect, it } from 'vitest'; |
| 3 | +import PlaybackRateButton from '../../src/components/player/PlaybackRateButton'; |
| 4 | +import { createRef } from 'preact'; |
| 5 | + |
| 6 | +// Mock audio element |
| 7 | +const mockAudioElement = { |
| 8 | + playbackRate: 1, |
| 9 | + currentTime: 0, |
| 10 | + duration: 180 |
| 11 | +}; |
| 12 | + |
| 13 | +describe('PlaybackRateButton', () => { |
| 14 | + let audioPlayerRef: any; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + mockAudioElement.playbackRate = 1; |
| 18 | + audioPlayerRef = createRef(); |
| 19 | + audioPlayerRef.current = mockAudioElement; |
| 20 | + }); |
| 21 | + |
| 22 | + it('renders with initial playback rate of 1x', () => { |
| 23 | + render(<PlaybackRateButton audioPlayer={audioPlayerRef} />); |
| 24 | + |
| 25 | + const button = screen.getByRole('button'); |
| 26 | + expect(button).toBeInTheDocument(); |
| 27 | + expect(button).toHaveAttribute('aria-label', 'Playback rate'); |
| 28 | + expect(screen.getByText('1')).toBeInTheDocument(); |
| 29 | + expect(screen.getByText('x')).toBeInTheDocument(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('cycles through playback rates when clicked', () => { |
| 33 | + render(<PlaybackRateButton audioPlayer={audioPlayerRef} />); |
| 34 | + |
| 35 | + const button = screen.getByRole('button'); |
| 36 | + |
| 37 | + // Initial state: 1x |
| 38 | + expect(screen.getByText('1')).toBeInTheDocument(); |
| 39 | + expect(mockAudioElement.playbackRate).toBe(1); |
| 40 | + |
| 41 | + // Click 1: should go to 1.2x |
| 42 | + fireEvent.click(button); |
| 43 | + expect(screen.getByText('1.2')).toBeInTheDocument(); |
| 44 | + expect(mockAudioElement.playbackRate).toBe(1.2); |
| 45 | + |
| 46 | + // Click 2: should go to 1.5x |
| 47 | + fireEvent.click(button); |
| 48 | + expect(screen.getByText('1.5')).toBeInTheDocument(); |
| 49 | + expect(mockAudioElement.playbackRate).toBe(1.5); |
| 50 | + |
| 51 | + // Click 3: should go to 2x |
| 52 | + fireEvent.click(button); |
| 53 | + expect(screen.getByText('2')).toBeInTheDocument(); |
| 54 | + expect(mockAudioElement.playbackRate).toBe(2); |
| 55 | + |
| 56 | + // Click 4: should cycle back to 1x |
| 57 | + fireEvent.click(button); |
| 58 | + expect(screen.getByText('1')).toBeInTheDocument(); |
| 59 | + expect(mockAudioElement.playbackRate).toBe(1); |
| 60 | + }); |
| 61 | + |
| 62 | + it('handles null audio player gracefully', () => { |
| 63 | + const nullRef = createRef(); |
| 64 | + nullRef.current = null; |
| 65 | + |
| 66 | + render(<PlaybackRateButton audioPlayer={nullRef} />); |
| 67 | + |
| 68 | + const button = screen.getByRole('button'); |
| 69 | + |
| 70 | + // Should render without crashing |
| 71 | + expect(button).toBeInTheDocument(); |
| 72 | + expect(screen.getByText('1')).toBeInTheDocument(); |
| 73 | + |
| 74 | + // Clicking should not crash when audio player is null |
| 75 | + expect(() => fireEvent.click(button)).not.toThrow(); |
| 76 | + expect(screen.getByText('1.2')).toBeInTheDocument(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('has proper styling classes', () => { |
| 80 | + render(<PlaybackRateButton audioPlayer={audioPlayerRef} />); |
| 81 | + |
| 82 | + const button = screen.getByRole('button'); |
| 83 | + expect(button).toHaveClass('gradient-icon'); |
| 84 | + expect(button).toHaveClass('relative'); |
| 85 | + expect(button).toHaveClass('rounded-md'); |
| 86 | + expect(button).toHaveClass('transition-colors'); |
| 87 | + expect(button).toHaveClass('focus:outline-hidden'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('displays rate with x suffix correctly', () => { |
| 91 | + render(<PlaybackRateButton audioPlayer={audioPlayerRef} />); |
| 92 | + |
| 93 | + const button = screen.getByRole('button'); |
| 94 | + |
| 95 | + // Test different rates maintain x suffix |
| 96 | + fireEvent.click(button); // 1.2x |
| 97 | + expect(screen.getByText('1.2')).toBeInTheDocument(); |
| 98 | + expect(screen.getByText('x')).toBeInTheDocument(); |
| 99 | + |
| 100 | + fireEvent.click(button); // 1.5x |
| 101 | + expect(screen.getByText('1.5')).toBeInTheDocument(); |
| 102 | + expect(screen.getByText('x')).toBeInTheDocument(); |
| 103 | + |
| 104 | + fireEvent.click(button); // 2x |
| 105 | + expect(screen.getByText('2')).toBeInTheDocument(); |
| 106 | + expect(screen.getByText('x')).toBeInTheDocument(); |
| 107 | + }); |
| 108 | + |
| 109 | + it('maintains state independently across renders', () => { |
| 110 | + const { rerender } = render( |
| 111 | + <PlaybackRateButton audioPlayer={audioPlayerRef} /> |
| 112 | + ); |
| 113 | + |
| 114 | + const button = screen.getByRole('button'); |
| 115 | + |
| 116 | + // Change to 1.5x |
| 117 | + fireEvent.click(button); |
| 118 | + fireEvent.click(button); |
| 119 | + expect(screen.getByText('1.5')).toBeInTheDocument(); |
| 120 | + |
| 121 | + // Rerender should maintain the same state |
| 122 | + rerender(<PlaybackRateButton audioPlayer={audioPlayerRef} />); |
| 123 | + expect(screen.getByText('1.5')).toBeInTheDocument(); |
| 124 | + }); |
| 125 | +}); |
0 commit comments