mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 17:28:26 -04:00
adding tests for the on and off methods to vis.js
This commit is contained in:
parent
afeb314cec
commit
adcd177ebb
3 changed files with 187 additions and 3 deletions
|
@ -171,9 +171,10 @@ define(function (require) {
|
|||
charts.forEach(function (chart) {
|
||||
this.handler.disable(event, chart);
|
||||
}, this);
|
||||
} else {
|
||||
this.eventTypes.enabled.splice(eventIndex, 1);
|
||||
}
|
||||
|
||||
// Remove event from enabled array
|
||||
this.eventTypes.enabled.splice(eventIndex, 1);
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
|
|
@ -150,6 +150,14 @@ define(function (require) {
|
|||
// });
|
||||
// });
|
||||
|
||||
describe('enable Method', function () {
|
||||
|
||||
});
|
||||
|
||||
describe('disable Method', function () {
|
||||
|
||||
});
|
||||
|
||||
describe('removeAll Method', function () {
|
||||
beforeEach(function () {
|
||||
inject(function () {
|
||||
|
|
|
@ -6,6 +6,8 @@ define(function (require) {
|
|||
angular.module('VisFactory', ['kibana']);
|
||||
|
||||
describe('VisLib Vis Test Suite', function () {
|
||||
var beforeEvent = 'click';
|
||||
var afterEvent = 'brush';
|
||||
var Vis;
|
||||
var vis;
|
||||
var el;
|
||||
|
@ -87,8 +89,10 @@ define(function (require) {
|
|||
});
|
||||
|
||||
afterEach(function () {
|
||||
el.remove();
|
||||
vis.off(beforeEvent);
|
||||
vis.off(afterEvent);
|
||||
vis.destroy();
|
||||
el.remove();
|
||||
});
|
||||
|
||||
describe('render Method', function () {
|
||||
|
@ -145,5 +149,176 @@ define(function (require) {
|
|||
expect(vis.get('type')).to.be('histogram');
|
||||
});
|
||||
});
|
||||
|
||||
describe('on Method', function () {
|
||||
var events = [
|
||||
beforeEvent,
|
||||
afterEvent
|
||||
];
|
||||
var listeners;
|
||||
var listener1;
|
||||
var listener2;
|
||||
|
||||
beforeEach(function () {
|
||||
listeners = [];
|
||||
listener1 = function (e) {
|
||||
console.log(e, 'listener1');
|
||||
};
|
||||
listener2 = function (e) {
|
||||
console.log(e, 'listener2');
|
||||
};
|
||||
listeners.push(listener1);
|
||||
listeners.push(listener2);
|
||||
|
||||
// Add event and listeners to chart
|
||||
listeners.forEach(function (listener) {
|
||||
vis.on(beforeEvent, listener);
|
||||
});
|
||||
|
||||
// Render chart
|
||||
vis.render(data);
|
||||
|
||||
// Add event after charts have rendered
|
||||
listeners.forEach(function (listener) {
|
||||
vis.on(afterEvent, listener);
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
vis.off(beforeEvent);
|
||||
vis.off(afterEvent);
|
||||
});
|
||||
|
||||
it('should add an event and its listeners to the _listeners object', function () {
|
||||
// Test for presence of beforeEvent in _listener object
|
||||
expect(vis._listeners[beforeEvent] instanceof Array).to.be(true);
|
||||
|
||||
vis._listeners[beforeEvent].forEach(function (listener, i) {
|
||||
expect(typeof listener.handler).to.be('function');
|
||||
expect(listener.handler).to.be(listeners[i]);
|
||||
});
|
||||
|
||||
vis._listeners[afterEvent].forEach(function (listener, i) {
|
||||
expect(typeof listener.handler).to.be('function');
|
||||
expect(listener.handler).to.be(listeners[i]);
|
||||
});
|
||||
});
|
||||
|
||||
it('should add an event to the eventTypes.enabled array', function () {
|
||||
vis.eventTypes.enabled.forEach(function (eventType, i) {
|
||||
expect(eventType).to.be(events[i]);
|
||||
});
|
||||
});
|
||||
|
||||
it('should attach an event and its listeners to the chart', function () {
|
||||
var charts = vis.handler.charts;
|
||||
|
||||
charts.forEach(function (chart, i) {
|
||||
expect(typeof chart.on(beforeEvent) === 'function');
|
||||
expect(typeof chart.on(afterEvent) === 'function');
|
||||
expect(chart.on(beforeEvent) === listeners[i]);
|
||||
expect(chart.on(afterEvent) === listeners[i]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('off Method', function () {
|
||||
var listeners;
|
||||
var listener1;
|
||||
var listener2;
|
||||
|
||||
beforeEach(function () {
|
||||
listeners = [];
|
||||
listener1 = function (e) {
|
||||
console.log(e, 'listener1');
|
||||
};
|
||||
listener2 = function (e) {
|
||||
console.log(e, 'listener2');
|
||||
};
|
||||
listeners.push(listener1);
|
||||
listeners.push(listener2);
|
||||
|
||||
// Add event and listeners to chart
|
||||
listeners.forEach(function (listener) {
|
||||
vis.on(beforeEvent, listener);
|
||||
});
|
||||
|
||||
// Turn off event listener before chart rendered
|
||||
vis.off(beforeEvent, listener1);
|
||||
|
||||
// Render chart
|
||||
vis.render(data);
|
||||
|
||||
// Add event after charts have rendered
|
||||
listeners.forEach(function (listener) {
|
||||
vis.on(afterEvent, listener);
|
||||
});
|
||||
|
||||
// Turn off event listener after chart is rendered
|
||||
vis.off(afterEvent, listener1);
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
vis.off(beforeEvent);
|
||||
vis.off(afterEvent);
|
||||
});
|
||||
|
||||
it('should remove a listener from the _listeners[event] array', function () {
|
||||
var charts = vis.handler.charts;
|
||||
|
||||
expect(vis._listeners[beforeEvent].length).to.be(1);
|
||||
expect(vis._listeners[afterEvent].length).to.be(1);
|
||||
|
||||
// should still have the 2 events in the eventTypes enabled array
|
||||
expect(vis.eventTypes.enabled.length).to.be(2);
|
||||
|
||||
// Test that listener that was not removed is still present
|
||||
vis._listeners[beforeEvent].forEach(function (listener) {
|
||||
expect(typeof listener.handler).to.be('function');
|
||||
expect(listener.handler).to.be(listener2);
|
||||
});
|
||||
|
||||
vis._listeners[afterEvent].forEach(function (listener) {
|
||||
expect(typeof listener.handler).to.be('function');
|
||||
expect(listener.handler).to.be(listener2);
|
||||
});
|
||||
|
||||
// Events should still be attached to charts
|
||||
charts.forEach(function (chart) {
|
||||
expect(typeof chart.on(beforeEvent)).to.be('function');
|
||||
expect(typeof chart.on(afterEvent)).to.be('function');
|
||||
});
|
||||
});
|
||||
|
||||
it('should remove the event and all listeners when only event passed an argument', function () {
|
||||
var charts = vis.handler.charts;
|
||||
vis.off(afterEvent);
|
||||
|
||||
// should remove 'brush' from _listeners object
|
||||
expect(vis._listeners[afterEvent]).to.be(undefined);
|
||||
|
||||
// should remove 'brush' from eventTypes.enabled array
|
||||
expect(vis.eventTypes.enabled.length).to.be(1);
|
||||
|
||||
// should remove the event from the charts
|
||||
charts.forEach(function (chart) {
|
||||
expect(typeof chart.on(afterEvent)).to.be('undefined');
|
||||
});
|
||||
});
|
||||
|
||||
it('should remove the event from the eventTypes.enabled array as well as ' +
|
||||
'from the chart when the _listeners array has a length of 0', function () {
|
||||
var charts = vis.handler.charts;
|
||||
vis.off(afterEvent, listener2);
|
||||
|
||||
expect(vis._listeners[afterEvent].length).to.be(0);
|
||||
expect(vis.eventTypes.enabled.length).to.be(1);
|
||||
|
||||
charts.forEach(function (chart) {
|
||||
expect(typeof chart.on(afterEvent)).to.be('undefined');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue