|
1 | | -/* global $:false */ |
| 1 | +/* global $:false, jQuery:false */ |
2 | 2 |
|
3 | 3 | /* |
4 | 4 | * Note this test requires JQuery in the global scope. |
5 | 5 | * we should keep it that way to keep testing our backward |
6 | 6 | * compatibility with JQuery events. |
7 | 7 | */ |
8 | 8 |
|
9 | | - |
10 | 9 | var Events = require('@src/lib/events'); |
11 | 10 |
|
12 | 11 | describe('Events', function() { |
@@ -62,7 +61,6 @@ describe('Events', function() { |
62 | 61 | it('triggers jquery events', function(done) { |
63 | 62 | Events.init(plotDiv); |
64 | 63 |
|
65 | | - |
66 | 64 | $(plotDiv).bind('ping', function(event, data) { |
67 | 65 | expect(data).toBe('pong'); |
68 | 66 | done(); |
@@ -192,4 +190,107 @@ describe('Events', function() { |
192 | 190 | }); |
193 | 191 | }); |
194 | 192 |
|
| 193 | + describe('when jQuery.noConflict is set, ', function() { |
| 194 | + |
| 195 | + beforeEach(function() { |
| 196 | + $.noConflict(); |
| 197 | + }); |
| 198 | + |
| 199 | + afterEach(function() { |
| 200 | + window.$ = jQuery; |
| 201 | + }); |
| 202 | + |
| 203 | + it('triggers jquery events', function(done) { |
| 204 | + |
| 205 | + Events.init(plotDiv); |
| 206 | + |
| 207 | + jQuery(plotDiv).bind('ping', function(event, data) { |
| 208 | + expect(data).toBe('pong'); |
| 209 | + done(); |
| 210 | + }); |
| 211 | + |
| 212 | + setTimeout(function() { |
| 213 | + jQuery(plotDiv).trigger('ping', 'pong'); |
| 214 | + }); |
| 215 | + }); |
| 216 | + |
| 217 | + it('triggers jQuery handlers when no matching node events bound', function() { |
| 218 | + var eventBaton = 0; |
| 219 | + |
| 220 | + Events.init(plotDiv); |
| 221 | + |
| 222 | + jQuery(plotDiv).bind('ping', function() { |
| 223 | + eventBaton++; |
| 224 | + return 'ping'; |
| 225 | + }); |
| 226 | + |
| 227 | + /* |
| 228 | + * This will not be called |
| 229 | + */ |
| 230 | + plotDiv.on('pong', function() { |
| 231 | + eventBaton++; |
| 232 | + return 'ping'; |
| 233 | + }); |
| 234 | + |
| 235 | + jQuery(plotDiv).bind('ping', function() { |
| 236 | + eventBaton++; |
| 237 | + return 'pong'; |
| 238 | + }); |
| 239 | + |
| 240 | + var result = Events.triggerHandler(plotDiv, 'ping'); |
| 241 | + |
| 242 | + expect(eventBaton).toBe(2); |
| 243 | + expect(result).toBe('pong'); |
| 244 | + }); |
| 245 | + |
| 246 | + it('triggers jQuery handlers when no node events initialized', function() { |
| 247 | + var eventBaton = 0; |
| 248 | + |
| 249 | + jQuery(plotDiv).bind('ping', function() { |
| 250 | + eventBaton++; |
| 251 | + return 'ping'; |
| 252 | + }); |
| 253 | + |
| 254 | + jQuery(plotDiv).bind('ping', function() { |
| 255 | + eventBaton++; |
| 256 | + return 'ping'; |
| 257 | + }); |
| 258 | + |
| 259 | + jQuery(plotDiv).bind('ping', function() { |
| 260 | + eventBaton++; |
| 261 | + return 'pong'; |
| 262 | + }); |
| 263 | + |
| 264 | + var result = Events.triggerHandler(plotDiv, 'ping'); |
| 265 | + |
| 266 | + expect(eventBaton).toBe(3); |
| 267 | + expect(result).toBe('pong'); |
| 268 | + }); |
| 269 | + |
| 270 | + it('triggers jQuery + nodejs handlers and returns last jQuery value', function() { |
| 271 | + var eventBaton = 0; |
| 272 | + |
| 273 | + Events.init(plotDiv); |
| 274 | + |
| 275 | + jQuery(plotDiv).bind('ping', function() { |
| 276 | + eventBaton++; |
| 277 | + return 'ping'; |
| 278 | + }); |
| 279 | + |
| 280 | + plotDiv.on('ping', function() { |
| 281 | + eventBaton++; |
| 282 | + return 'ping'; |
| 283 | + }); |
| 284 | + |
| 285 | + jQuery(plotDiv).bind('ping', function() { |
| 286 | + eventBaton++; |
| 287 | + return 'pong'; |
| 288 | + }); |
| 289 | + |
| 290 | + var result = Events.triggerHandler(plotDiv, 'ping'); |
| 291 | + |
| 292 | + expect(eventBaton).toBe(3); |
| 293 | + expect(result).toBe('pong'); |
| 294 | + }); |
| 295 | + }); |
195 | 296 | }); |
0 commit comments