Skip to content

Commit 92f760b

Browse files
authored
Support getResultCardImage url for a custom image url (#223)
* Support getResultCardImage url for a custom image url * pass result instead of imageUrl
1 parent 71805b9 commit 92f760b

File tree

5 files changed

+21
-1
lines changed

5 files changed

+21
-1
lines changed

spec/components/ResultCard/ResultCard.server.test.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe(`${ResultCard.name} client`, () => {
1515
ratingCountKey: 'rating_count',
1616
ratingScoreKey: 'rating_score',
1717
renderResultCardPriceDetails: jest.fn().mockReturnValue(<div>Price Details</div>),
18+
getResultCardImageUrl: jest.fn().mockReturnValue('www.custom_img.com'),
1819
};
1920

2021
const contextMocks: Partial<QuizContextValue> = {
@@ -38,6 +39,11 @@ describe(`${ResultCard.name} client`, () => {
3839
expect(view).toContain('Price Details');
3940
});
4041

42+
it('renders custom image url', () => {
43+
const view = renderToString(<Subject {...props} />);
44+
expect(view).toContain('www.custom_img.com');
45+
});
46+
4147
it('calls default price details', () => {
4248
const view = renderToString(<Subject {...props} renderResultCardPriceDetails={undefined} />);
4349
expect(view).toContain('80');

spec/components/ResultCard/ResultCard.test.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe(`${ResultCard.name} client`, () => {
1515
ratingCountKey: 'rating_count',
1616
ratingScoreKey: 'rating_score',
1717
renderResultCardPriceDetails: jest.fn().mockReturnValue(<div>Price Details</div>),
18+
getResultCardImageUrl: jest.fn().mockReturnValue('www.custom_img.com'),
1819
};
1920

2021
const contextMocks: Partial<QuizContextValue> = {
@@ -59,6 +60,11 @@ describe(`${ResultCard.name} client`, () => {
5960
render(<Subject {...props} />);
6061
expect(screen.queryByLabelText('Add to favorites')).not.toBeInTheDocument();
6162
});
63+
64+
it('renders custom image url', () => {
65+
render(<Subject {...props} />);
66+
expect(screen.getByRole('img').getAttribute('src')).toEqual('www.custom_img.com');
67+
});
6268
});
6369

6470
describe('without context function', () => {

src/components/ResultCard/ResultCard.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ interface ResultCardOptions {
1313
ratingScoreKey?: string;
1414
resultPosition: number;
1515
renderResultCardPriceDetails?: (result: QuizResultDataPartial) => JSX.Element;
16+
getResultCardImageUrl?: (result: QuizResultDataPartial) => string;
1617
}
1718

1819
export default function ResultCard(props: ResultCardOptions) {
@@ -24,6 +25,7 @@ export default function ResultCard(props: ResultCardOptions) {
2425
ratingCountKey,
2526
ratingScoreKey,
2627
renderResultCardPriceDetails,
28+
getResultCardImageUrl,
2729
} = props;
2830
const {
2931
customAddToFavoritesCallback,
@@ -48,7 +50,10 @@ export default function ResultCard(props: ResultCardOptions) {
4850
const resultCardContent = () => (
4951
<>
5052
<div className='cio-result-card-image'>
51-
<img src={result.data?.image_url} alt='product' />
53+
<img
54+
src={getResultCardImageUrl ? getResultCardImageUrl(result) : result.data?.image_url}
55+
alt='product'
56+
/>
5257
</div>
5358
<div className='cio-result-card-text'>
5459
<p className='cio-result-card-title'>{result.value}</p>

src/components/Results/Results.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ function Results(props: ResultsProps) {
1313
resultCardRatingScoreKey,
1414
renderResultCardPriceDetails,
1515
renderResultCard,
16+
getResultCardImageUrl,
1617
} = props;
1718

1819
const { state, getAddToCartButtonProps, getAddToFavoritesButtonProps, getQuizResultLinkProps } =
@@ -32,6 +33,7 @@ function Results(props: ResultsProps) {
3233
ratingCountKey={resultCardRatingCountKey}
3334
ratingScoreKey={resultCardRatingScoreKey}
3435
renderResultCardPriceDetails={renderResultCardPriceDetails}
36+
getResultCardImageUrl={getResultCardImageUrl}
3537
resultPosition={index + 1}
3638
/>
3739
)

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export interface ResultCardOptions {
5353
},
5454
index: number
5555
) => JSX.Element;
56+
getResultCardImageUrl?: (result: QuizResultDataPartial) => string;
5657
}
5758

5859
export namespace QuizResultsEventsProps {

0 commit comments

Comments
 (0)