Skip to content

Commit dc88ec9

Browse files
committed
Add font config and asset loading tests (#274)
Adds harness tests for two previously untested areas: **Font config** (6 tests): loadFont by system name, loadFont by URL, systemFallback, setFallbackFonts with default/weight-specific fonts, clearFallbackFonts, error on invalid font name. **Asset loading** (4 tests): referencedAssets with font type, image URL type, multiple asset types, and undefined assets. Covers HybridRiveFontConfig.swift (was 0%), AssetLoader.swift (was 1%), DataSourceResolver.swift (was 0%), HTTPDataLoader.swift (was 43%).
1 parent eb9497c commit dc88ec9

2 files changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { describe, it, expect } from 'react-native-harness';
2+
import { RiveFileFactory } from '@rive-app/react-native';
3+
4+
const OUT_OF_BAND = require('../assets/rive/out_of_band.riv');
5+
6+
function isExperimental() {
7+
return RiveFileFactory.getBackend() === 'experimental';
8+
}
9+
10+
describe('Asset loading with referencedAssets', () => {
11+
// referencedAssets with raw ResolvedReferencedAsset objects only works
12+
// on the experimental backend; legacy uses a different resolution path.
13+
it('loads file with font asset (type: font)', async () => {
14+
if (!isExperimental()) return;
15+
const file = await RiveFileFactory.fromSource(OUT_OF_BAND, {
16+
'Inter-594377': {
17+
sourceAssetId: 'Inter-594377.ttf',
18+
type: 'font',
19+
},
20+
});
21+
expect(file).toBeDefined();
22+
expect(file.artboardNames.length).toBeGreaterThan(0);
23+
});
24+
25+
it('loads file with image asset via URL (type: image)', async () => {
26+
if (!isExperimental()) return;
27+
const file = await RiveFileFactory.fromSource(OUT_OF_BAND, {
28+
'referenced-image-2929282': {
29+
sourceUrl: 'https://picsum.photos/id/237/200/200',
30+
type: 'image',
31+
},
32+
});
33+
expect(file).toBeDefined();
34+
expect(file.artboardNames.length).toBeGreaterThan(0);
35+
});
36+
37+
it('loads file with multiple asset types', async () => {
38+
if (!isExperimental()) return;
39+
const file = await RiveFileFactory.fromSource(OUT_OF_BAND, {
40+
'Inter-594377': {
41+
sourceAssetId: 'Inter-594377.ttf',
42+
type: 'font',
43+
},
44+
'referenced-image-2929282': {
45+
sourceUrl: 'https://picsum.photos/id/237/200/200',
46+
type: 'image',
47+
},
48+
});
49+
expect(file).toBeDefined();
50+
});
51+
52+
it('loads file without referencedAssets (undefined)', async () => {
53+
const file = await RiveFileFactory.fromSource(OUT_OF_BAND, undefined);
54+
expect(file).toBeDefined();
55+
expect(file.artboardNames.length).toBeGreaterThan(0);
56+
});
57+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { describe, it, expect } from 'react-native-harness';
2+
import { Platform } from 'react-native';
3+
import { RiveFonts } from '@rive-app/react-native';
4+
5+
const SYSTEM_FONT = Platform.OS === 'ios' ? 'Helvetica' : 'sans-serif';
6+
7+
describe('RiveFonts', () => {
8+
it('systemFallback() returns a font object', () => {
9+
const font = RiveFonts.systemFallback();
10+
expect(font).toBeDefined();
11+
});
12+
13+
it('loadFont with system font name', async () => {
14+
const font = await RiveFonts.loadFont({ name: SYSTEM_FONT });
15+
expect(font).toBeDefined();
16+
});
17+
18+
it('loadFont with URL', async () => {
19+
const font = await RiveFonts.loadFont({
20+
uri: 'https://raw.githubusercontent.com/google/fonts/main/ofl/kanit/Kanit-Regular.ttf',
21+
});
22+
expect(font).toBeDefined();
23+
});
24+
25+
it('setFallbackFonts + clearFallbackFonts round-trip', async () => {
26+
const systemFont = RiveFonts.systemFallback();
27+
const urlFont = await RiveFonts.loadFont({
28+
uri: 'https://raw.githubusercontent.com/google/fonts/main/ofl/kanit/Kanit-Regular.ttf',
29+
});
30+
31+
await RiveFonts.setFallbackFonts({
32+
default: [urlFont, systemFont],
33+
});
34+
35+
await RiveFonts.clearFallbackFonts();
36+
});
37+
38+
it('setFallbackFonts with weight-specific fonts', async () => {
39+
const regular = await RiveFonts.loadFont({
40+
uri: 'https://raw.githubusercontent.com/google/fonts/main/ofl/kanit/Kanit-Regular.ttf',
41+
});
42+
const bold = await RiveFonts.loadFont({
43+
uri: 'https://raw.githubusercontent.com/google/fonts/main/ofl/kanit/Kanit-Bold.ttf',
44+
});
45+
const systemFont = RiveFonts.systemFallback();
46+
47+
await RiveFonts.setFallbackFonts({
48+
default: [regular, systemFont],
49+
700: [bold, systemFont],
50+
});
51+
52+
await RiveFonts.clearFallbackFonts();
53+
});
54+
55+
it('loadFont with invalid name throws', async () => {
56+
await expect(
57+
RiveFonts.loadFont({ name: 'NonExistentFont_XYZ_12345' })
58+
).rejects.toBeDefined();
59+
});
60+
});

0 commit comments

Comments
 (0)