Skip to content

Commit b3e812e

Browse files
authored
Merge pull request Expensify#82715 from abzokhattab/add-imagesvg-cache-policy-tests
[No QA] Add unit tests for ImageSVG cache policy behavior
2 parents 2044ede + e01b887 commit b3e812e

1 file changed

Lines changed: 118 additions & 0 deletions

File tree

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
jest.fn((source: unknown) => {
25+
if (typeof source === 'number') {
26+
return String(source);
27+
}
28+
if (typeof source === 'object' && source !== null && 'uri' in source) {
29+
return (source as {uri: string}).uri;
30+
}
31+
return undefined;
32+
}),
33+
);
34+
35+
const MOCK_STATIC_SOURCE = 42;
36+
37+
function getFirstCallProps(): Record<string, unknown> {
38+
const firstCall = mockImageComponent.mock.calls.at(0) as unknown[] | undefined;
39+
return firstCall?.at(0) as Record<string, unknown>;
40+
}
41+
42+
describe('ImageSVG cache policy', () => {
43+
beforeEach(() => {
44+
jest.clearAllMocks();
45+
});
46+
47+
describe('iOS implementation', () => {
48+
it('should use memory-disk cache policy for static image sources', () => {
49+
render(<ImageSVGiOS src={MOCK_STATIC_SOURCE} />);
50+
51+
expect(mockImageComponent).toHaveBeenCalled();
52+
const props = getFirstCallProps();
53+
expect(props.cachePolicy).toBe('memory-disk');
54+
});
55+
56+
it('should set recyclingKey for static image sources', () => {
57+
render(<ImageSVGiOS src={MOCK_STATIC_SOURCE} />);
58+
59+
const props = getFirstCallProps();
60+
expect(props.recyclingKey).toBe(String(MOCK_STATIC_SOURCE));
61+
});
62+
63+
it('should render React component sources directly without expo-image Image', () => {
64+
const MockSvgComponent = jest.fn(() => null);
65+
render(<ImageSVGiOS src={MockSvgComponent} />);
66+
67+
expect(mockImageComponent).not.toHaveBeenCalled();
68+
expect(MockSvgComponent).toHaveBeenCalled();
69+
});
70+
71+
it('should return null when src is undefined', () => {
72+
const {toJSON} = render(<ImageSVGiOS src={undefined} />);
73+
74+
expect(toJSON()).toBeNull();
75+
expect(mockImageComponent).not.toHaveBeenCalled();
76+
});
77+
});
78+
79+
describe('Android implementation', () => {
80+
it('should use memory cache policy for static image sources', () => {
81+
render(<ImageSVGAndroid src={MOCK_STATIC_SOURCE} />);
82+
83+
expect(mockImageComponent).toHaveBeenCalled();
84+
const props = getFirstCallProps();
85+
expect(props.cachePolicy).toBe('memory');
86+
});
87+
88+
it('should set recyclingKey for static image sources', () => {
89+
render(<ImageSVGAndroid src={MOCK_STATIC_SOURCE} />);
90+
91+
const props = getFirstCallProps();
92+
expect(props.recyclingKey).toBe(String(MOCK_STATIC_SOURCE));
93+
});
94+
95+
it('should clear memory cache on unmount to prevent memory leaks', () => {
96+
const {unmount} = render(<ImageSVGAndroid src={MOCK_STATIC_SOURCE} />);
97+
98+
expect(mockClearMemoryCache).not.toHaveBeenCalled();
99+
unmount();
100+
expect(mockClearMemoryCache).toHaveBeenCalled();
101+
});
102+
103+
it('should render React component sources directly without expo-image Image', () => {
104+
const MockSvgComponent = jest.fn(() => null);
105+
render(<ImageSVGAndroid src={MockSvgComponent} />);
106+
107+
expect(mockImageComponent).not.toHaveBeenCalled();
108+
expect(MockSvgComponent).toHaveBeenCalled();
109+
});
110+
111+
it('should return null when src is undefined', () => {
112+
const {toJSON} = render(<ImageSVGAndroid src={undefined} />);
113+
114+
expect(toJSON()).toBeNull();
115+
expect(mockImageComponent).not.toHaveBeenCalled();
116+
});
117+
});
118+
});

0 commit comments

Comments
 (0)