8000 Make events work in jQuery no-conflict scopes [fixes #350] by etpinard · Pull Request #352 · plotly/plotly.js · GitHub
[go: up one dir, main page]

Skip to content

Make events work in jQuery no-conflict scopes [fixes #350] #352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 23, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
add event test for jQuery.noConflict scopes
  • Loading branch information
etpinard committed Mar 23, 2016
commit 2a77b03c09bf6be855ce2cd365472412497f0d90
107 changes: 104 additions & 3 deletions test/jasmine/tests/events_test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/* global $:false */
/* global $:false, jQuery:false */

/*
* Note this test requires JQuery in the global scope.
* we should keep it that way to keep testing our backward
* compatibility with JQuery events.
*/


var Events = require('@src/lib/events');

describe('Events', function() {
Expand Down Expand Up @@ -62,7 +61,6 @@ describe('Events', function() {
it('triggers jquery events', function(done) {
Events.init(plotDiv);


$(plotDiv).bind('ping', function(event, data) {
expect(data).toBe('pong');
done();
Expand Down Expand Up @@ -192,4 +190,107 @@ describe('Events', function() {
});
});

describe('when jQuery.noConflict is set, ', function() {

beforeEach(function() {
$.noConflict();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

});

afterEach(function() {
window.$ = jQuery;
});

it('triggers jquery events', function(done) {

Events.init(plotDiv);

jQuery(plotDiv).bind('ping', function(event, data) {
expect(data).toBe('pong');
done();
});

setTimeout(function() {
jQuery(plotDiv).trigger('ping', 'pong');
});
});

it('triggers jQuery handlers when no matching node events bound', function() {
var eventBaton = 0;

Events.init(plotDiv);

jQuery(plotDiv).bind('ping', function() {
eventBaton++;
return 'ping';
});

/*
* This will not be called
*/
plotDiv.on('pong', function() {
eventBaton++;
return 'ping';
});

jQuery(plotDiv).bind('ping', function() {
eventBaton++;
return 'pong';
});

var result = Events.triggerHandler(plotDiv, 'ping');

expect(eventBaton).toBe(2);
expect(result).toBe('pong');
});

it('triggers jQuery handlers when no node events initialized', function() {
var eventBaton = 0;

jQuery(plotDiv).bind('ping', function() {
eventBaton++;
return 'ping';
});

jQuery(plotDiv).bind('ping', function() {
eventBaton++;
return 'ping';
});

jQuery(plotDiv).bind('ping', function() {
eventBaton++;
return 'pong';
});

var result = Events.triggerHandler(plotDiv, 'ping');

expect(eventBaton).toBe(3);
expect(result).toBe('pong');
});

it('triggers jQuery + nodejs handlers and returns last jQuery value', function() {
var eventBaton = 0;

Events.init(plotDiv);

jQuery(plotDiv).bind('ping', function() {
eventBaton++;
return 'ping';
});

plotDiv.on('ping', function() {
eventBaton++;
return 'ping';
});

jQuery(plotDiv).bind('ping', function() {
eventBaton++;
return 'pong';
});

var result = Events.triggerHandler(plotDiv, 'ping');

expect(eventBaton).toBe(3);
expect(result).toBe('pong');
});
});
});
0