import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import { AnimationsProvider, useAnimationsConfig, useHasAnimations } from '../AnimationsProvider';
// Test component that uses the useAnimationsConfig hook
const TestConfigComponent = () => {
const { hasAnimations } = useAnimationsConfig();
return
{hasAnimations ? 'animations-enabled' : 'animations-disabled'}
;
};
// Test component that uses the useHasAnimations hook with a local prop
const TestHasAnimationsComponent = ({ hasAnimations: hasAnimationsProp }: { hasAnimations?: boolean }) => {
const hasAnimations = useHasAnimations(hasAnimationsProp);
return {hasAnimations ? 'animations-enabled' : 'animations-disabled'}
;
};
test('renders children correctly', () => {
render(
Test Child
);
expect(screen.getByTestId('child')).toBeInTheDocument();
expect(screen.getByTestId('child')).toHaveTextContent('Test Child');
});
test('provides explicit config', () => {
render(
);
expect(screen.getByTestId('config')).toHaveTextContent('animations-disabled');
});
test('provides animations enabled config', () => {
render(
);
expect(screen.getByTestId('config')).toHaveTextContent('animations-enabled');
});
test('provides animations disabled config', () => {
render(
);
expect(screen.getByTestId('config')).toHaveTextContent('animations-disabled');
});
test('useAnimationsConfig returns default config when no provider', () => {
render();
expect(screen.getByTestId('config')).toHaveTextContent('animations-disabled');
});
test('useHasAnimations uses context when no local prop provided', () => {
render(
);
expect(screen.getByTestId('has-animations')).toHaveTextContent('animations-enabled');
});
test('useHasAnimations uses local prop when provided (true)', () => {
render(
);
expect(screen.getByTestId('has-animations')).toHaveTextContent('animations-enabled');
});
test('useHasAnimations uses local prop when provided (false)', () => {
render(
);
expect(screen.getByTestId('has-animations')).toHaveTextContent('animations-disabled');
});
test('useHasAnimations works without provider', () => {
render();
expect(screen.getByTestId('has-animations')).toHaveTextContent('animations-disabled');
});
test('useHasAnimations with local prop and no provider', () => {
render();
expect(screen.getByTestId('has-animations')).toHaveTextContent('animations-enabled');
});
test('works in app-level scenario with multiple components', () => {
// Simulate realistic app-level usage
const MockApp = () => (
{/* Component override */}
);
render(
);
const configs = screen.getAllByTestId('config');
const hasAnimationsComponents = screen.getAllByTestId('has-animations');
expect(configs[0]).toHaveTextContent('animations-enabled'); // Header uses provider
expect(hasAnimationsComponents[0]).toHaveTextContent('animations-enabled'); // Main uses provider
expect(hasAnimationsComponents[1]).toHaveTextContent('animations-disabled'); // Component override
});
test('handles dynamic context changes', () => {
const TestComponent = ({ hasAnimations }: { hasAnimations: boolean }) => (
);
const { rerender } = render();
// Initially animations enabled
expect(screen.getByTestId('config')).toHaveTextContent('animations-enabled');
expect(screen.getByTestId('has-animations')).toHaveTextContent('animations-enabled');
// Change to disabled
rerender();
expect(screen.getByTestId('config')).toHaveTextContent('animations-disabled');
expect(screen.getByTestId('has-animations')).toHaveTextContent('animations-disabled');
// Change back to enabled
rerender();
expect(screen.getByTestId('config')).toHaveTextContent('animations-enabled');
expect(screen.getByTestId('has-animations')).toHaveTextContent('animations-enabled');
});