@@ -3,9 +3,10 @@ import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
33import { defineComponent , h } from 'vue'
44
55import type { ChainNamespace } from '@reown/appkit-common'
6- import { ProviderController } from '@reown/appkit-controllers'
6+ import { ChainController , ProviderController } from '@reown/appkit-controllers'
77
88import { type ConnectorType , createAppKit , useAppKitProvider } from '../../exports/vue-core.js'
9+ import { useAppKitNetwork } from '../../exports/vue.js'
910import { mainnet } from '../mocks/Networks.js'
1011
1112const TestComponent = defineComponent ( {
@@ -95,3 +96,92 @@ describe('useAppKitProvider', () => {
9596 expect ( mockUnsubscribe ) . toHaveBeenCalled ( )
9697 } )
9798} )
99+
100+ describe ( 'useAppKitNetwork' , ( ) => {
101+ const NetworkTestComponent = defineComponent ( {
102+ setup ( ) {
103+ const state = useAppKitNetwork ( )
104+
105+ return { state }
106+ } ,
107+ render ( ) {
108+ return h ( 'div' , { } , [
109+ h (
110+ 'div' ,
111+ { 'data-testid' : 'approvedCaipNetworkIds' } ,
112+ JSON . stringify ( this . state . approvedCaipNetworkIds )
113+ ) ,
114+ h (
115+ 'div' ,
116+ { 'data-testid' : 'supportsAllNetworks' } ,
117+ JSON . stringify ( this . state . supportsAllNetworks )
118+ )
119+ ] )
120+ }
121+ } )
122+
123+ let mockUnsubscribeKey : ReturnType < typeof vi . fn >
124+ let mockUnsubscribeChainProp : ReturnType < typeof vi . fn >
125+
126+ beforeEach ( ( ) => {
127+ vi . clearAllMocks ( )
128+
129+ mockUnsubscribeKey = vi . fn ( )
130+ mockUnsubscribeChainProp = vi . fn ( )
131+ vi . spyOn ( ChainController , 'subscribeKey' ) . mockReturnValue ( mockUnsubscribeKey )
132+ vi . spyOn ( ChainController , 'subscribeChainProp' ) . mockReturnValue ( mockUnsubscribeChainProp )
133+ } )
134+
135+ it ( 'should return defaults when no chain is active' , ( ) => {
136+ ChainController . state . activeChain = undefined
137+
138+ const wrapper = mount ( NetworkTestComponent )
139+
140+ expect ( wrapper . get ( '[data-testid="supportsAllNetworks"]' ) . text ( ) ) . toBe ( 'true' )
141+ } )
142+
143+ it ( 'should return approved networks from chain networkState' , ( ) => {
144+ ChainController . state . activeChain = 'eip155'
145+ ChainController . state . chains = new Map ( [
146+ [
147+ 'eip155' ,
148+ {
149+ networkState : {
150+ approvedCaipNetworkIds : [ 'eip155:1' , 'eip155:137' ] ,
151+ supportsAllNetworks : false
152+ }
153+ }
154+ ]
155+ ] )
156+
157+ const wrapper = mount ( NetworkTestComponent )
158+
159+ expect ( wrapper . get ( '[data-testid="approvedCaipNetworkIds"]' ) . text ( ) ) . toBe (
160+ '["eip155:1","eip155:137"]'
161+ )
162+ expect ( wrapper . get ( '[data-testid="supportsAllNetworks"]' ) . text ( ) ) . toBe ( 'false' )
163+ } )
164+
165+ it ( 'should subscribe to activeCaipNetwork and networkState' , ( ) => {
166+ ChainController . state . activeChain = 'eip155'
167+ ChainController . state . chains = new Map ( [
168+ [ 'eip155' , { networkState : { supportsAllNetworks : true } } ]
169+ ] )
170+
171+ const wrapper = mount ( NetworkTestComponent )
172+
173+ expect ( ChainController . subscribeKey ) . toHaveBeenCalledWith (
174+ 'activeCaipNetwork' ,
175+ expect . any ( Function )
176+ )
177+ expect ( ChainController . subscribeChainProp ) . toHaveBeenCalledWith (
178+ 'networkState' ,
179+ expect . any ( Function )
180+ )
181+
182+ wrapper . unmount ( )
183+
184+ expect ( mockUnsubscribeKey ) . toHaveBeenCalled ( )
185+ expect ( mockUnsubscribeChainProp ) . toHaveBeenCalled ( )
186+ } )
187+ } )
0 commit comments