@@ -24,7 +24,6 @@ import { exhaustMicrotasks } from '../tests/testUtils';
24
24
import { ODP_USER_KEY } from './constant' ;
25
25
import { OptimizelySegmentOption } from './segment_manager/optimizely_segment_option' ;
26
26
import { OdpEventManager } from './event_manager/odp_event_manager' ;
27
- import exp from 'constants' ;
28
27
import { CLIENT_VERSION , JAVASCRIPT_CLIENT_ENGINE } from '../utils/enums' ;
29
28
30
29
const keyA = 'key-a' ;
@@ -62,49 +61,6 @@ const getMockOdpSegmentManager = () => {
62
61
} ;
63
62
64
63
describe ( 'DefaultOdpManager' , ( ) => {
65
- // let mockLogger: LogHandler;
66
- // let mockRequestHandler: RequestHandler;
67
-
68
- // let odpConfig: OdpConfig;
69
- // let logger: LogHandler;
70
- // let defaultRequestHandler: RequestHandler;
71
-
72
- // let mockEventApiManager: OdpEventApiManager;
73
- // let mockEventManager: OdpEventManager;
74
- // let mockSegmentApiManager: OdpSegmentApiManager;
75
- // let mockSegmentManager: OdpSegmentManager;
76
-
77
- // let eventApiManager: OdpEventApiManager;
78
- // let eventManager: OdpEventManager;
79
- // let segmentApiManager: OdpSegmentApiManager;
80
- // let segmentManager: OdpSegmentManager;
81
-
82
- // beforeAll(() => {
83
- // mockLogger = mock<LogHandler>();
84
- // mockRequestHandler = mock<RequestHandler>();
85
-
86
- // logger = instance(mockLogger);
87
- // defaultRequestHandler = instance(mockRequestHandler);
88
-
89
- // mockEventApiManager = mock<OdpEventApiManager>();
90
- // mockEventManager = mock<OdpEventManager>();
91
- // mockSegmentApiManager = mock<OdpSegmentApiManager>();
92
- // mockSegmentManager = mock<OdpSegmentManager>();
93
-
94
- // eventApiManager = instance(mockEventApiManager);
95
- // eventManager = instance(mockEventManager);
96
- // segmentApiManager = instance(mockSegmentApiManager);
97
- // segmentManager = instance(mockSegmentManager);
98
- // });
99
-
100
- // beforeEach(() => {
101
- // resetCalls(mockLogger);
102
- // resetCalls(mockRequestHandler);
103
- // resetCalls(mockEventApiManager);
104
- // resetCalls(mockEventManager);
105
- // resetCalls(mockSegmentManager);
106
- // });
107
-
108
64
it ( 'should be in new state on construction' , ( ) => {
109
65
const odpManager = new DefaultOdpManager ( {
110
66
segmentManager : getMockOdpSegmentManager ( ) ,
@@ -631,30 +587,113 @@ describe('DefaultOdpManager', () => {
631
587
expect ( identifiers ) . toEqual ( new Map ( [ [ 'vuid' , 'vuid_a' ] ] ) ) ;
632
588
} ) ;
633
589
590
+ it ( 'should reject onRunning() if stopped in new state' , async ( ) => {
591
+ const eventManager = getMockOdpEventManager ( ) ;
592
+ eventManager . onRunning . mockReturnValue ( Promise . resolve ( ) ) ;
593
+ eventManager . onTerminated . mockReturnValue ( Promise . resolve ( ) ) ;
594
+
595
+ const odpManager = new DefaultOdpManager ( {
596
+ segmentManager : getMockOdpSegmentManager ( ) ,
597
+ eventManager,
598
+ } ) ;
599
+
600
+ odpManager . stop ( ) ;
601
+
602
+ await expect ( odpManager . onRunning ( ) ) . rejects . toThrow ( ) ;
603
+ } ) ;
604
+
605
+ it ( 'should reject onRunning() if stopped in starting state' , async ( ) => {
606
+ const eventManager = getMockOdpEventManager ( ) ;
607
+ eventManager . onRunning . mockReturnValue ( Promise . resolve ( ) ) ;
608
+ eventManager . onTerminated . mockReturnValue ( Promise . resolve ( ) ) ;
609
+
610
+ const odpManager = new DefaultOdpManager ( {
611
+ segmentManager : getMockOdpSegmentManager ( ) ,
612
+ eventManager,
613
+ } ) ;
634
614
615
+ odpManager . start ( ) ;
616
+ expect ( odpManager . getState ( ) ) . toEqual ( ServiceState . Starting ) ;
635
617
636
- // it('should stop itself and eventManager if stop is called', async () => {
637
- // const odpIntegrationConfig: OdpIntegratedConfig = {
638
- // integrated: true,
639
- // odpConfig: new OdpConfig(keyA, hostA, pixelA, segmentsA)
640
- // };
618
+ odpManager . stop ( ) ;
619
+ await expect ( odpManager . onRunning ( ) ) . rejects . toThrow ( ) ;
620
+ } ) ;
621
+
622
+ it ( 'should go to stopping state and wait for eventManager to stop if stop is called' , async ( ) => {
623
+ const eventManager = getMockOdpEventManager ( ) ;
624
+ eventManager . onRunning . mockReturnValue ( Promise . resolve ( ) ) ;
625
+ eventManager . onTerminated . mockReturnValue ( resolvablePromise ( ) . promise ) ;
641
626
642
- // const odpManager = testOdpManager({
643
- // odpIntegrationConfig,
644
- // segmentManager,
645
- // eventManager,
646
- // logger,
647
- // vuidEnabled: true,
648
- // });
627
+ const odpManager = new DefaultOdpManager ( {
628
+ segmentManager : getMockOdpSegmentManager ( ) ,
629
+ eventManager,
630
+ } ) ;
649
631
650
- // await odpManager.onReady();
632
+ odpManager . start ( ) ;
633
+ odpManager . stop ( ) ;
651
634
652
- // odpManager.stop();
635
+ const terminatedHandler = vi . fn ( ) ;
636
+ odpManager . onTerminated ( ) . then ( terminatedHandler ) ;
653
637
654
- // expect(odpManager.getStatus()).toEqual(Status.Stopped);
655
- // verify(mockEventManager.stop()).once();
656
- // });
638
+ expect ( odpManager . getState ( ) ) . toEqual ( ServiceState . Stopping ) ;
639
+ await exhaustMicrotasks ( ) ;
640
+ expect ( terminatedHandler ) . not . toHaveBeenCalled ( ) ;
641
+ } ) ;
657
642
643
+ it ( 'should stop eventManager if stop is called' , async ( ) => {
644
+ const eventManager = getMockOdpEventManager ( ) ;
645
+ eventManager . onRunning . mockReturnValue ( Promise . resolve ( ) ) ;
646
+ eventManager . onTerminated . mockReturnValue ( Promise . resolve ( ) ) ;
658
647
648
+ const odpManager = new DefaultOdpManager ( {
649
+ segmentManager : getMockOdpSegmentManager ( ) ,
650
+ eventManager,
651
+ } ) ;
652
+
653
+ odpManager . start ( ) ;
654
+
655
+ odpManager . stop ( ) ;
656
+ expect ( eventManager . stop ) . toHaveBeenCalled ( ) ;
657
+ } ) ;
658
+
659
+ it ( 'should resolve onTerminated after eventManager stops successfully' , async ( ) => {
660
+ const eventManager = getMockOdpEventManager ( ) ;
661
+ eventManager . onRunning . mockReturnValue ( Promise . resolve ( ) ) ;
662
+ const eventManagerTerminatedPromise = resolvablePromise < void > ( ) ;
663
+ eventManager . onTerminated . mockReturnValue ( eventManagerTerminatedPromise . promise ) ;
664
+
665
+ const odpManager = new DefaultOdpManager ( {
666
+ segmentManager : getMockOdpSegmentManager ( ) ,
667
+ eventManager,
668
+ } ) ;
669
+
670
+ odpManager . start ( ) ;
671
+ odpManager . stop ( ) ;
672
+ await exhaustMicrotasks ( ) ;
673
+ expect ( odpManager . getState ( ) ) . toEqual ( ServiceState . Stopping ) ;
674
+
675
+ eventManagerTerminatedPromise . resolve ( ) ;
676
+ await expect ( odpManager . onTerminated ( ) ) . resolves . not . toThrow ( ) ;
677
+ } ) ;
678
+
679
+ it ( 'should reject onTerminated after eventManager fails to stop correctly' , async ( ) => {
680
+ const eventManager = getMockOdpEventManager ( ) ;
681
+ eventManager . onRunning . mockReturnValue ( Promise . resolve ( ) ) ;
682
+ const eventManagerTerminatedPromise = resolvablePromise < void > ( ) ;
683
+ eventManager . onTerminated . mockReturnValue ( eventManagerTerminatedPromise . promise ) ;
684
+
685
+ const odpManager = new DefaultOdpManager ( {
686
+ segmentManager : getMockOdpSegmentManager ( ) ,
687
+ eventManager,
688
+ } ) ;
689
+
690
+ odpManager . start ( ) ;
691
+ odpManager . stop ( ) ;
692
+ await exhaustMicrotasks ( ) ;
693
+ expect ( odpManager . getState ( ) ) . toEqual ( ServiceState . Stopping ) ;
694
+
695
+ eventManagerTerminatedPromise . reject ( new Error ( 'Failed to stop' ) ) ;
696
+ await expect ( odpManager . onTerminated ( ) ) . rejects . toThrow ( ) ;
697
+ } ) ;
659
698
} ) ;
660
699
0 commit comments