Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions spec/components/Carousel/Carousel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,66 @@ describe('Carousel component', () => {
expect(screen.queryByText('Product 1')).not.toBeInTheDocument();
});

test('Ensure ProductCard componentOverrides are passed down', () => {
render(
<CioCarousel
items={mockProducts}
componentOverrides={{
item: {
productCard: {
reactNode: (props) => (
<div className='custom-product-card'>
<h3 data-testid='custom-product-name'>{props.product?.name}</h3>
<span data-testid='custom-product-price'>${props.product?.price}</span>
</div>
),
},
},
}}
/>,
);

// All products should use the custom override
const customNames = screen.getAllByTestId('custom-product-name');
const customPrices = screen.getAllByTestId('custom-product-price');

expect(customNames).toHaveLength(3);
expect(customNames[0].textContent).toEqual(mockProducts[0].name);
expect(customNames[1].textContent).toEqual(mockProducts[1].name);
expect(customNames[2].textContent).toEqual(mockProducts[2].name);

expect(customPrices).toHaveLength(3);
expect(customPrices[0].textContent).toEqual(`$${mockProducts[0].price}`);
expect(customPrices[1].textContent).toEqual(`$${mockProducts[1].price}`);
expect(customPrices[2].textContent).toEqual(`$${mockProducts[2].price}`);
});

test('Ensure nested ProductCard componentOverrides are passed down', () => {
render(
<CioCarousel
items={mockProducts}
componentOverrides={{
item: {
productCard: {
image: {
reactNode: (props) => (
<div data-testid='custom-image' className='custom-image-override'>
{props.product?.imageUrl}
</div>
),
},
},
},
}}
/>,
);

// Nested override (ProductCard.image) should be applied
const customImages = screen.getAllByTestId('custom-image');
expect(customImages).toHaveLength(3);
expect(customImages[0].textContent).toEqual(mockProducts[0].imageUrl);
});

test('renders component override for navigation buttons', () => {
render(
<CioCarousel
Expand Down
6 changes: 5 additions & 1 deletion src/components/carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,11 @@ function Carousel<T = Product>(props: CarouselOpts<T>) {

return (
<CarouselItem key={index} item={product} index={index}>
<ProductCard product={product} className='w-full h-full' />
<ProductCard
product={product}
className='w-full h-full'
componentOverrides={componentOverrides?.item?.productCard}
/>
</CarouselItem>
);
})}
Expand Down
6 changes: 4 additions & 2 deletions src/types/carouselTypes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Product } from '@/types/productCardTypes';
import type { Product, ProductCardOverrides } from '@/types/productCardTypes';
import type {
ComponentOverrideProps,
IncludeComponentOverrides,
Expand Down Expand Up @@ -66,7 +66,9 @@ export type CarouselItemRenderProps<T = Product> = CarouselRenderProps<T> & {

export type CarouselOverrides<T = Product> = ComponentOverrideProps<CarouselRenderProps<T>> & {
content?: ComponentOverrideProps<CarouselRenderProps<T>>;
item?: ComponentOverrideProps<CarouselItemRenderProps<T>>;
item?: ComponentOverrideProps<CarouselItemRenderProps<T>> & {
productCard?: ProductCardOverrides;
};
previous?: ComponentOverrideProps<CarouselRenderProps<T>>;
next?: ComponentOverrideProps<CarouselRenderProps<T>>;
};
Expand Down
Loading