-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move dup code to getImageSource
- Loading branch information
1 parent
cff0b55
commit e4c03e7
Showing
3 changed files
with
58 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { type ImageObj, getImageSource } from '@/lib/getImageSource' | ||
|
||
describe('getImageSource', () => { | ||
it('should return small image URL when image object exists and isSmall is true', () => { | ||
const imageObj: ImageObj = { | ||
sm: 'small-image.jpg', | ||
md: 'medium-image.jpg', | ||
alt: 'Small Image', | ||
} | ||
const isSmall = true | ||
|
||
const result = getImageSource(isSmall, imageObj) | ||
|
||
expect(result).toBe('small-image.jpg') | ||
}) | ||
|
||
it('should return medium image URL when image object exists and isSmall is false', () => { | ||
const imageObj: ImageObj = { | ||
sm: 'small-image.jpg', | ||
md: 'medium-image.jpg', | ||
alt: 'Medium Image', | ||
} | ||
const isSmall = false | ||
|
||
const result = getImageSource(isSmall, imageObj) | ||
|
||
expect(result).toBe('medium-image.jpg') | ||
}) | ||
|
||
it('should return undefined when image object does not exist', () => { | ||
const imageObj = undefined | ||
const isSmall = true | ||
|
||
const result = getImageSource(isSmall, imageObj) | ||
|
||
expect(result).toBeUndefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export type ImageObj = { | ||
sm: string | ||
md: string | ||
alt?: string | ||
} | ||
|
||
/** | ||
* Get the source URL for an image based on its size and screen condition. | ||
* @param {ImageObj} imageObj - The object containing URLs for different image sizes. | ||
* @param {boolean} isSmall - A flag indicating whether the small image shall be used. | ||
* @returns {string|undefined} - The source URL for the image, or undefined. | ||
*/ | ||
export function getImageSource(isSmall: boolean, imageObj?: ImageObj) { | ||
return imageObj ? (isSmall ? imageObj.sm : imageObj.md) : undefined | ||
} |