|
| 1 | +import { describe, it, expect, cleanup } from 'react-native-harness'; |
| 2 | +import { RiveFileFactory, RiveLog } from '@rive-app/react-native'; |
| 3 | + |
| 4 | +const BOUNCING_BALL = require('../assets/rive/bouncing_ball.riv'); |
| 5 | + |
| 6 | +type LogEntry = { level: string; tag: string; message: string }; |
| 7 | + |
| 8 | +describe('RiveLog', () => { |
| 9 | + it('captures deprecation warning from sync method', async () => { |
| 10 | + const logs: LogEntry[] = []; |
| 11 | + RiveLog.setHandler((level, tag, message) => { |
| 12 | + logs.push({ level, tag, message }); |
| 13 | + }); |
| 14 | + |
| 15 | + const file = await RiveFileFactory.fromSource(BOUNCING_BALL, undefined); |
| 16 | + |
| 17 | + file.defaultArtboardViewModel(); |
| 18 | + |
| 19 | + const deprecation = logs.find((l) => l.tag === 'Deprecation'); |
| 20 | + expect(deprecation).toBeDefined(); |
| 21 | + expect(deprecation!.level).toBe('warn'); |
| 22 | + expect(deprecation!.message).toContain('defaultArtboardViewModel'); |
| 23 | + expect(deprecation!.message).toContain('defaultArtboardViewModelAsync'); |
| 24 | + |
| 25 | + RiveLog.resetHandler(); |
| 26 | + cleanup(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('emits each deprecation only once', async () => { |
| 30 | + const logs: LogEntry[] = []; |
| 31 | + RiveLog.setHandler((level, tag, message) => { |
| 32 | + logs.push({ level, tag, message }); |
| 33 | + }); |
| 34 | + |
| 35 | + const file = await RiveFileFactory.fromSource(BOUNCING_BALL, undefined); |
| 36 | + |
| 37 | + // Use artboardNames — a different deprecated method so the once-per-session |
| 38 | + // dedup doesn't collide with the previous test. |
| 39 | + file.artboardNames; |
| 40 | + file.artboardNames; |
| 41 | + |
| 42 | + const deprecations = logs.filter( |
| 43 | + (l) => l.tag === 'Deprecation' && l.message.includes('artboardNames') |
| 44 | + ); |
| 45 | + expect(deprecations.length).toBe(1); |
| 46 | + |
| 47 | + RiveLog.resetHandler(); |
| 48 | + cleanup(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('suppresses all logs with a no-op handler', async () => { |
| 52 | + const logs: LogEntry[] = []; |
| 53 | + RiveLog.setHandler(() => {}); |
| 54 | + |
| 55 | + const file = await RiveFileFactory.fromSource(BOUNCING_BALL, undefined); |
| 56 | + |
| 57 | + file.artboardCount; |
| 58 | + |
| 59 | + expect(logs.length).toBe(0); |
| 60 | + |
| 61 | + RiveLog.resetHandler(); |
| 62 | + cleanup(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('resetHandler restores default logging without throwing', async () => { |
| 66 | + RiveLog.setHandler(() => {}); |
| 67 | + RiveLog.resetHandler(); |
| 68 | + |
| 69 | + const file = await RiveFileFactory.fromSource(BOUNCING_BALL, undefined); |
| 70 | + |
| 71 | + file.viewModelByName('nonexistent'); |
| 72 | + |
| 73 | + // If we got here without throwing, the default handler works. |
| 74 | + expect(true).toBe(true); |
| 75 | + |
| 76 | + cleanup(); |
| 77 | + }); |
| 78 | +}); |
0 commit comments