Skip to content

Commit

Permalink
fix: catch RiveFile initialization error in hook
Browse files Browse the repository at this point in the history
  • Loading branch information
damzobridge committed Jun 20, 2024
1 parent b25c775 commit 84fdfac
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
29 changes: 17 additions & 12 deletions src/hooks/useRiveFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,25 @@ function useRiveFile(params: UseRiveFileParameters): RiveFileState {
let file: RiveFile | null = null;

const loadRiveFile = async () => {
setStatus('loading');
file = new RiveFile(params);
file.init();
file.on(EventType.Load, () => {
// We request an instance to add +1 to the referencesCount so it doesn't get destroyed
// while this hook is active
file?.getInstance();
try {
setStatus('loading');
file = new RiveFile(params);
file.init();
file.on(EventType.Load, () => {
// We request an instance to add +1 to the referencesCount so it doesn't get destroyed
// while this hook is active
file?.getInstance();
setRiveFile(file);
setStatus('success');
});
file.on(EventType.LoadError, () => {
setStatus('failed');
});
setRiveFile(file);
setStatus('success');
});
file.on(EventType.LoadError, () => {
} catch (error) {
console.error(error);
setStatus('failed');
});
setRiveFile(file);
}
};

loadRiveFile();
Expand Down
33 changes: 30 additions & 3 deletions test/useRiveFile.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ describe('useRiveFile', () => {
mocked(RiveFile).mockClear();
});



it('initializes RiveFile with provided parameters', async () => {
const params = {
src: 'file-src',
Expand All @@ -38,7 +40,7 @@ describe('useRiveFile', () => {
it('cleans up RiveFile on unmount', async () => {
const params = {
src: 'file-src',
enableRiveAssetCDN: false
enableRiveAssetCDN: false
};

const { result, unmount } = renderHook(() => useRiveFile(params));
Expand All @@ -54,7 +56,7 @@ describe('useRiveFile', () => {
it('does not reinitialize RiveFile if src has not changed', async () => {
const params = {
src: 'file-src',
enableRiveAssetCDN: false
enableRiveAssetCDN: false
};

const { rerender } = renderHook(() => useRiveFile(params));
Expand All @@ -64,7 +66,7 @@ describe('useRiveFile', () => {
expect(RiveFile).toHaveBeenCalledTimes(1);
});

it('does not reinitialize RiveFile if buffer has not changed', async () => {
it('does not reinitialize RiveFile if buffer has not changed', async () => {
const params = {
buffer: new ArrayBuffer(10),
enableRiveAssetCDN: false
Expand Down Expand Up @@ -112,4 +114,29 @@ describe('useRiveFile', () => {

expect(RiveFile).toHaveBeenCalledTimes(2);
});

it('handles RiveFile initialization failure gracefully', async () => {

const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => { });
const error = new Error("Initialization failed");

mocked(RiveFile).mockImplementation(() => {
throw error;
});

const params = {
src: 'file-src',
enableRiveAssetCDN: false,
};

const { result, rerender } = renderHook(() => useRiveFile(params));

rerender();

expect(result.current.status).toBe('failed');
expect(result.current.riveFile).toBeNull();
expect(consoleSpy).toHaveBeenCalledWith(error);

consoleSpy.mockRestore();
});
});

0 comments on commit 84fdfac

Please sign in to comment.