-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathFailedImageProvider.test.jsx
More file actions
56 lines (43 loc) · 2.16 KB
/
FailedImageProvider.test.jsx
File metadata and controls
56 lines (43 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { act } from '@tests/utils/test-utils';
import { renderHook } from '@testing-library/react';
import { useContext } from 'react';
import FailedImageProvider from '../../../src/contexts/FailedImageProvider';
import FailedImageContext from '../../../src/contexts/FailedImageContext';
import config from '../../../src/config/settings';
describe('FailedImageProvider', () => {
let originalFallback;
beforeEach(() => {
// Save original value
originalFallback = config.fallbackImage;
});
afterEach(() => {
// Restore original value
config.fallbackImage = originalFallback;
});
it('provides the fallbackImage from config if defined', () => {
config.fallbackImage = 'http://example.com/config-fallback.jpg';
const wrapper = ({ children }) => <FailedImageProvider>{children}</FailedImageProvider>;
const { result } = renderHook(() => useContext(FailedImageContext), { wrapper });
expect(result.current.fallbackImage).toBe('http://example.com/config-fallback.jpg');
});
it('provides the default SVG fallback if config.fallbackImage is not set', () => {
config.fallbackImage = undefined;
const wrapper = ({ children }) => <FailedImageProvider>{children}</FailedImageProvider>;
const { result } = renderHook(() => useContext(FailedImageContext), { wrapper });
expect(result.current.fallbackImage).toBeDefined();
// Default is an SVG data URI with warning icon
expect(result.current.fallbackImage).toContain('data:image/svg+xml');
expect(result.current.fallbackImage).toContain('%231967d2'); // Blue color
});
it('provides failedImages as empty Set initially and can add to it', () => {
const wrapper = ({ children }) => <FailedImageProvider>{children}</FailedImageProvider>;
const { result } = renderHook(() => useContext(FailedImageContext), { wrapper });
expect(result.current.failedImages).toBeInstanceOf(Set);
expect(result.current.failedImages.size).toBe(0);
act(() => {
result.current.notifyFailure('https://example.com/image1.jpg');
});
expect(result.current.failedImages.size).toBe(1);
expect(result.current.failedImages.has('https://example.com/image1.jpg')).toBe(true);
});
});