Greenlens/__tests__/utils/imageUri.test.ts

38 lines
1.6 KiB
TypeScript

import { getPlantImageSourceFallbackUri, tryResolveImageUri } from '../../utils/imageUri';
describe('imageUri utilities', () => {
const originalApiUrl = process.env.EXPO_PUBLIC_API_URL;
const originalBackendUrl = process.env.EXPO_PUBLIC_BACKEND_URL;
beforeEach(() => {
process.env.EXPO_PUBLIC_API_URL = 'http://localhost:3000/api';
delete process.env.EXPO_PUBLIC_BACKEND_URL;
});
afterEach(() => {
process.env.EXPO_PUBLIC_API_URL = originalApiUrl;
process.env.EXPO_PUBLIC_BACKEND_URL = originalBackendUrl;
});
it('resolves local plant asset paths against the API host', () => {
expect(tryResolveImageUri('/plants/monstera.webp')).toBe('http://localhost:3000/plants/monstera.webp');
expect(tryResolveImageUri('plants/aloe-vera-thumb.webp')).toBe('http://localhost:3000/plants/aloe-vera-thumb.webp');
});
it('rejects invalid local paths outside the plants directory', () => {
expect(tryResolveImageUri('/uploads/monstera.webp')).toBeNull();
expect(tryResolveImageUri('../plants/monstera.webp')).toBeNull();
});
it('resolves local plant asset paths against EXPO_PUBLIC_BACKEND_URL when API_URL is absent', () => {
delete process.env.EXPO_PUBLIC_API_URL;
process.env.EXPO_PUBLIC_BACKEND_URL = 'https://backend.example.com';
expect(tryResolveImageUri('/plants/rose.webp')).toBe('https://backend.example.com/plants/rose.webp');
});
it('falls back from a missing local asset to the manifest-backed source image', () => {
expect(getPlantImageSourceFallbackUri('/plants/rosa-x-hybrida--rose--7375780c.webp')).toMatch(/^https?:\/\//);
});
});