mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 01:13:23 -04:00
[vislib/tests] update to use Event Emitter inspection api
This commit is contained in:
parent
d75e720bc6
commit
f78f9c769c
3 changed files with 117 additions and 118 deletions
|
@ -2,6 +2,7 @@ define(function (require) {
|
|||
var angular = require('angular');
|
||||
var _ = require('lodash');
|
||||
var $ = require('jquery');
|
||||
var d3 = require('d3');
|
||||
|
||||
// Data
|
||||
var data = require('vislib_fixtures/mock_data/date_histogram/_series');
|
||||
|
@ -15,87 +16,96 @@ define(function (require) {
|
|||
vis = null;
|
||||
}
|
||||
|
||||
function getEls(n, type) {
|
||||
return d3.select().data(new Array(n)).enter().append(type);
|
||||
}
|
||||
|
||||
describe('', function () {
|
||||
var vis;
|
||||
var SimpleEmitter;
|
||||
|
||||
beforeEach(module('AreaChartFactory'));
|
||||
beforeEach(inject(function (Private) {
|
||||
vis = Private(require('vislib_fixtures/_vis_fixture'))();
|
||||
vis.render(data);
|
||||
SimpleEmitter = require('utils/SimpleEmitter');
|
||||
}));
|
||||
|
||||
afterEach(function () {
|
||||
destroyVis(vis);
|
||||
});
|
||||
|
||||
it('extends the SimpleEmitter class', function () {
|
||||
var events = _.pluck(vis.handler.charts, 'events');
|
||||
expect(events.length).to.be.above(0);
|
||||
events.forEach(function (dispatch) {
|
||||
expect(dispatch).to.be.a(SimpleEmitter);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Stock event handlers', function () {
|
||||
var vis;
|
||||
|
||||
beforeEach(function () {
|
||||
module('AreaChartFactory');
|
||||
});
|
||||
beforeEach(module('AreaChartFactory'));
|
||||
beforeEach(inject(function (Private) {
|
||||
vis = Private(require('vislib_fixtures/_vis_fixture'))();
|
||||
require('css!components/vislib/styles/main');
|
||||
|
||||
beforeEach(function () {
|
||||
inject(function (Private) {
|
||||
vis = Private(require('vislib_fixtures/_vis_fixture'))();
|
||||
require('css!components/vislib/styles/main');
|
||||
vis.on('brush', _.noop);
|
||||
|
||||
vis.on('brush', _.noop);
|
||||
|
||||
vis.render(data);
|
||||
});
|
||||
});
|
||||
vis.render(data);
|
||||
}));
|
||||
|
||||
afterEach(function () {
|
||||
destroyVis(vis);
|
||||
});
|
||||
|
||||
describe('addEvent method', function () {
|
||||
it('should return a function', function () {
|
||||
vis.handler.charts.forEach(function (chart) {
|
||||
var addEvent = chart.events.addEvent;
|
||||
expect(_.isFunction(addEvent('click', _.noop))).to.be(true);
|
||||
it('returns a function that binds the passed event to a selection', function () {
|
||||
var chart = _.first(vis.handler.charts);
|
||||
var apply = chart.events.addEvent('event', _.noop);
|
||||
expect(apply).to.be.a('function');
|
||||
|
||||
var els = getEls(3, 'div');
|
||||
apply(els);
|
||||
els.each(function () {
|
||||
expect(d3.select(this).on('event')).to.be(_.noop);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('addHoverEvent method', function () {
|
||||
it('should return a function', function () {
|
||||
vis.handler.charts.forEach(function (chart) {
|
||||
var hover = chart.events.addHoverEvent;
|
||||
// test the addHoverEvent, addClickEvent, addBrushEvent methods by
|
||||
// checking that they return function which bind the events expected
|
||||
function checkBoundAddMethod(name, event) {
|
||||
describe(name + ' method', function () {
|
||||
it('should be a function', function () {
|
||||
vis.handler.charts.forEach(function (chart) {
|
||||
expect(chart.events[name]).to.be.a('function');
|
||||
});
|
||||
});
|
||||
|
||||
expect(_.isFunction(hover)).to.be(true);
|
||||
it('returns a function that binds ' + event + ' events to a selection', function () {
|
||||
var chart = _.first(vis.handler.charts);
|
||||
var apply = chart.events[name](d3.select(document.createElement('svg')));
|
||||
expect(apply).to.be.a('function');
|
||||
|
||||
var els = getEls(3, 'div');
|
||||
apply(els);
|
||||
els.each(function () {
|
||||
expect(d3.select(this).on(event)).to.be.a('function');
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
it('should attach a hover event', function () {
|
||||
vis.handler.charts.forEach(function (chart) {
|
||||
expect(chart.events.listenerCount('hover')).to.be.above(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('addClickEvent method', function () {
|
||||
it('should return a function', function () {
|
||||
vis.handler.charts.forEach(function (chart) {
|
||||
var click = chart.events.addClickEvent;
|
||||
|
||||
expect(_.isFunction(click)).to.be(true);
|
||||
});
|
||||
});
|
||||
|
||||
it('should attach a click event', function () {
|
||||
vis.handler.charts.forEach(function (chart) {
|
||||
expect(chart.events.listenerCount('click')).to.be.above(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('addBrushEvent method', function () {
|
||||
it('should return a function', function () {
|
||||
vis.handler.charts.forEach(function (chart) {
|
||||
var brush = chart.events.addBrushEvent;
|
||||
|
||||
expect(_.isFunction(brush)).to.be(true);
|
||||
});
|
||||
});
|
||||
|
||||
it('should attach a brush event', function () {
|
||||
vis.handler.charts.forEach(function (chart) {
|
||||
expect(chart.events.listenerCount('brush')).to.be.above(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
checkBoundAddMethod('addHoverEvent', 'mouseover');
|
||||
checkBoundAddMethod('addMouseoutEvent', 'mouseout');
|
||||
checkBoundAddMethod('addClickEvent', 'click');
|
||||
checkBoundAddMethod('addBrushEvent', 'mousedown');
|
||||
|
||||
describe('addMousePointer method', function () {
|
||||
it('should return a function', function () {
|
||||
it('should be a function', function () {
|
||||
vis.handler.charts.forEach(function (chart) {
|
||||
var pointer = chart.events.addMousePointer;
|
||||
|
||||
|
@ -116,13 +126,30 @@ define(function (require) {
|
|||
vis.render(data);
|
||||
|
||||
vis.handler.charts.forEach(function (chart) {
|
||||
expect(chart.events.listenerCount('someEvent')).to.be.above(0);
|
||||
expect(chart.events.listenerCount('someEvent')).to.be(1);
|
||||
});
|
||||
|
||||
destroyVis(vis);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can be added after rendering', function () {
|
||||
var vis;
|
||||
var chart;
|
||||
module('AreaChartFactory');
|
||||
inject(function (Private) {
|
||||
vis = Private(require('vislib_fixtures/_vis_fixture'))();
|
||||
vis.render(data);
|
||||
vis.on('someEvent', _.noop);
|
||||
|
||||
vis.handler.charts.forEach(function (chart) {
|
||||
expect(chart.events.listenerCount('someEvent')).to.be(1);
|
||||
});
|
||||
|
||||
destroyVis(vis);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -77,7 +77,7 @@ define(function (require) {
|
|||
it('should add events to chart and emit to the Events class', function () {
|
||||
charts.forEach(function (chart) {
|
||||
events.forEach(function (event) {
|
||||
expect(typeof chart.on(event)).to.be('function');
|
||||
expect(chart.events.listenerCount(event)).to.be.above(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -99,7 +99,7 @@ define(function (require) {
|
|||
it('should remove events from the chart', function () {
|
||||
charts.forEach(function (chart) {
|
||||
events.forEach(function (event) {
|
||||
expect(typeof chart.on(event)).to.be('undefined');
|
||||
expect(chart.events.listenerCount(event)).to.be(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -138,11 +138,10 @@ define(function (require) {
|
|||
var listener2;
|
||||
|
||||
beforeEach(function () {
|
||||
listeners = [];
|
||||
listener1 = function () {};
|
||||
listener2 = function () {};
|
||||
listeners.push(listener1);
|
||||
listeners.push(listener2);
|
||||
listeners = [
|
||||
listener1 = function () {},
|
||||
listener2 = function () {}
|
||||
];
|
||||
|
||||
// Add event and listeners to chart
|
||||
listeners.forEach(function (listener) {
|
||||
|
@ -163,35 +162,22 @@ define(function (require) {
|
|||
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]);
|
||||
it('should add an event and its listeners', function () {
|
||||
listeners.forEach(function (listener) {
|
||||
expect(vis.listeners(beforeEvent)).to.contain(listener);
|
||||
});
|
||||
|
||||
vis._listeners[afterEvent].forEach(function (listener, i) {
|
||||
expect(typeof listener.handler).to.be('function');
|
||||
expect(listener.handler).to.be(listeners[i]);
|
||||
listeners.forEach(function (listener) {
|
||||
expect(vis.listeners(afterEvent)).to.contain(listener);
|
||||
});
|
||||
});
|
||||
|
||||
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 () {
|
||||
it('should cause a listener for each event to be attached to each 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]);
|
||||
expect(chart.events.listenerCount(beforeEvent)).to.be.above(0);
|
||||
expect(chart.events.listenerCount(afterEvent)).to.be.above(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -233,30 +219,19 @@ define(function (require) {
|
|||
vis.off(afterEvent);
|
||||
});
|
||||
|
||||
it('should remove a listener from the _listeners[event] array', function () {
|
||||
it('should remove a listener', function () {
|
||||
var charts = vis.handler.charts;
|
||||
|
||||
expect(vis._listeners[beforeEvent].length).to.be(1);
|
||||
expect(vis._listeners[afterEvent].length).to.be(1);
|
||||
expect(vis.listeners(beforeEvent)).to.not.contain(listener1);
|
||||
expect(vis.listeners(beforeEvent)).to.contain(listener2);
|
||||
|
||||
// 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);
|
||||
});
|
||||
expect(vis.listeners(afterEvent)).to.not.contain(listener1);
|
||||
expect(vis.listeners(afterEvent)).to.contain(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');
|
||||
expect(chart.events.listenerCount(beforeEvent)).to.be.above(0);
|
||||
expect(chart.events.listenerCount(afterEvent)).to.be.above(0);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -264,28 +239,25 @@ define(function (require) {
|
|||
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 'brush' event
|
||||
expect(vis.listeners(beforeEvent)).to.contain(listener2);
|
||||
expect(vis.listeners(afterEvent)).to.not.contain(listener2);
|
||||
|
||||
// should remove the event from the charts
|
||||
charts.forEach(function (chart) {
|
||||
expect(typeof chart.on(afterEvent)).to.be('undefined');
|
||||
expect(chart.events.listenerCount(beforeEvent)).to.be.above(0);
|
||||
expect(chart.events.listenerCount(afterEvent)).to.be(0);
|
||||
});
|
||||
});
|
||||
|
||||
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 () {
|
||||
it('should remove the event from the chart when the last listener is removed', 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);
|
||||
expect(vis.listenerCount(afterEvent)).to.be(0);
|
||||
|
||||
charts.forEach(function (chart) {
|
||||
expect(typeof chart.on(afterEvent)).to.be('undefined');
|
||||
expect(chart.events.listenerCount(afterEvent)).to.be(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue