Skip to content
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

Run event tests on plugin and annotation levels #529

Merged
merged 1 commit into from Dec 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
317 changes: 161 additions & 156 deletions test/events.js
@@ -1,156 +1,161 @@
export function testEvents(options, innerElement) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm wrong or it loos like completely change? Maybe lien separator?

Copy link
Member Author

Choose a reason for hiding this comment

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

It was ndentation level change

const descr = innerElement ? options.type + '.' + innerElement : options.type;
const chartOptions = {
type: 'scatter',
options: {
animation: false,
scales: {
x: {
display: false,
min: 0,
max: 10
},
y: {
display: false,
min: 0,
max: 10
}
},
plugins: {
legend: false,
annotation: {
}
}
},
};

describe('events', function() {

it(`should detect enter and leave events on ${descr}`, function(done) {
const enterSpy = jasmine.createSpy('enter');
const leaveSpy = jasmine.createSpy('leave');

options.enter = enterSpy;
options.leave = leaveSpy;
chartOptions.options.plugins.annotation.annotations = [options];

const chart = window.acquireChart(chartOptions);
const eventPoint = getEventPoint(chart, innerElement);

window.triggerMouseEvent(chart, 'mousemove', eventPoint);
window.afterEvent(chart, 'mousemove', function() {
expect(enterSpy.calls.count()).toBe(1);

window.triggerMouseEvent(chart, 'mousemove', {
x: 0,
y: 0
});

window.afterEvent(chart, 'mousemove', function() {
expect(leaveSpy.calls.count()).toBe(1);
delete options.enter;
delete options.leave;
done();
});
});
});

it(`should detect click event on ${descr}`, function(done) {
const clickSpy = jasmine.createSpy('click');

options.click = clickSpy;
chartOptions.options.plugins.annotation.annotations = [options];

const chart = window.acquireChart(chartOptions);
const eventPoint = getEventPoint(chart, innerElement);

window.afterEvent(chart, 'click', function() {
expect(clickSpy.calls.count()).toBe(1);
delete options.click;
done();
});
window.triggerMouseEvent(chart, 'click', eventPoint);
});

it(`should detect dbl click event on ${descr}`, function(done) {
const dblClickSpy = jasmine.createSpy('dblclick');

options.dblclick = dblClickSpy;
chartOptions.options.plugins.annotation.dblClickSpeed = 1000;
chartOptions.options.plugins.annotation.annotations = [options];

const chart = window.acquireChart(chartOptions);
const eventPoint = getEventPoint(chart, innerElement);

let dblClick = false;
window.afterEvent(chart, 'click', function() {
if (!dblClick) {
dblClick = true;
window.triggerMouseEvent(chart, 'click', eventPoint);
} else {
expect(dblClickSpy.calls.count()).toBe(1);
delete options.dblclick;
done();
}
});
window.triggerMouseEvent(chart, 'click', eventPoint);
});

it(`should detect a click event even if 2 clicks are fired on ${descr}`, function(done) {
const dblClickSpy = jasmine.createSpy('dblclick');

options.dblclick = dblClickSpy;
chartOptions.options.plugins.annotation.dblClickSpeed = 1;
chartOptions.options.plugins.annotation.annotations = [options];

const chart = window.acquireChart(chartOptions);
const eventPoint = getEventPoint(chart, innerElement);

let dblClick = false;
window.afterEvent(chart, 'click', function() {
if (!dblClick) {
dblClick = true;
setTimeout(() => {
window.triggerMouseEvent(chart, 'click', eventPoint);
}, 500);
} else {
expect(dblClickSpy.calls.count()).toBe(0);
delete options.dblclick;
done();
}
});
window.triggerMouseEvent(chart, 'click', eventPoint);
});

if (!innerElement) {
it(`should center point in range on ${descr}`, function() {
chartOptions.options.plugins.annotation.annotations = [options];
const chart = window.acquireChart(chartOptions);
const element = getElement(chart);
const center = element.getCenterPoint();
expect(element.inRange(center.x, center.y)).toBe(true);
});

it(`shouldn't center point plus adjustment in range on ${descr}`, function() {
chartOptions.options.plugins.annotation.annotations = [options];
const chart = window.acquireChart(chartOptions);
const element = getElement(chart);
const center = element.getCenterPoint();
expect(element.inRange(center.x + center.width, center.y)).toBe(false);
});

}
});
}

function getElement(chart) {
const Annotation = window['chartjs-plugin-annotation'];
const state = Annotation._getState(chart);
return state.elements[0];
}

function getEventPoint(chart, innerElement) {
const annotation = getElement(chart);
const element = innerElement ? annotation[innerElement] : annotation;
return innerElement ? element : element.getCenterPoint();
}
export function testEvents(options, innerElement) {
const descr = innerElement ? options.type + '.' + innerElement : options.type;
const chartConfig = {
type: 'scatter',
options: {
animation: false,
scales: {
x: {
display: false,
min: 0,
max: 10
},
y: {
display: false,
min: 0,
max: 10
}
},
plugins: {
legend: false,
annotation: {
}
}
},
};

describe('events', function() {
const pluginOpts = chartConfig.options.plugins.annotation;

[pluginOpts, options].forEach(function(targetOptions) {

it(`should detect enter and leave events on ${descr}`, function(done) {
const enterSpy = jasmine.createSpy('enter');
const leaveSpy = jasmine.createSpy('leave');

targetOptions.enter = enterSpy;
targetOptions.leave = leaveSpy;
pluginOpts.annotations = [options];

const chart = window.acquireChart(chartConfig);
const eventPoint = getEventPoint(chart, innerElement);

window.triggerMouseEvent(chart, 'mousemove', eventPoint);
window.afterEvent(chart, 'mousemove', function() {
expect(enterSpy.calls.count()).toBe(1);

window.triggerMouseEvent(chart, 'mousemove', {
x: 0,
y: 0
});

window.afterEvent(chart, 'mousemove', function() {
expect(leaveSpy.calls.count()).toBe(1);
delete targetOptions.enter;
delete targetOptions.leave;
done();
});
});
});

it(`should detect click event on ${descr}`, function(done) {
const clickSpy = jasmine.createSpy('click');

targetOptions.click = clickSpy;
pluginOpts.annotations = [options];

const chart = window.acquireChart(chartConfig);
const eventPoint = getEventPoint(chart, innerElement);

window.afterEvent(chart, 'click', function() {
expect(clickSpy.calls.count()).toBe(1);
delete targetOptions.click;
done();
});
window.triggerMouseEvent(chart, 'click', eventPoint);
});

it(`should detect dbl click event on ${descr}`, function(done) {
const dblClickSpy = jasmine.createSpy('dblclick');

targetOptions.dblclick = dblClickSpy;
pluginOpts.dblClickSpeed = 1000;
pluginOpts.annotations = [options];

const chart = window.acquireChart(chartConfig);
const eventPoint = getEventPoint(chart, innerElement);

let dblClick = false;
window.afterEvent(chart, 'click', function() {
if (!dblClick) {
dblClick = true;
window.triggerMouseEvent(chart, 'click', eventPoint);
} else {
expect(dblClickSpy.calls.count()).toBe(1);
delete targetOptions.dblclick;
delete pluginOpts.dblClickSpeed;
done();
}
});
window.triggerMouseEvent(chart, 'click', eventPoint);
});

it(`should detect a click event even if 2 clicks are fired on ${descr}`, function(done) {
const dblClickSpy = jasmine.createSpy('dblclick');

targetOptions.dblclick = dblClickSpy;
pluginOpts.dblClickSpeed = 1;
pluginOpts.annotations = [options];

const chart = window.acquireChart(chartConfig);
const eventPoint = getEventPoint(chart, innerElement);

let dblClick = false;
window.afterEvent(chart, 'click', function() {
if (!dblClick) {
dblClick = true;
setTimeout(() => {
window.triggerMouseEvent(chart, 'click', eventPoint);
}, 50);
} else {
expect(dblClickSpy.calls.count()).toBe(0);
delete targetOptions.dblclick;
delete pluginOpts.dblClickSpeed;
done();
}
});
window.triggerMouseEvent(chart, 'click', eventPoint);
});

if (!innerElement) {
it(`should center point in range on ${descr}`, function() {
pluginOpts.annotations = [options];
const chart = window.acquireChart(chartConfig);
const element = getElement(chart);
const center = element.getCenterPoint();
expect(element.inRange(center.x, center.y)).toBe(true);
});

it(`shouldn't center point plus adjustment in range on ${descr}`, function() {
pluginOpts.annotations = [options];
const chart = window.acquireChart(chartConfig);
const element = getElement(chart);
const center = element.getCenterPoint();
expect(element.inRange(center.x + center.width, center.y)).toBe(false);
});
}
});
});
}

function getElement(chart) {
const Annotation = window['chartjs-plugin-annotation'];
const state = Annotation._getState(chart);
return state.elements[0];
}

function getEventPoint(chart, innerElement) {
const annotation = getElement(chart);
const element = innerElement ? annotation[innerElement] : annotation;
return innerElement ? element : element.getCenterPoint();
}