1
1
import { debug } from 'electron-log' ;
2
2
import { defaultStateBalances , defaultStateInfo , getNetwork } from 'utils/tests' ;
3
- import { clightningService } from './' ;
4
3
import * as clightningApi from './clightningApi' ;
4
+ import { CLightningService } from './clightningService' ;
5
5
import * as CLN from './types' ;
6
6
7
- jest . mock ( 'electron-log' ) ;
8
7
jest . mock ( './clightningApi' ) ;
9
8
10
9
const clightningApiMock = clightningApi as jest . Mocked < typeof clightningApi > ;
11
10
12
11
describe ( 'CLightningService' , ( ) => {
13
12
const node = getNetwork ( ) . nodes . lightning [ 1 ] ;
13
+ let clightningService : CLightningService ;
14
+
15
+ beforeEach ( ( ) => {
16
+ clightningService = new CLightningService ( ) ;
17
+ } ) ;
14
18
15
19
it ( 'should get node info' , async ( ) => {
16
20
const infoResponse : Partial < CLN . GetInfoResponse > = {
@@ -253,11 +257,6 @@ describe('CLightningService', () => {
253
257
} ) ;
254
258
255
259
describe ( 'subscribeChannelEvents' , ( ) => {
256
- afterEach ( ( ) => {
257
- // Clean up any resources or mock implementations after each test
258
- jest . restoreAllMocks ( ) ;
259
- } ) ;
260
-
261
260
it ( 'should create a channel cache, set interval, and call checkChannels' , async ( ) => {
262
261
jest . useFakeTimers ( ) ;
263
262
const mockCallback = jest . fn ( ) ;
@@ -279,6 +278,7 @@ describe('CLightningService', () => {
279
278
) ;
280
279
281
280
expect ( clightningService . channelCaches ) . toBeDefined ( ) ;
281
+ jest . useRealTimers ( ) ;
282
282
} ) ;
283
283
284
284
it ( 'should do nothing if node has already subscribed to channel event' , async ( ) => {
@@ -288,9 +288,18 @@ describe('CLightningService', () => {
288
288
jest . spyOn ( clightningService , 'checkChannels' ) . mockReturnValue ( Promise . resolve ( ) ) ;
289
289
290
290
await clightningService . subscribeChannelEvents ( node , mockCallback ) ;
291
+ jest . advanceTimersByTime ( 30 * 1000 ) ;
292
+
293
+ expect ( setInterval ) . toHaveBeenCalledTimes ( 1 ) ;
294
+ expect ( clightningService . checkChannels ) . toHaveBeenCalledTimes ( 1 ) ;
295
+
296
+ // the second time should not call setInterval or checkChannels again
297
+ await clightningService . subscribeChannelEvents ( node , mockCallback ) ;
298
+
299
+ expect ( setInterval ) . toHaveBeenCalledTimes ( 1 ) ;
300
+ expect ( clightningService . checkChannels ) . toHaveBeenCalledTimes ( 1 ) ;
291
301
292
- expect ( setInterval ) . toHaveBeenCalledTimes ( 0 ) ;
293
- expect ( clightningService . checkChannels ) . toHaveBeenCalledTimes ( 0 ) ;
302
+ jest . useRealTimers ( ) ;
294
303
} ) ;
295
304
296
305
it ( 'should throw an error when the implementation is not c-lightning' , async ( ) => {
@@ -333,8 +342,8 @@ describe('CLightningService', () => {
333
342
[ node . ports . rest ! ] : {
334
343
intervalId : setInterval ( ( ) => { } , 1000 ) ,
335
344
channels : [
336
- { channelID : '01ff' , pending : true , status : 'Opening' } ,
337
- { channelID : '04bb' , pending : false , status : 'Open' } ,
345
+ { channelId : '01ff' , status : 'Opening' } ,
346
+ { channelId : '04bb' , status : 'Open' } ,
338
347
] ,
339
348
} ,
340
349
} ;
@@ -352,8 +361,8 @@ describe('CLightningService', () => {
352
361
[ node . ports . rest ! ] : {
353
362
intervalId : setInterval ( ( ) => { } , 1000 ) ,
354
363
channels : [
355
- { channelID : '01ff' , pending : true , status : 'Opening' } ,
356
- { channelID : '04bb' , pending : false , status : 'Open' } ,
364
+ { channelId : '01ff' , status : 'Opening' } ,
365
+ { channelId : '04bb' , status : 'Open' } ,
357
366
] ,
358
367
} ,
359
368
} ;
@@ -385,8 +394,8 @@ describe('CLightningService', () => {
385
394
[ node . ports . rest ! ] : {
386
395
intervalId : setInterval ( ( ) => { } , 1000 ) ,
387
396
channels : [
388
- { channelID : '01ff' , pending : true , status : 'Opening' } ,
389
- { channelID : '04bb' , pending : false , status : 'Open' } ,
397
+ { channelId : '01ff' , status : 'Opening' } ,
398
+ { channelId : '04bb' , status : 'Open' } ,
390
399
] ,
391
400
} ,
392
401
} ;
0 commit comments