@@ -3,23 +3,28 @@ import * as idImportlibrary from 'modules/idImportLibrary.js';
3
3
4
4
var expect = require ( 'chai' ) . expect ;
5
5
6
- describe ( 'currency' , function ( ) {
7
- let fakeCurrencyFileServer ;
6
+ const mockMutationObserver = {
7
+ observe : ( ) => {
8
+ return null
9
+ }
10
+ }
11
+
12
+ describe ( 'IdImportLibrary Tests' , function ( ) {
13
+ let fakeServer ;
8
14
let sandbox ;
9
15
let clock ;
10
-
11
16
let fn = sinon . spy ( ) ;
12
17
13
18
beforeEach ( function ( ) {
14
- fakeCurrencyFileServer = sinon . fakeServer . create ( ) ;
19
+ fakeServer = sinon . fakeServer . create ( ) ;
15
20
sinon . stub ( utils , 'logInfo' ) ;
16
21
sinon . stub ( utils , 'logError' ) ;
17
22
} ) ;
18
23
19
24
afterEach ( function ( ) {
20
25
utils . logInfo . restore ( ) ;
21
26
utils . logError . restore ( ) ;
22
- fakeCurrencyFileServer . restore ( ) ;
27
+ fakeServer . restore ( ) ;
23
28
idImportlibrary . setConfig ( { } ) ;
24
29
} ) ;
25
30
@@ -34,28 +39,210 @@ describe('currency', function () {
34
39
clock . restore ( ) ;
35
40
} ) ;
36
41
37
- it ( 'results when no config available' , function ( ) {
42
+ it ( 'results when no config is set' , function ( ) {
43
+ idImportlibrary . setConfig ( ) ;
44
+ sinon . assert . called ( utils . logError ) ;
45
+ } ) ;
46
+ it ( 'results when config is empty' , function ( ) {
38
47
idImportlibrary . setConfig ( { } ) ;
39
48
sinon . assert . called ( utils . logError ) ;
40
49
} ) ;
41
- it ( 'results with config available' , function ( ) {
42
- idImportlibrary . setConfig ( { 'url' : 'URL' } ) ;
50
+ it ( 'results with config available with url and debounce ' , function ( ) {
51
+ idImportlibrary . setConfig ( { 'url' : 'URL' , 'debounce' : 0 } ) ;
43
52
sinon . assert . called ( utils . logInfo ) ;
44
53
} ) ;
54
+ it ( 'results with config debounce ' , function ( ) {
55
+ let config = { 'url' : 'URL' , 'debounce' : 300 }
56
+ idImportlibrary . setConfig ( config ) ;
57
+ expect ( config . debounce ) . to . be . equal ( 300 ) ;
58
+ } ) ;
59
+
45
60
it ( 'results with config default debounce ' , function ( ) {
46
61
let config = { 'url' : 'URL' }
47
62
idImportlibrary . setConfig ( config ) ;
48
63
expect ( config . debounce ) . to . be . equal ( 250 ) ;
49
64
} ) ;
50
65
it ( 'results with config default fullscan ' , function ( ) {
51
- let config = { 'url' : 'URL' }
66
+ let config = { 'url' : 'URL' , 'debounce' : 0 }
52
67
idImportlibrary . setConfig ( config ) ;
53
68
expect ( config . fullscan ) . to . be . equal ( false ) ;
54
69
} ) ;
55
70
it ( 'results with config fullscan ' , function ( ) {
56
- let config = { 'url' : 'URL' , 'fullscan' : true }
71
+ let config = { 'url' : 'URL' , 'fullscan' : true , 'debounce' : 0 }
72
+ idImportlibrary . setConfig ( config ) ;
73
+ expect ( config . fullscan ) . to . be . equal ( true ) ;
74
+ expect ( config . inputscan ) . to . be . equal ( false ) ;
75
+ } ) ;
76
+ it ( 'results with config inputscan ' , function ( ) {
77
+ let config = { 'inputscan' : true , 'debounce' : 0 }
78
+ idImportlibrary . setConfig ( config ) ;
79
+ expect ( config . inputscan ) . to . be . equal ( true ) ;
80
+ } ) ;
81
+ } ) ;
82
+ describe ( 'Test with email is found' , function ( ) {
83
+ let mutationObserverStub ;
84
+ let userId ;
85
+ let refreshUserIdSpy ;
86
+ beforeEach ( function ( ) {
87
+ let sandbox = sinon . createSandbox ( ) ;
88
+ refreshUserIdSpy = sinon . spy ( window . $$PREBID_GLOBAL$$ , 'refreshUserIds' ) ;
89
+ clock = sinon . useFakeTimers ( 1046952000000 ) ; // 2003-03-06T12:00:00Z
90
+ mutationObserverStub = sinon . stub ( window , 'MutationObserver' ) . returns ( mockMutationObserver ) ;
91
+ userId = sandbox . stub ( window . $$PREBID_GLOBAL$$ , 'getUserIds' ) . returns ( { id : { 'MOCKID' : '1111' } } ) ;
92
+ fakeServer . respondWith ( 'POST' , 'URL' , [ 200 ,
93
+ {
94
+ 'Content-Type' : 'application/json' ,
95
+ 'Access-Control-Allow-Origin' : '*'
96
+ } ,
97
+ ''
98
+ ] ) ;
99
+ } ) ;
100
+ afterEach ( function ( ) {
101
+ sandbox . restore ( ) ;
102
+ clock . restore ( ) ;
103
+ userId . restore ( ) ;
104
+ refreshUserIdSpy . restore ( ) ;
105
+ mutationObserverStub . restore ( ) ;
106
+ document . body . innerHTML = '' ;
107
+ } ) ;
108
+
109
+ it ( 'results with config fullscan with email found in html ' , function ( ) {
110
+ document . body . innerHTML = '<body><div>[email protected] </div></body>' ;
111
+ let config = { 'url' : 'URL' , 'fullscan' : true , 'debounce' : 0 }
112
+ idImportlibrary . setConfig ( config ) ;
113
+ expect ( config . fullscan ) . to . be . equal ( true ) ;
114
+ expect ( config . inputscan ) . to . be . equal ( false ) ;
115
+ expect ( refreshUserIdSpy . calledOnce ) . to . equal ( true ) ;
116
+ } ) ;
117
+
118
+ it ( 'results with config fullscan with no email found in html ' , function ( ) {
119
+ document . body . innerHTML = '<body><div>test</div></body>' ;
120
+ let config = { 'url' : 'URL' , 'fullscan' : true , 'debounce' : 0 }
121
+ idImportlibrary . setConfig ( config ) ;
122
+ expect ( config . fullscan ) . to . be . equal ( true ) ;
123
+ expect ( config . inputscan ) . to . be . equal ( false ) ;
124
+ expect ( refreshUserIdSpy . calledOnce ) . to . equal ( false ) ;
125
+ } ) ;
126
+
127
+ it ( 'results with config formElementId without listner ' , function ( ) {
128
+ let config = { url : 'testUrl' , 'formElementId' : 'userid' , 'debounce' : 0 }
129
+ document . body . innerHTML = '<body><input type="text" id="userid" value="[email protected] "></body>' ;
130
+ idImportlibrary . setConfig ( config ) ;
131
+ expect ( config . formElementId ) . to . be . equal ( 'userid' ) ;
132
+ expect ( refreshUserIdSpy . calledOnce ) . to . equal ( true ) ;
133
+ } ) ;
134
+
135
+ it ( 'results with config formElementId with listner ' , function ( ) {
136
+ let config = { url : 'testUrl' , 'formElementId' : 'userid' , 'debounce' : 0 }
137
+ document . body . innerHTML = '<body><input type="text" id="userid" value=""></body>' ;
138
+ idImportlibrary . setConfig ( config ) ;
139
+ expect ( config . formElementId ) . to . be . equal ( 'userid' ) ;
140
+ expect ( refreshUserIdSpy . calledOnce ) . to . equal ( false ) ;
141
+ } ) ;
142
+
143
+ it ( 'results with config target without listner ' , function ( ) {
144
+ let config = { url : 'testUrl' , 'target' : 'userid' , 'debounce' : 0 }
145
+ document . body . innerHTML = '<body><div id="userid">[email protected] <div></div></body>' ;
146
+ idImportlibrary . setConfig ( config ) ;
147
+ expect ( config . target ) . to . be . equal ( 'userid' ) ;
148
+ expect ( refreshUserIdSpy . calledOnce ) . to . equal ( true ) ;
149
+ } ) ;
150
+ it ( 'results with config target with listner ' , function ( ) {
151
+ let config = { url : 'testUrl' , 'target' : 'userid' , 'debounce' : 0 }
152
+ document . body . innerHTML = '<body><div id="userid"><div></div></body>' ;
153
+ idImportlibrary . setConfig ( config ) ;
154
+
155
+ expect ( config . target ) . to . be . equal ( 'userid' ) ;
156
+ expect ( refreshUserIdSpy . calledOnce ) . to . equal ( false ) ;
157
+ } ) ;
158
+
159
+ it ( 'results with config target with listner' , function ( ) {
160
+ let config = { url : 'testUrl' , 'target' : 'userid' , 'debounce' : 0 }
161
+ idImportlibrary . setConfig ( config ) ;
162
+ document . body . innerHTML = '<body><div id="userid">[email protected] <div></div></body>' ;
163
+ expect ( config . target ) . to . be . equal ( 'userid' ) ;
164
+ expect ( refreshUserIdSpy . calledOnce ) . to . equal ( false ) ;
165
+ } ) ;
166
+ it ( 'results with config fullscan ' , function ( ) {
167
+ let config = { url : 'testUrl' , 'fullscan' : true , 'debounce' : 0 }
57
168
idImportlibrary . setConfig ( config ) ;
169
+ document . body . innerHTML = '<body><div id="userid"><div></div></body>' ;
58
170
expect ( config . fullscan ) . to . be . equal ( true ) ;
171
+ expect ( refreshUserIdSpy . calledOnce ) . to . equal ( false ) ;
172
+ } ) ;
173
+ it ( 'results with config inputscan with listner' , function ( ) {
174
+ let config = { url : 'testUrl' , 'inputscan' : true , 'debounce' : 0 }
175
+ var input = document . createElement ( 'input' ) ;
176
+ input . setAttribute ( 'type' , 'text' ) ;
177
+ document . body . appendChild ( input ) ;
178
+ idImportlibrary . setConfig ( config ) ;
179
+ expect ( config . inputscan ) . to . be . equal ( true ) ;
180
+ input . setAttribute ( 'value' , '[email protected] ' ) ;
181
+ const inputEvent = new InputEvent ( 'blur' ) ;
182
+ input . dispatchEvent ( inputEvent ) ;
183
+ expect ( refreshUserIdSpy . calledOnce ) . to . equal ( true ) ;
184
+ } ) ;
185
+
186
+ it ( 'results with config inputscan with listner and no user ids ' , function ( ) {
187
+ let config = { 'url' : 'testUrl' , 'inputscan' : true , 'debounce' : 0 }
188
+ document . body . innerHTML = '<body><input id="userid" value=""></body>' ;
189
+ idImportlibrary . setConfig ( config ) ;
190
+ expect ( config . inputscan ) . to . be . equal ( true ) ;
191
+ expect ( refreshUserIdSpy . calledOnce ) . to . equal ( false ) ;
192
+ } ) ;
193
+
194
+ it ( 'results with config inputscan with listner ' , function ( ) {
195
+ let config = { 'url' : 'testUrl' , 'inputscan' : true , 'debounce' : 0 }
196
+ document . body . innerHTML = '<body><input id="userid" type=text value=""></body>' ;
197
+ idImportlibrary . setConfig ( config ) ;
198
+ expect ( config . inputscan ) . to . be . equal ( true ) ;
199
+ expect ( refreshUserIdSpy . calledOnce ) . to . equal ( false ) ;
200
+ } ) ;
201
+
202
+ it ( 'results with config inputscan without listner ' , function ( ) {
203
+ let config = { 'url' : 'testUrl' , 'inputscan' : true , 'debounce' : 0 }
204
+ document . body . innerHTML = '<body><input id="userid" value="[email protected] "></body>' ;
205
+ idImportlibrary . setConfig ( config ) ;
206
+ expect ( config . inputscan ) . to . be . equal ( true ) ;
207
+ expect ( refreshUserIdSpy . calledOnce ) . to . equal ( true ) ;
208
+ } ) ;
209
+ } ) ;
210
+ describe ( 'Tests with no user ids' , function ( ) {
211
+ let mutationObserverStub ;
212
+ let userId ;
213
+ let jsonSpy ;
214
+ beforeEach ( function ( ) {
215
+ let sandbox = sinon . createSandbox ( ) ;
216
+ clock = sinon . useFakeTimers ( 1046952000000 ) ; // 2003-03-06T12:00:00Z
217
+ mutationObserverStub = sinon . stub ( window , 'MutationObserver' ) ;
218
+ jsonSpy = sinon . spy ( JSON , 'stringify' ) ;
219
+ fakeServer . respondWith ( 'POST' , 'URL' , [ 200 ,
220
+ {
221
+ 'Content-Type' : 'application/json' ,
222
+ 'Access-Control-Allow-Origin' : '*'
223
+ } ,
224
+ ''
225
+ ] ) ;
226
+ } ) ;
227
+ afterEach ( function ( ) {
228
+ sandbox . restore ( ) ;
229
+ clock . restore ( ) ;
230
+ jsonSpy . restore ( ) ;
231
+ mutationObserverStub . restore ( ) ;
232
+ } ) ;
233
+ it ( 'results with config inputscan without listner with no user ids ' , function ( ) {
234
+ let config = { 'url' : 'testUrl' , 'inputscan' : true , 'debounce' : 0 }
235
+ document . body . innerHTML = '<body><input id="userid" value="[email protected] "></body>' ;
236
+ idImportlibrary . setConfig ( config ) ;
237
+ expect ( config . inputscan ) . to . be . equal ( true ) ;
238
+ expect ( jsonSpy . calledOnce ) . to . equal ( false ) ;
239
+ } ) ;
240
+ it ( 'results with config inputscan without listner with no user ids ' , function ( ) {
241
+ let config = { 'url' : 'testUrl' , 'inputscan' : true , 'debounce' : 0 }
242
+ document . body . innerHTML = '<body><input id="userid"></body>' ;
243
+ idImportlibrary . setConfig ( config ) ;
244
+ expect ( config . inputscan ) . to . be . equal ( true ) ;
245
+ expect ( jsonSpy . calledOnce ) . to . equal ( false ) ;
59
246
} ) ;
60
247
} ) ;
61
248
} ) ;
0 commit comments