@@ -48,6 +48,11 @@ jest.mock("@app/hooks/use-app-config", () => ({
4848 } ) ,
4949} ) )
5050
51+ const mockListSelfCustodialAccounts = jest . fn ( )
52+ jest . mock ( "@app/self-custodial/storage/account-index" , ( ) => ( {
53+ listSelfCustodialAccounts : ( ) => mockListSelfCustodialAccounts ( ) ,
54+ } ) )
55+
5156let mockNonCustodialEnabled = false
5257let mockActiveAccountId : string | undefined
5358
@@ -56,6 +61,7 @@ describe("useAccountRegistry", () => {
5661 jest . clearAllMocks ( )
5762 mockNonCustodialEnabled = false
5863 mockActiveAccountId = undefined
64+ mockListSelfCustodialAccounts . mockResolvedValue ( [ ] )
5965 } )
6066
6167 it ( "returns custodial account when authenticated" , ( ) => {
@@ -78,15 +84,22 @@ describe("useAccountRegistry", () => {
7884 expect ( result . current . activeAccount ) . toBeUndefined ( )
7985 } )
8086
81- it ( "includes self-custodial account when flag enabled" , ( ) => {
87+ it ( "includes self-custodial accounts loaded from the index" , async ( ) => {
8288 mockUseIsAuthed . mockReturnValue ( true )
8389 mockNonCustodialEnabled = true
90+ mockListSelfCustodialAccounts . mockResolvedValue ( [
91+ { id : "sc-uuid-1" , lightningAddress : null } ,
92+ ] )
8493
8594 const { result } = renderHook ( ( ) => useAccountRegistry ( ) )
8695
96+ await act ( async ( ) => {
97+ await Promise . resolve ( )
98+ } )
99+
87100 expect ( result . current . accounts ) . toHaveLength ( 2 )
88101 expect ( result . current . accounts [ 1 ] . type ) . toBe ( AccountType . SelfCustodial )
89- expect ( result . current . accounts [ 1 ] . label ) . toBe ( "Spark " )
102+ expect ( result . current . accounts [ 1 ] . id ) . toBe ( "sc-uuid-1 " )
90103 expect ( result . current . accounts [ 1 ] . status ) . toBe ( AccountStatus . RequiresRestore )
91104 } )
92105
@@ -99,14 +112,21 @@ describe("useAccountRegistry", () => {
99112 expect ( result . current . activeAccount ?. selected ) . toBe ( true )
100113 } )
101114
102- it ( "selects account matching activeAccountId" , ( ) => {
115+ it ( "selects account matching activeAccountId" , async ( ) => {
103116 mockUseIsAuthed . mockReturnValue ( true )
104117 mockNonCustodialEnabled = true
105- mockActiveAccountId = "self-custodial-default"
118+ mockActiveAccountId = "sc-uuid-1"
119+ mockListSelfCustodialAccounts . mockResolvedValue ( [
120+ { id : "sc-uuid-1" , lightningAddress : null } ,
121+ ] )
106122
107123 const { result } = renderHook ( ( ) => useAccountRegistry ( ) )
108124
109- expect ( result . current . activeAccount ?. id ) . toBe ( "self-custodial-default" )
125+ await act ( async ( ) => {
126+ await Promise . resolve ( )
127+ } )
128+
129+ expect ( result . current . activeAccount ?. id ) . toBe ( "sc-uuid-1" )
110130 expect ( result . current . activeAccount ?. type ) . toBe ( AccountType . SelfCustodial )
111131 } )
112132
@@ -134,14 +154,21 @@ describe("useAccountRegistry", () => {
134154 expect ( mockSaveToken ) . toHaveBeenCalledWith ( "token" )
135155 } )
136156
137- it ( "setActiveAccountId does not call saveToken for self-custodial" , ( ) => {
157+ it ( "setActiveAccountId does not call saveToken for self-custodial" , async ( ) => {
138158 mockUseIsAuthed . mockReturnValue ( true )
139159 mockNonCustodialEnabled = true
160+ mockListSelfCustodialAccounts . mockResolvedValue ( [
161+ { id : "sc-uuid-1" , lightningAddress : null } ,
162+ ] )
140163
141164 const { result } = renderHook ( ( ) => useAccountRegistry ( ) )
142165
166+ await act ( async ( ) => {
167+ await Promise . resolve ( )
168+ } )
169+
143170 act ( ( ) => {
144- result . current . setActiveAccountId ( "self-custodial-default " )
171+ result . current . setActiveAccountId ( "sc-uuid-1 " )
145172 } )
146173
147174 expect ( mockSaveToken ) . not . toHaveBeenCalled ( )
@@ -162,9 +189,9 @@ describe("createCustodialDescriptor", () => {
162189
163190describe ( "createSelfCustodialDescriptor" , ( ) => {
164191 it ( "creates a self-custodial descriptor with correct defaults" , ( ) => {
165- const desc = createSelfCustodialDescriptor ( "Spark" )
192+ const desc = createSelfCustodialDescriptor ( "sc-id-1" , " Spark")
166193
167- expect ( desc . id ) . toBe ( DefaultAccountId . SelfCustodial )
194+ expect ( desc . id ) . toBe ( "sc-id-1" )
168195 expect ( desc . type ) . toBe ( AccountType . SelfCustodial )
169196 expect ( desc . label ) . toBe ( "Spark" )
170197 expect ( desc . selected ) . toBe ( false )
@@ -175,11 +202,11 @@ describe("createSelfCustodialDescriptor", () => {
175202describe ( "markSelected" , ( ) => {
176203 const accounts = [
177204 createCustodialDescriptor ( "Blink" ) ,
178- createSelfCustodialDescriptor ( "Spark" ) ,
205+ createSelfCustodialDescriptor ( "sc-id-1" , " Spark") ,
179206 ]
180207
181208 it ( "marks account matching activeId as selected" , ( ) => {
182- const result = markSelected ( accounts , DefaultAccountId . SelfCustodial )
209+ const result = markSelected ( accounts , "sc-id-1" )
183210
184211 expect ( result [ 0 ] . selected ) . toBe ( false )
185212 expect ( result [ 1 ] . selected ) . toBe ( true )
0 commit comments