Skip to content

Commit

Permalink
refactor: move dup code to getImageSource
Browse files Browse the repository at this point in the history
  • Loading branch information
DiogoSoaress committed Dec 19, 2023
1 parent cff0b55 commit e4c03e7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/components/common/Masthead/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import layoutCss from '@/components/common/styles.module.css'
import css from './styles.module.css'
import type { BaseBlock } from '@/components/Home/types'
import ButtonsWrapper from '@/components/common/ButtonsWrapper'
import { getImageSource, type ImageObj } from '@/lib/getImageSource'

type FooterProps = {
text: string
Expand Down Expand Up @@ -42,18 +43,14 @@ export const Masthead = ({
footer,
backgroundImage,
}: BaseBlock & {
image: {
sm: string
md: string
alt: string
}
backgroundImage: { sm: string; md: string }
image: ImageObj
backgroundImage: ImageObj
footer: FooterProps
}): ReactElement => {
const isSmallScreen = useMediaQuery('(max-width:900px)')

const bgImage = backgroundImage ? (isSmallScreen ? backgroundImage.sm : backgroundImage.md) : undefined
const imageSrc = image ? (isSmallScreen ? image.sm : image.md) : undefined
const bgImage = getImageSource(isSmallScreen, backgroundImage)
const imageSrc = getImageSource(isSmallScreen, image)

return (
<Container className={layoutCss.containerShort} id="masthead">
Expand Down
38 changes: 38 additions & 0 deletions src/lib/__test__/getImageSource.test.ts
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()
})
})
15 changes: 15 additions & 0 deletions src/lib/getImageSource.ts
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
}

0 comments on commit e4c03e7

Please sign in to comment.