1+ import RoktManager , { IRoktLauncher , IRoktSelectPlacementsOptions } from "../../src/roktManager" ;
2+
3+ describe ( 'RoktManager' , ( ) => {
4+ let roktManager : RoktManager ;
5+
6+ beforeEach ( ( ) => {
7+ roktManager = new RoktManager ( ) ;
8+ } ) ;
9+
10+ describe ( 'constructor' , ( ) => {
11+ it ( 'should be initialized' , ( ) => {
12+ expect ( roktManager ) . toBeDefined ( ) ;
13+ } ) ;
14+
15+ it ( 'should have a null launcher' , ( ) => {
16+ expect ( roktManager [ 'launcher' ] ) . toBeNull ( ) ;
17+ } ) ;
18+ } ) ;
19+
20+ describe ( '#attachLauncher' , ( ) => {
21+ it ( 'should attach a launcher' , ( ) => {
22+ const launcher = { } as IRoktLauncher ;
23+ roktManager . attachLauncher ( launcher ) ;
24+ expect ( roktManager [ 'launcher' ] ) . not . toBeNull ( ) ;
25+ } ) ;
26+
27+ it ( 'should process the message queue if a launcher is attached' , ( ) => {
28+ const launcher : IRoktLauncher = {
29+ selectPlacements : jest . fn ( )
30+ } ;
31+
32+ roktManager . selectPlacements ( { } as IRoktSelectPlacementsOptions ) ;
33+ roktManager . selectPlacements ( { } as IRoktSelectPlacementsOptions ) ;
34+ roktManager . selectPlacements ( { } as IRoktSelectPlacementsOptions ) ;
35+
36+ expect ( roktManager [ 'messageQueue' ] . length ) . toBe ( 3 ) ;
37+
38+ roktManager . attachLauncher ( launcher ) ;
39+ expect ( roktManager [ 'launcher' ] ) . not . toBeNull ( ) ;
40+ expect ( roktManager [ 'messageQueue' ] . length ) . toBe ( 0 ) ;
41+ expect ( launcher . selectPlacements ) . toHaveBeenCalledTimes ( 3 ) ;
42+ } ) ;
43+ } ) ;
44+
45+ describe ( '#selectPlacements' , ( ) => {
46+ it ( 'should call launcher.selectPlacements with empty attributes' , ( ) => {
47+ const launcher : IRoktLauncher = {
48+ selectPlacements : jest . fn ( )
49+ } ;
50+
51+ roktManager . attachLauncher ( launcher ) ;
52+ const options = {
53+ attributes : { }
54+ } as IRoktSelectPlacementsOptions ;
55+
56+ roktManager . selectPlacements ( options ) ;
57+ expect ( launcher . selectPlacements ) . toHaveBeenCalledWith ( options ) ;
58+ } ) ;
59+
60+ it ( 'should call launcher.selectPlacements with passed in attributes' , ( ) => {
61+ const launcher : IRoktLauncher = {
62+ selectPlacements : jest . fn ( )
63+ } ;
64+
65+ roktManager . attachLauncher ( launcher ) ;
66+
67+ const options : IRoktSelectPlacementsOptions = {
68+ attributes : {
69+ age : 25 ,
70+ score : 100.5 ,
71+ isSubscribed : true ,
72+ isActive : false ,
73+ interests : 'sports,music,books'
74+ }
75+ } ;
76+
77+ roktManager . selectPlacements ( options ) ;
78+ expect ( launcher . selectPlacements ) . toHaveBeenCalledWith ( options ) ;
79+ } ) ;
80+
81+ it ( 'should queue the selectPlacements method if no launcher is attached' , ( ) => {
82+ const options = {
83+ attributes : { }
84+ } as IRoktSelectPlacementsOptions ;
85+
86+ roktManager . selectPlacements ( options ) ;
87+
88+ expect ( roktManager [ 'launcher' ] ) . toBeNull ( ) ;
89+ expect ( roktManager [ 'messageQueue' ] . length ) . toBe ( 1 ) ;
90+ expect ( roktManager [ 'messageQueue' ] [ 0 ] . methodName ) . toBe ( 'selectPlacements' ) ;
91+ expect ( roktManager [ 'messageQueue' ] [ 0 ] . payload ) . toBe ( options ) ;
92+ } ) ;
93+
94+ it ( 'should process queued selectPlacements calls once the launcher is attached' , ( ) => {
95+ const launcher : IRoktLauncher = {
96+ selectPlacements : jest . fn ( )
97+ } ;
98+
99+ const options = {
100+ attributes : { }
101+ } as IRoktSelectPlacementsOptions ;
102+
103+ roktManager . selectPlacements ( options ) ;
104+ expect ( roktManager [ 'launcher' ] ) . toBeNull ( ) ;
105+ expect ( roktManager [ 'messageQueue' ] . length ) . toBe ( 1 ) ;
106+ expect ( roktManager [ 'messageQueue' ] [ 0 ] . methodName ) . toBe ( 'selectPlacements' ) ;
107+ expect ( roktManager [ 'messageQueue' ] [ 0 ] . payload ) . toBe ( options ) ;
108+
109+ roktManager . attachLauncher ( launcher ) ;
110+ expect ( roktManager [ 'launcher' ] ) . not . toBeNull ( ) ;
111+ expect ( roktManager [ 'messageQueue' ] . length ) . toBe ( 0 ) ;
112+ expect ( launcher . selectPlacements ) . toHaveBeenCalledWith ( options ) ;
113+ } ) ;
114+ } ) ;
115+ } ) ;
0 commit comments