Skip to content

Commit be822cf

Browse files
committed
test: Add unit tests for ImageSVG cache policy behavior (iOS/Android)
1 parent 96af6f9 commit be822cf

1 file changed

Lines changed: 114 additions & 0 deletions

File tree

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import {render} from '@testing-library/react-native';
2+
import React from 'react';
3+
import ImageSVGAndroid from '../../src/components/ImageSVG/index.android';
4+
import ImageSVGiOS from '../../src/components/ImageSVG/index.ios';
5+
6+
type MockImageType = jest.Mock & {clearMemoryCache: jest.Mock};
7+
8+
const mockClearMemoryCache = jest.fn(() => Promise.resolve(true));
9+
10+
const mockImageComponent: MockImageType = Object.assign(
11+
jest.fn(() => null),
12+
{
13+
clearMemoryCache: mockClearMemoryCache,
14+
},
15+
) as MockImageType;
16+
17+
jest.mock('expo-image', () => ({
18+
get Image() {
19+
return mockImageComponent;
20+
},
21+
}));
22+
23+
jest.mock('@libs/getImageRecyclingKey', () => ({
24+
__esModule: true,
25+
default: jest.fn((source: unknown) => {
26+
if (typeof source === 'number') {
27+
return String(source);
28+
}
29+
if (typeof source === 'object' && source !== null && 'uri' in source) {
30+
return (source as {uri: string}).uri;
31+
}
32+
return undefined;
33+
}),
34+
}));
35+
36+
const MOCK_STATIC_SOURCE = 42;
37+
38+
describe('ImageSVG cache policy', () => {
39+
beforeEach(() => {
40+
jest.clearAllMocks();
41+
});
42+
43+
describe('iOS implementation', () => {
44+
it('should use memory-disk cache policy for static image sources', () => {
45+
render(<ImageSVGiOS src={MOCK_STATIC_SOURCE} />);
46+
47+
expect(mockImageComponent).toHaveBeenCalled();
48+
const props = mockImageComponent.mock.calls[0][0] as Record<string, unknown>;
49+
expect(props.cachePolicy).toBe('memory-disk');
50+
});
51+
52+
it('should set recyclingKey for static image sources', () => {
53+
render(<ImageSVGiOS src={MOCK_STATIC_SOURCE} />);
54+
55+
const props = mockImageComponent.mock.calls[0][0] as Record<string, unknown>;
56+
expect(props.recyclingKey).toBe(String(MOCK_STATIC_SOURCE));
57+
});
58+
59+
it('should render React component sources directly without expo-image Image', () => {
60+
const MockSvgComponent = jest.fn(() => null);
61+
render(<ImageSVGiOS src={MockSvgComponent} />);
62+
63+
expect(mockImageComponent).not.toHaveBeenCalled();
64+
expect(MockSvgComponent).toHaveBeenCalled();
65+
});
66+
67+
it('should return null when src is undefined', () => {
68+
const {toJSON} = render(<ImageSVGiOS src={undefined} />);
69+
70+
expect(toJSON()).toBeNull();
71+
expect(mockImageComponent).not.toHaveBeenCalled();
72+
});
73+
});
74+
75+
describe('Android implementation', () => {
76+
it('should use memory cache policy for static image sources', () => {
77+
render(<ImageSVGAndroid src={MOCK_STATIC_SOURCE} />);
78+
79+
expect(mockImageComponent).toHaveBeenCalled();
80+
const props = mockImageComponent.mock.calls[0][0] as Record<string, unknown>;
81+
expect(props.cachePolicy).toBe('memory');
82+
});
83+
84+
it('should set recyclingKey for static image sources', () => {
85+
render(<ImageSVGAndroid src={MOCK_STATIC_SOURCE} />);
86+
87+
const props = mockImageComponent.mock.calls[0][0] as Record<string, unknown>;
88+
expect(props.recyclingKey).toBe(String(MOCK_STATIC_SOURCE));
89+
});
90+
91+
it('should clear memory cache on unmount to prevent memory leaks', () => {
92+
const {unmount} = render(<ImageSVGAndroid src={MOCK_STATIC_SOURCE} />);
93+
94+
expect(mockClearMemoryCache).not.toHaveBeenCalled();
95+
unmount();
96+
expect(mockClearMemoryCache).toHaveBeenCalled();
97+
});
98+
99+
it('should render React component sources directly without expo-image Image', () => {
100+
const MockSvgComponent = jest.fn(() => null);
101+
render(<ImageSVGAndroid src={MockSvgComponent} />);
102+
103+
expect(mockImageComponent).not.toHaveBeenCalled();
104+
expect(MockSvgComponent).toHaveBeenCalled();
105+
});
106+
107+
it('should return null when src is undefined', () => {
108+
const {toJSON} = render(<ImageSVGAndroid src={undefined} />);
109+
110+
expect(toJSON()).toBeNull();
111+
expect(mockImageComponent).not.toHaveBeenCalled();
112+
});
113+
});
114+
});

0 commit comments

Comments
 (0)