Skip to content

Commit

Permalink
fix: adding unit test to handle fallback image
Browse files Browse the repository at this point in the history
  • Loading branch information
nhussein11 committed Jul 31, 2023
1 parent 0d01700 commit 8bcd71e
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/lib/components/image/image.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, it, expect } from 'vitest'
import { render, screen } from '@testing-library/react'
import { ImageIPFS } from './index'
import { DEFAULT_FALLBACK_IMAGE } from '../../const/images'

const MOCK_HASH = 'QmPbxeGcXhYQQNgsC6a36dDyYUcHgMLnGKnF8pVFmGsvqi'

Expand Down Expand Up @@ -78,4 +79,24 @@ describe('IPFSImage', () => {
expect(image).toBeDefined()
expect(image.getAttribute('class')).toBe('custom-class')
})

it('should render the IPFSImage component with default fallback image due to invalid hash', () => {
render(<ImageIPFS hash="invalid-hash" />)
const image = screen.getByRole('img')
setTimeout(() => {
expect(image).toBeDefined()
expect(image.getAttribute('src')).toBe(DEFAULT_FALLBACK_IMAGE)
}, 1000)
})

it('should render the IPFSImage component with custom fallback image due to invalid hash', () => {
const CUSTOM_DEFAULT_FALLBACK_IMAGE =
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQMAAADCCAMAAAB6zFdcAAAAclBMVEXy8vJmZmb19fV1dXX5+flaWlrx8fHc3NxnZ2eDg4NXV1fOzs76+vpiYmLq6upfX1/l5eVPT0+0tLSamppsbGxHR0ekpKTJycl4eHjU1NSUlJTDw8Pn5+eNjY2qqqp3d3exsbFAQECOjo5JSUk5OTmXl5daBGJNAAAH20lEQVR4nO2d22KqOhBAYRhAkXCTCoiiu57+/y+eJKhV7kotwc566LbuUmV1JgmZEDWNIAiCIAiCIAiCIAiCIAiCIAiCIAiCIAiCIAiCIAiCIAiCIAj1AdSs30dDmPrEv4EoXUxBGoEqFrzU1KfBTL2pT74EDaYzcwr46xo49ekLIDd1f2tPwdbXzVyFbMA9Y3sXpsDdM32vQiDAQXeSaf4Ys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Km4Hc39TO/GSFUH/CKLw7gDN01dDTzGGwnPPLq8sTy94sMLXgzY/4LyX8f0xNj3KwiKylhZtvX4FQsHRx724ocyw/Az0TR8BXa8EA1lxMfKmRo19kcJlmXc4/pTjP6y1HGM5Hoi3pKPINeRHASInh+Stbg00EP/6KU8WjBZhGYUifubecsoNlCY7ETGcDnf8roXwLJu+zc5vVIu+cfrVwBP3PonTtzlD91C3uPND9XmGAUjwU8xNMZl74bEbwwcwwSgOM2wQ/w5cOOcTi/46I5ZgV5mt2yn84eY4xUCQRAEQRAEQRAEQRAEQRAEQfwJ/gcc+4W9BEHqMAAAAABJRU5ErkJggg=='
render(<ImageIPFS hash="invalid-hash" fallbackImage={CUSTOM_DEFAULT_FALLBACK_IMAGE} />)
const image = screen.getByRole('img')
setTimeout(() => {
expect(image).toBeDefined()
expect(image.getAttribute('src')).toBe(CUSTOM_DEFAULT_FALLBACK_IMAGE)
}, 1000)
})
})

0 comments on commit 8bcd71e

Please sign in to comment.