Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions test/specs/ellipse.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('Ellipse annotation', function() {
y: center.y + Math.sin(rad) * (yRadius + halfBorder)
}, center, rotation / 180 * Math.PI);

expect(element.inRange(x, y)).withContext(`in, rotation: ${rotation}, angle: ${angle}, borderWidth: ${borderWidth}, {x: ${x.toFixed(1)}, y: ${y.toFixed(1)}}`).toEqual(true);
expect(element.inRange(x, y)).withContext(`rotation: ${rotation}, angle: ${angle}, borderWidth: ${borderWidth}, {x: ${x.toFixed(1)}, y: ${y.toFixed(1)}}`).toEqual(true);
}
}
});
Expand All @@ -52,7 +52,7 @@ describe('Ellipse annotation', function() {
y: center.y + Math.sin(rad) * (yRadius + halfBorder + 1)
}, center, rotation / 180 * Math.PI);

expect(element.inRange(x, y)).withContext(`in, rotation: ${rotation}, angle: ${angle}, borderWidth: ${borderWidth}, {x: ${x.toFixed(1)}, y: ${y.toFixed(1)}}`).toEqual(false);
expect(element.inRange(x, y)).withContext(`rotation: ${rotation}, angle: ${angle}, borderWidth: ${borderWidth}, {x: ${x.toFixed(1)}, y: ${y.toFixed(1)}}`).toEqual(false);
}
}
});
Expand Down
141 changes: 63 additions & 78 deletions test/specs/point.spec.js
Original file line number Diff line number Diff line change
@@ -1,92 +1,77 @@
describe('Point annotation', function() {
describe('auto', jasmine.fixtures('point'));

const eventIn = function(xScale, yScale, element) {
const options = element.options;
const adjust = options.borderWidth / 2 - 1;
return {x: element.x, y: element.y - element.height / 2 - adjust};
};
const eventOut = function(xScale, yScale, element) {
const options = element.options;
const adjust = options.borderWidth / 2 + 1;
return {x: element.x, y: element.y - element.height / 2 - adjust};
};
describe('inRange', function() {
const annotation1 = {
type: 'point',
xValue: 7,
yValue: 7,
radius: 30
};
const annotation2 = {
type: 'point',
xValue: 3,
yValue: 3,
radius: 5
};
const annotation3 = {
type: 'point',
xValue: 5,
yValue: 5,
radius: 0
};

window.testEvents({
type: 'point',
id: 'test',
xValue: 5,
yValue: 5,
radius: 30,
borderWidth: 12
}, eventIn, eventOut);
const chart = window.scatter10x10({annotation1, annotation2, annotation3});
const elems = window.getAnnotationElements(chart).filter(el => el.options.radius > 0);
const elemsNoRad = window.getAnnotationElements(chart).filter(el => el.options.radius === 0);

describe('events, removing borderWidth by callback, ', function() {
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: {
annotations: {
point: {
type: 'point',
xValue: 5,
yValue: 5,
radius: 20,
borderWidth: 10
}
}
elems.forEach(function(element) {
it(`should return true inside element '${element.options.id}'`, function() {
for (const borderWidth of [0, 10]) {
const halfBorder = borderWidth / 2;
element.options.borderWidth = borderWidth;
const radius = element.height / 2;
for (const angle of [0, 45, 90, 135, 180, 225, 270, 315]) {
const rad = angle * (Math.PI / 180);
const {x, y} = {
x: element.x + Math.cos(rad) * (radius + halfBorder - 1),
y: element.y + Math.sin(rad) * (radius + halfBorder - 1)
};
expect(element.inRange(x, y)).withContext(`angle: ${angle}, radius: ${radius}, borderWidth: ${borderWidth}, {x: ${x.toFixed(1)}, y: ${y.toFixed(1)}}`).toEqual(true);
}
}
},
};

const pointOpts = chartConfig.options.plugins.annotation.annotations.point;

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

pointOpts.click = function(ctx) {
if (ctx.element.options.borderWidth) {
delete ctx.element.options.borderWidth;
ctx.chart.draw();
} else {
ctx.element.options.click = clickSpy;
it(`should return false outside element '${element.options.id}'`, function() {
for (const borderWidth of [0, 10]) {
const halfBorder = borderWidth / 2;
element.options.borderWidth = borderWidth;
const radius = element.height / 2;
for (const angle of [0, 45, 90, 135, 180, 225, 270, 315]) {
const rad = angle * (Math.PI / 180);
const {x, y} = {
x: element.x + Math.cos(rad) * (radius + halfBorder + 1),
y: element.y + Math.sin(rad) * (radius + halfBorder + 1)
};
expect(element.inRange(x, y)).withContext(`angle: ${angle}, radius: ${radius}, borderWidth: ${borderWidth}, {x: ${x.toFixed(1)}, y: ${y.toFixed(1)}}`).toEqual(false);
}
}
};

const chart = window.acquireChart(chartConfig);
const xScale = chart.scales.x;
const yScale = chart.scales.y;
const eventPoint = {x: xScale.getPixelForValue(5), y: yScale.getPixelForValue(5)};

window.afterEvent(chart, 'click', function() {
expect(clickSpy.calls.count()).toBe(0);

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

elemsNoRad.forEach(function(element) {
it(`should return false radius is 0 element '${element.options.id}'`, function() {
for (const borderWidth of [0, 10]) {
const halfBorder = borderWidth / 2;
element.options.borderWidth = borderWidth;
for (const x of [element.x - halfBorder, element.x + halfBorder]) {
expect(element.inRange(x, element.y)).toEqual(false);
}
for (const y of [element.y - halfBorder, element.y + halfBorder]) {
expect(element.inRange(element.x, y)).toEqual(false);
}
}
});
});
});

});