@@ -26,7 +26,7 @@ var childWindow = createPopup('child.htm');
26
26
27
27
var otherChildFrame = createIframe ( 'child.htm' ) ;
28
28
29
- describe ( 'post-robot' , function ( ) {
29
+ describe ( '[ post-robot] happy cases ' , function ( ) {
30
30
31
31
it ( 'should set up a simple server and listen for a request' , function ( done ) {
32
32
@@ -75,26 +75,6 @@ describe('post-robot', function() {
75
75
} ) . catch ( done ) ;
76
76
} ) ;
77
77
78
- it ( 'should get an error when messaging with an unknown name' , function ( ) {
79
-
80
- return postRobot . send ( childFrame , 'doesntexist' ) . then ( function ( data ) {
81
- throw new Error ( 'Expected success handler to not be called' ) ;
82
- } , function ( err ) {
83
- assert . ok ( err ) ;
84
- } ) ;
85
- } ) ;
86
-
87
- it ( 'should get a callback error when messaging with an unknown name' , function ( done ) {
88
-
89
- postRobot . send ( childFrame , 'doesntexist' , function ( err , data ) {
90
- assert . ok ( err ) ;
91
- if ( data ) {
92
- throw new Error ( 'Expected data to be blank' ) ;
93
- }
94
- done ( ) ;
95
- } ) ;
96
- } ) ;
97
-
98
78
it ( 'should pass a function across windows and be able to call it later' , function ( done ) {
99
79
100
80
postRobot . send ( childFrame , 'setupListener' , {
@@ -112,7 +92,9 @@ describe('post-robot', function() {
112
92
} ) ;
113
93
} ) ;
114
94
115
- it ( 'should work when referencing the child by id' , function ( ) {
95
+ it . skip ( 'should be able to proxy messsages from one window to another' , function ( ) {
96
+
97
+ postRobot . proxy ( childFrame , otherChildFrame ) ;
116
98
117
99
return postRobot . send ( childFrame , 'setupListener' , {
118
100
@@ -123,15 +105,23 @@ describe('post-robot', function() {
123
105
124
106
} ) . then ( function ( ) {
125
107
126
- return postRobot . send ( 'childframe' , 'foo' ) . then ( function ( data ) {
108
+ return postRobot . send ( otherChildFrame , 'sendMessageToParent' , {
109
+ messageName : 'foo' ,
110
+
111
+
112
+ } ) . then ( function ( data ) {
127
113
assert . equal ( data . foo , 'bar' ) ;
128
114
} ) ;
129
115
} ) ;
130
116
} ) ;
117
+ } ) ;
131
118
132
- it ( 'should work with a child window' , function ( ) {
133
119
134
- return postRobot . send ( childWindow , 'setupListener' , {
120
+ describe ( '[post-robot] options' , function ( ) {
121
+
122
+ it ( 'should work when referencing the child by id' , function ( ) {
123
+
124
+ return postRobot . send ( childFrame , 'setupListener' , {
135
125
136
126
messageName : 'foo' ,
137
127
data : {
@@ -140,7 +130,7 @@ describe('post-robot', function() {
140
130
141
131
} ) . then ( function ( ) {
142
132
143
- return postRobot . send ( childWindow , 'foo' ) . then ( function ( data ) {
133
+ return postRobot . send ( 'childframe' , 'foo' ) . then ( function ( data ) {
144
134
assert . equal ( data . foo , 'bar' ) ;
145
135
} ) ;
146
136
} ) ;
@@ -159,28 +149,43 @@ describe('post-robot', function() {
159
149
} ) . then ( function ( ) {
160
150
return postRobot . send ( childFrame , 'sendMessageToParent' , {
161
151
messageName : 'foobu'
162
- } )
163
- } ) . then ( function ( ) {
164
- assert . equal ( count , 1 ) ;
152
+ } ) . then ( function ( ) {
153
+ throw new Error ( 'Expected success handler to not be called' ) ;
154
+ } , function ( ) {
155
+ assert . equal ( count , 1 ) ;
156
+ } ) ;
165
157
} ) ;
166
158
} ) ;
167
159
168
- it ( 'should error out if you try to register the same listener name twice ' , function ( ) {
160
+ it . skip ( 'should be able to re- register the same once handler after the first is called ' , function ( ) {
169
161
170
- postRobot . on ( 'onceonly' , function ( ) {
171
- // pass
162
+ var count = 0 ;
163
+
164
+ postRobot . once ( 'foobu' , { override : true } , function ( source , data ) {
165
+ count += data . add ;
172
166
} ) ;
173
167
174
- try {
175
- postRobot . on ( 'onceonly' , function ( ) {
176
- // pass
168
+ return postRobot . send ( childFrame , 'sendMessageToParent' , {
169
+ messageName : 'foobu' ,
170
+ data : {
171
+ add : 2
172
+ }
173
+ } ) . then ( function ( ) {
174
+
175
+ postRobot . once ( 'foobu' , { override : true } , function ( ) {
176
+ count += data . add ;
177
177
} ) ;
178
- } catch ( err ) {
179
- assert . ok ( err ) ;
180
- return ;
181
- }
182
178
183
- throw new Error ( 'Expected error handler to be called' ) ;
179
+ return postRobot . send ( childFrame , 'sendMessageToParent' , {
180
+ messageName : 'foobu' ,
181
+ data : {
182
+ add : 3
183
+ }
184
+ } ) ;
185
+
186
+ } ) . then ( function ( ) {
187
+ assert . equal ( count , 5 ) ;
188
+ } ) ;
184
189
} ) ;
185
190
186
191
it ( 'should allow you to register the same listener twice providing it is to different windows' , function ( ) {
@@ -207,19 +212,60 @@ describe('post-robot', function() {
207
212
} ) . then ( function ( ) {
208
213
return postRobot . send ( childFrame , 'sendMessageToParent' , {
209
214
messageName : 'specificchildlistener'
210
- } )
211
- } ) . then ( function ( ) {
212
- assert . equal ( count , 1 ) ;
215
+ } ) . then ( function ( ) {
216
+ throw new Error ( 'Expected success handler to not be called' ) ;
217
+ } , function ( err ) {
218
+ assert . ok ( err ) ;
219
+ assert . equal ( count , 1 ) ;
220
+ } ) ;
213
221
} ) ;
214
222
} ) ;
223
+ } ) ;
224
+
225
+
226
+ describe ( '[post-robot] error cases' , function ( ) {
227
+
228
+ it ( 'should get an error when messaging with an unknown name' , function ( ) {
229
+
230
+ return postRobot . send ( childFrame , 'doesntexist' ) . then ( function ( data ) {
231
+ throw new Error ( 'Expected success handler to not be called' ) ;
232
+ } , function ( err ) {
233
+ assert . ok ( err ) ;
234
+ } ) ;
235
+ } ) ;
236
+
237
+ it ( 'should get a callback error when messaging with an unknown name' , function ( done ) {
238
+
239
+ postRobot . send ( childFrame , 'doesntexist' , function ( err , data ) {
240
+ assert . ok ( err ) ;
241
+ if ( data ) {
242
+ throw new Error ( 'Expected data to be blank' ) ;
243
+ }
244
+ done ( ) ;
245
+ } ) ;
246
+ } ) ;
247
+
248
+ it ( 'should error out if you try to register the same listener name twice' , function ( ) {
249
+
250
+ postRobot . on ( 'onceonly' , function ( ) {
251
+ // pass
252
+ } ) ;
215
253
216
- /*
254
+ try {
255
+ postRobot . on ( 'onceonly' , function ( ) {
256
+ // pass
257
+ } ) ;
258
+ } catch ( err ) {
259
+ assert . ok ( err ) ;
260
+ return ;
261
+ }
262
+
263
+ throw new Error ( 'Expected error handler to be called' ) ;
264
+ } ) ;
217
265
218
266
it ( 'should fail when postMessage or global methods are not available' , function ( done ) {
219
267
220
- delete window.postMessage;
221
268
delete window . __postRobot__ ;
222
- delete window.frames;
223
269
224
270
Object . defineProperty ( window , 'postMessage' , {
225
271
value : function ( ) {
@@ -235,6 +281,25 @@ describe('post-robot', function() {
235
281
messageName : 'nowayin'
236
282
} ) ;
237
283
} ) ;
284
+ } ) ;
285
+
286
+
287
+ describe ( '[post-robot] popup tests' , function ( ) {
288
+
289
+ it ( 'should work with a child window' , function ( ) {
238
290
239
- */
291
+ return postRobot . send ( childWindow , 'setupListener' , {
292
+
293
+ messageName : 'foo' ,
294
+ data : {
295
+ foo : 'bar'
296
+ }
297
+
298
+ } ) . then ( function ( ) {
299
+
300
+ return postRobot . send ( childWindow , 'foo' ) . then ( function ( data ) {
301
+ assert . equal ( data . foo , 'bar' ) ;
302
+ } ) ;
303
+ } ) ;
304
+ } ) ;
240
305
} ) ;
0 commit comments