import { renderWithProviders as render, screen, fireEvent } from '../../test/render';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { FloatingMediaWrapper } from './FloatingMediaWrapper';
import React from 'react';
// The component reads/writes `xertica-media-player-${playerId}` in localStorage
// to persist the floating window's position/size. Clear it between tests so one
// test's saved state can't leak into another's initial render.
beforeEach(() => {
localStorage.clear();
});
describe('FloatingMediaWrapper', () => {
it('renders children inline (no portal) when not floating', () => {
render(
content
);
expect(screen.getByTestId('media-content')).toBeInTheDocument();
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
});
it('renders a placeholder plus a portaled floating player when floating', () => {
render(
content
);
// Placeholder shown in the page flow
expect(screen.getByText('Reproduzindo em janela flutuante')).toBeInTheDocument();
// Portaled floating dialog, rendered into document.body
const dialog = screen.getByRole('dialog');
expect(dialog).toBeInTheDocument();
expect(dialog).toHaveAttribute('aria-label', 'Reproduzindo Mídia');
// Children are rendered once, inside the portal
expect(screen.getByTestId('media-content')).toBeInTheDocument();
});
it('uses the title prop for the dialog aria-label and header text when provided', () => {
render(
content
);
expect(screen.getByRole('dialog')).toHaveAttribute('aria-label', 'My Podcast');
expect(screen.getByText('My Podcast')).toBeInTheDocument();
});
it('calls setIsFloating(false) when "Restore to page" is clicked', () => {
const setIsFloating = vi.fn();
render(
content
);
fireEvent.click(screen.getByRole('button', { name: 'Restaurar para a página' }));
expect(setIsFloating).toHaveBeenCalledWith(false);
});
it('calls onCloseMedia (preferred over onClose) when the close button is clicked', () => {
const onClose = vi.fn();
const onCloseMedia = vi.fn();
render(
content
);
fireEvent.click(screen.getByLabelText('Fechar player'));
expect(onCloseMedia).toHaveBeenCalledTimes(1);
expect(onClose).not.toHaveBeenCalled();
});
it('falls back to onClose when onCloseMedia is not provided', () => {
const onClose = vi.fn();
render(
content
);
fireEvent.click(screen.getByLabelText('Fechar player'));
expect(onClose).toHaveBeenCalledTimes(1);
});
it('restores position and size from a previously saved localStorage entry', () => {
localStorage.setItem(
'xertica-media-player-restore-test',
JSON.stringify({ position: { x: 42, y: 84 }, size: { width: 400, height: 225 } })
);
render(
content
);
const dialog = screen.getByRole('dialog');
expect(dialog).toHaveStyle({
transform: 'translate(42px, 84px)',
width: '400px',
height: '225px',
});
});
});