Skip to content

Commit 2403712

Browse files
committed
Add useImageDimensions tests
1 parent 5c8506a commit 2403712

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

src/useImageDimensions.test.ts

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import {useImageDimensions} from './useImageDimensions'
2+
import {act, renderHook} from '@testing-library/react-hooks'
3+
import {Image} from 'react-native'
4+
5+
jest.mock('react-native', () => ({
6+
Image: {
7+
resolveAssetSource: jest.fn().mockReturnValue({width: 0, height: 0}),
8+
getSize: jest.fn(),
9+
},
10+
}))
11+
12+
describe('useImageDimensions', () => {
13+
describe('external images', () => {
14+
const mockSource = {uri: 'test'}
15+
const getSizeMock = Image.getSize as jest.Mock
16+
17+
it('should invoke getSize with with passed uri', () => {
18+
renderHook(() => useImageDimensions(mockSource))
19+
20+
expect(getSizeMock).toBeCalledWith(
21+
mockSource.uri,
22+
expect.any(Function),
23+
expect.any(Function),
24+
)
25+
})
26+
27+
it('should return error when cannot get image size', () => {
28+
const error = new Error('Ops...')
29+
30+
getSizeMock.mockImplementationOnce((_, onSuccess, onError) => {
31+
onError(error)
32+
})
33+
34+
const {result} = renderHook(() => useImageDimensions(mockSource))
35+
36+
expect(result.current).toEqual({
37+
loading: false,
38+
error,
39+
})
40+
})
41+
42+
it('should return dimensions when successfully get image size', () => {
43+
const width = 111
44+
const height = 222
45+
let emitOnSuccess = (() => {}) as (width: number, height: number) => void
46+
47+
getSizeMock.mockImplementationOnce((_, onSuccess) => {
48+
emitOnSuccess = onSuccess
49+
})
50+
51+
const {result} = renderHook(() => useImageDimensions(mockSource))
52+
53+
expect(result.current).toEqual({loading: true})
54+
55+
act(() => {
56+
emitOnSuccess(width, height)
57+
})
58+
59+
expect(result.current).toEqual({
60+
loading: false,
61+
dimensions: {width, height, aspectRatio: 0.5},
62+
})
63+
})
64+
65+
it('should update image size when source change', () => {
66+
let emitOnSuccess = (() => {}) as (width: number, height: number) => void
67+
68+
getSizeMock.mockImplementation((_, onSuccess) => {
69+
emitOnSuccess = onSuccess
70+
})
71+
72+
const {result, rerender} = renderHook(
73+
(props) => useImageDimensions(props.source),
74+
{initialProps: {source: mockSource}},
75+
)
76+
77+
expect(result.current).toEqual({loading: true})
78+
79+
act(() => emitOnSuccess(1, 2))
80+
81+
expect(result.current).toEqual({
82+
loading: false,
83+
dimensions: {width: 1, height: 2, aspectRatio: 0.5},
84+
})
85+
86+
rerender({source: {uri: 'new-uri'}})
87+
88+
expect(result.current).toEqual({loading: true})
89+
90+
act(() => emitOnSuccess(3, 4))
91+
92+
expect(result.current).toEqual({
93+
loading: false,
94+
dimensions: {width: 3, height: 4, aspectRatio: 0.75},
95+
})
96+
})
97+
})
98+
99+
describe('bundled up images', () => {
100+
const mockImage = 0 // typeof require('img.png') === 'number'
101+
const getResolveAssetSource = Image.resolveAssetSource as jest.Mock
102+
103+
it('should invoke resolveAssetSource with passed image', () => {
104+
renderHook(() => useImageDimensions(mockImage))
105+
106+
expect(getResolveAssetSource).toBeCalledWith(mockImage)
107+
})
108+
109+
it('should return image dimensions', () => {
110+
const width = 111
111+
const height = 222
112+
113+
getResolveAssetSource.mockReturnValueOnce({width, height})
114+
115+
const {result} = renderHook(() => useImageDimensions(mockImage))
116+
117+
expect(result.current).toEqual({
118+
loading: false,
119+
dimensions: {
120+
width,
121+
height,
122+
aspectRatio: 0.5,
123+
},
124+
})
125+
})
126+
127+
it('should return an error when unexpected error happened', () => {
128+
const error = new Error('Cannot find image')
129+
130+
getResolveAssetSource.mockImplementationOnce(() => {
131+
throw error
132+
})
133+
134+
const {result} = renderHook(() => useImageDimensions(mockImage))
135+
136+
expect(result.current).toEqual({
137+
loading: false,
138+
error,
139+
})
140+
})
141+
})
142+
143+
it('should return an error when pass non image', () => {
144+
const nonImage = 'Ops )))'
145+
const {result} = renderHook(() =>
146+
useImageDimensions(
147+
// @ts-expect-error - non image
148+
nonImage,
149+
),
150+
)
151+
152+
expect(result.current).toEqual({
153+
loading: false,
154+
error: expect.any(Error),
155+
})
156+
})
157+
})

0 commit comments

Comments
 (0)