@@ -86,7 +86,8 @@ describe('useViewModelInstance - RiveFile with instanceName parameter', () => {
8686 'PersonInstance'
8787 ) ;
8888 expect ( defaultViewModel . createDefaultInstance ) . not . toHaveBeenCalled ( ) ;
89- expect ( result . current ) . toBe ( personInstance ) ;
89+ expect ( result . current . instance ) . toBe ( personInstance ) ;
90+ expect ( result . current . error ) . toBeNull ( ) ;
9091 } ) ;
9192
9293 it ( 'should use defaultArtboardViewModel and createDefaultInstance when no instanceName provided' , ( ) => {
@@ -102,10 +103,11 @@ describe('useViewModelInstance - RiveFile with instanceName parameter', () => {
102103 ) ;
103104 expect ( defaultViewModel . createDefaultInstance ) . toHaveBeenCalled ( ) ;
104105 expect ( defaultViewModel . createInstanceByName ) . not . toHaveBeenCalled ( ) ;
105- expect ( result . current ) . toBe ( defaultInstance ) ;
106+ expect ( result . current . instance ) . toBe ( defaultInstance ) ;
107+ expect ( result . current . error ) . toBeNull ( ) ;
106108 } ) ;
107109
108- it ( 'should return null when instance name not found and required is false' , ( ) => {
110+ it ( 'should return error when instance name not found and required is false' , ( ) => {
109111 const defaultViewModel = createMockViewModel ( {
110112 namedInstances : { } ,
111113 } ) ;
@@ -116,7 +118,9 @@ describe('useViewModelInstance - RiveFile with instanceName parameter', () => {
116118 useViewModelInstance ( mockRiveFile , { instanceName : 'NonExistent' } )
117119 ) ;
118120
119- expect ( result . current ) . toBeNull ( ) ;
121+ expect ( result . current . instance ) . toBeNull ( ) ;
122+ expect ( result . current . error ) . toBeInstanceOf ( Error ) ;
123+ expect ( result . current . error ?. message ) . toContain ( 'NonExistent' ) ;
120124 } ) ;
121125
122126 it ( 'should throw when instance name not found and required is true' , ( ) => {
@@ -136,7 +140,7 @@ describe('useViewModelInstance - RiveFile with instanceName parameter', () => {
136140 ) . toThrow ( "ViewModel instance 'NonExistent' not found" ) ;
137141 } ) ;
138142
139- it ( 'should return null when artboardName not found and required is false' , ( ) => {
143+ it ( 'should return error when artboardName not found and required is false' , ( ) => {
140144 const mockRiveFile = createMockRiveFile ( {
141145 artboardViewModels : { } ,
142146 } ) ;
@@ -145,7 +149,9 @@ describe('useViewModelInstance - RiveFile with instanceName parameter', () => {
145149 useViewModelInstance ( mockRiveFile , { artboardName : 'MissingArtboard' } )
146150 ) ;
147151
148- expect ( result . current ) . toBeNull ( ) ;
152+ expect ( result . current . instance ) . toBeNull ( ) ;
153+ expect ( result . current . error ) . toBeInstanceOf ( Error ) ;
154+ expect ( result . current . error ?. message ) . toContain ( 'MissingArtboard' ) ;
149155 } ) ;
150156
151157 it ( 'should throw when artboardName not found and required is true' , ( ) => {
@@ -203,7 +209,8 @@ describe('useViewModelInstance - RiveFile with artboardName parameter', () => {
203209 name : 'MainArtboard' ,
204210 } ) ;
205211 expect ( mainArtboardViewModel . createDefaultInstance ) . toHaveBeenCalled ( ) ;
206- expect ( result . current ) . toBe ( mainInstance ) ;
212+ expect ( result . current . instance ) . toBe ( mainInstance ) ;
213+ expect ( result . current . error ) . toBeNull ( ) ;
207214 } ) ;
208215
209216 it ( 'should combine artboardName and instanceName to get specific instance from specific artboard' , ( ) => {
@@ -230,7 +237,8 @@ describe('useViewModelInstance - RiveFile with artboardName parameter', () => {
230237 expect ( mainArtboardViewModel . createInstanceByName ) . toHaveBeenCalledWith (
231238 'SpecificInstance'
232239 ) ;
233- expect ( result . current ) . toBe ( specificInstance ) ;
240+ expect ( result . current . instance ) . toBe ( specificInstance ) ;
241+ expect ( result . current . error ) . toBeNull ( ) ;
234242 } ) ;
235243} ) ;
236244
@@ -252,10 +260,11 @@ describe('useViewModelInstance - RiveFile with viewModelName parameter', () => {
252260 expect ( mockRiveFile . viewModelByName ) . toHaveBeenCalledWith ( 'Settings' ) ;
253261 expect ( mockRiveFile . defaultArtboardViewModel ) . not . toHaveBeenCalled ( ) ;
254262 expect ( settingsViewModel . createDefaultInstance ) . toHaveBeenCalled ( ) ;
255- expect ( result . current ) . toBe ( settingsInstance ) ;
263+ expect ( result . current . instance ) . toBe ( settingsInstance ) ;
264+ expect ( result . current . error ) . toBeNull ( ) ;
256265 } ) ;
257266
258- it ( 'should return null when viewModelName not found and required is false' , ( ) => {
267+ it ( 'should return error when viewModelName not found and required is false' , ( ) => {
259268 const mockRiveFile = createMockRiveFile ( {
260269 namedViewModels : { } ,
261270 } ) ;
@@ -264,7 +273,9 @@ describe('useViewModelInstance - RiveFile with viewModelName parameter', () => {
264273 useViewModelInstance ( mockRiveFile , { viewModelName : 'NonExistent' } )
265274 ) ;
266275
267- expect ( result . current ) . toBeNull ( ) ;
276+ expect ( result . current . instance ) . toBeNull ( ) ;
277+ expect ( result . current . error ) . toBeInstanceOf ( Error ) ;
278+ expect ( result . current . error ?. message ) . toContain ( 'NonExistent' ) ;
268279 } ) ;
269280
270281 it ( 'should throw when viewModelName not found and required is true' , ( ) => {
@@ -303,7 +314,8 @@ describe('useViewModelInstance - RiveFile with viewModelName parameter', () => {
303314 expect ( settingsViewModel . createInstanceByName ) . toHaveBeenCalledWith (
304315 'UserSettings'
305316 ) ;
306- expect ( result . current ) . toBe ( specificInstance ) ;
317+ expect ( result . current . instance ) . toBe ( specificInstance ) ;
318+ expect ( result . current . error ) . toBeNull ( ) ;
307319 } ) ;
308320} ) ;
309321
@@ -320,7 +332,8 @@ describe('useViewModelInstance - ViewModel source', () => {
320332
321333 expect ( mockViewModel . createInstanceByName ) . toHaveBeenCalledWith ( 'Gordon' ) ;
322334 expect ( mockViewModel . createDefaultInstance ) . not . toHaveBeenCalled ( ) ;
323- expect ( result . current ) . toBe ( namedInstance ) ;
335+ expect ( result . current . instance ) . toBe ( namedInstance ) ;
336+ expect ( result . current . error ) . toBeNull ( ) ;
324337 } ) ;
325338
326339 it ( 'should use createInstance when useNew is true' , ( ) => {
@@ -334,7 +347,8 @@ describe('useViewModelInstance - ViewModel source', () => {
334347
335348 expect ( mockViewModel . createInstance ) . toHaveBeenCalled ( ) ;
336349 expect ( mockViewModel . createDefaultInstance ) . not . toHaveBeenCalled ( ) ;
337- expect ( result . current ) . toBe ( newInstance ) ;
350+ expect ( result . current . instance ) . toBe ( newInstance ) ;
351+ expect ( result . current . error ) . toBeNull ( ) ;
338352 } ) ;
339353
340354 it ( 'should use createDefaultInstance when no params provided' , ( ) => {
@@ -344,6 +358,16 @@ describe('useViewModelInstance - ViewModel source', () => {
344358 const { result } = renderHook ( ( ) => useViewModelInstance ( mockViewModel ) ) ;
345359
346360 expect ( mockViewModel . createDefaultInstance ) . toHaveBeenCalled ( ) ;
347- expect ( result . current ) . toBe ( defaultInstance ) ;
361+ expect ( result . current . instance ) . toBe ( defaultInstance ) ;
362+ expect ( result . current . error ) . toBeNull ( ) ;
363+ } ) ;
364+ } ) ;
365+
366+ describe ( 'useViewModelInstance - null source' , ( ) => {
367+ it ( 'should return undefined instance when source is null' , ( ) => {
368+ const { result } = renderHook ( ( ) => useViewModelInstance ( null ) ) ;
369+
370+ expect ( result . current . instance ) . toBeUndefined ( ) ;
371+ expect ( result . current . error ) . toBeNull ( ) ;
348372 } ) ;
349373} ) ;
0 commit comments