import React from 'react' import { render, screen } from '@testing-library/react' import { describe, it, expect, vi } from 'vitest' import { TestContext } from 'ra-test' import { RecordContextProvider } from 'react-admin' import { PlaylistLove, ToggleField, ToggleAutoImport } from './PlaylistList' vi.mock('../config', () => ({ default: { enableFavourites: true }, })) vi.mock('../common', () => ({ LoveButton: ({ record, resource }) => ( ), isWritable: (ownerId) => ownerId === 'me', })) describe('', () => { it('renders a LoveButton bound to the playlist resource', () => { render() const btn = screen.getByTestId('love') expect(btn.getAttribute('data-resource')).toBe('playlist') expect(btn.textContent).toBe('starred') }) it('exposes datagrid header props so the column renders unsorted', () => { // The Datagrid reads these off the element; the wrapper body must not // forward them to the button (which would leak onto the DOM). expect(PlaylistLove.defaultProps).toEqual({ source: 'starred', sortable: false, }) }) }) // react-admin evicts records older than 10 minutes while the list still holds // their ids, so rows can render with no record. describe('playlist toggles without a record', () => { it(' renders nothing', () => { const { container } = render( , ) expect(container.innerHTML).toBe('') }) it(' renders nothing', () => { const { container } = render( , ) expect(container.innerHTML).toBe('') }) }) // Secondary is a surface color in many themes, so these toggles must use primary describe('', () => { const renderToggle = (record) => render( , ) it.each([ ['owner', 'me', false], ['non-owner', 'someone-else', true], ])('renders a primary-colored switch for the %s', (_, ownerId, disabled) => { renderToggle({ id: 'pl-1', public: true, ownerId }) const input = screen.getByRole('checkbox') const switchBase = input.closest('.MuiSwitch-switchBase') expect(input.checked).toBe(true) expect(input.disabled).toBe(disabled) expect(switchBase.classList).toContain('MuiSwitch-colorPrimary') expect(switchBase.classList).not.toContain('MuiSwitch-colorSecondary') }) })