import { render, screen } from '@testing-library/react';
import PromoCard, { PromoCardProps } from './PromoCard';
import PromoCardGroup, { PromoCardGroupProps } from './PromoCardGroup';
describe('PromoCardGroup', () => {
const defaultCardProps: PromoCardProps = {
title: 'Test Card',
description: 'Test Description',
imageSource: 'https://via.placeholder.com/150',
type: 'radio',
};
const defaultProps = {
testId: 'test-promo-card-group',
children: [
,
,
],
};
let rerenderPromoCardGroup: (props?: PromoCardGroupProps) => void;
beforeEach(() => {
const { rerender } = render();
rerenderPromoCardGroup = (props?: PromoCardGroupProps) => {
rerender();
};
});
it('renders', () => {
const props = {
...defaultProps,
children: [
,
,
],
};
expect(screen.getByTestId('test-promo-card-group')).toBeInTheDocument();
expect(screen.getByTestId('test-promo-card-group')).toHaveClass('np-CardGroup');
expect(screen.getByRole('radiogroup')).toBeInTheDocument();
// Change children
rerenderPromoCardGroup(props);
expect(screen.getByTestId('test-4')).toBeInTheDocument();
});
it('sets the `defaultValue` for the group', () => {
const props = {
...defaultProps,
defaultChecked: 'radio-1',
};
expect(screen.getByTestId('test-1')).not.toHaveClass('is-checked');
// Change children
rerenderPromoCardGroup(props);
expect(screen.getByTestId('test-1')).toHaveClass('is-checked');
});
it('not to render as a `radiogroup` if we have a mix of PromoCard types', () => {
const props = {
...defaultProps,
children: [
,
,
],
};
expect(screen.getByRole('radiogroup')).toBeInTheDocument();
// Change children
rerenderPromoCardGroup(props);
expect(screen.queryByRole('radiogroup')).not.toBeInTheDocument();
expect(screen.getByTestId('test-4')).toBeInTheDocument();
});
it('applies `is-disabled` class to all children when isDisabled is true', () => {
const props = {
...defaultProps,
isDisabled: true,
};
expect(screen.getByTestId('test-1')).not.toHaveClass('is-disabled');
expect(screen.getByTestId('test-2')).not.toHaveClass('is-disabled');
// Change children
rerenderPromoCardGroup(props);
expect(screen.getByTestId('test-1')).toHaveClass('is-disabled');
expect(screen.getByTestId('test-2')).toHaveClass('is-disabled');
});
});