Skip to content

Commit

Permalink
AG-13306 Fix unit-testing testFindTarget function
Browse files Browse the repository at this point in the history
  • Loading branch information
olegat committed Nov 14, 2024
1 parent 716d89f commit 3f6ac1b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
12 changes: 12 additions & 0 deletions packages/ag-charts-community/src/chart/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1533,4 +1533,16 @@ export abstract class Chart extends Observable {
}
}
}

public testFindTarget(x: number, y: number): { target: HTMLElement; x: number; y: number } {
type TestModuleFns = { testFindTarget: Chart['testFindTarget'] };
for (const moduleName of ['legend', 'navigator', 'zoom']) {
const mod = this.modulesManager.getModule<TestModuleFns>(moduleName);
const modTarget = mod?.testFindTarget(x, y);
if (modTarget) {
return modTarget;
}
}
return this.seriesAreaManager.testFindTarget(x, y);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -578,4 +578,11 @@ export class SeriesAreaManager extends BaseManager {
this.update(ChartUpdateType.SERIES_UPDATE, { seriesToUpdate });
}
}

public testFindTarget(x: number, y: number): { target: HTMLElement; x: number; y: number } {
if (this.seriesRect?.containsPoint(x, y)) {
return { target: this.chart.ctx.scene.canvas.element, x: x - this.seriesRect.x, y: y - this.seriesRect.y };
}
return { target: this.chart.ctx.scene.canvas.element, x, y };
}
}
29 changes: 7 additions & 22 deletions packages/ag-charts-community/src/chart/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,24 +301,10 @@ const checkTargetValid = (target: HTMLElement) => {
if (!target.isConnected) throw new Error('Chart must be configured with a container for event testing to work');
};

type TestTarget = { target: HTMLElement; x: number; y: number };
type TestModuleFns = { testFindTarget: (canvasX: number, canvasY: number) => TestTarget | undefined };

function findTarget(chart: Chart, canvasX: number, canvasY: number): { target: HTMLElement; x: number; y: number } {
for (const moduleName of ['legend', 'navigator', 'zoom']) {
const mod = chart.modulesManager.getModule<TestModuleFns>(moduleName);
const modTarget = mod?.testFindTarget(canvasX, canvasY);
if (modTarget) {
return modTarget;
}
}
return { target: chart.ctx.scene.canvas.element, x: canvasX, y: canvasY };
}

export function hoverAction(canvasX: number, canvasY: number): (chart: ChartOrProxy) => Promise<void> {
return async (chartOrProxy) => {
const chart = deproxy(chartOrProxy);
const { target, x, y } = findTarget(chart, canvasX, canvasY);
const { target, x, y } = chart.testFindTarget(canvasX, canvasY);
checkTargetValid(target);

target?.dispatchEvent(mouseMoveEvent({ offsetX: x, offsetY: y }));
Expand All @@ -333,8 +319,7 @@ export function clickAction(
): (chart: ChartOrProxy) => Promise<void> {
return async (chartOrProxy) => {
const chart = deproxy(chartOrProxy);
const { target, x, y } = findTarget(
chart,
const { target, x, y } = chart.testFindTarget(
opts?.mousedown?.offsetX ?? canvasX,
opts?.mousedown?.offsetY ?? canvasY
);
Expand All @@ -351,7 +336,7 @@ export function clickAction(
export function doubleClickAction(canvasX: number, canvasY: number): (chart: ChartOrProxy) => Promise<void> {
return async (chartOrProxy) => {
const chart = deproxy(chartOrProxy);
const { target, x, y } = findTarget(chart, canvasX, canvasY);
const { target, x, y } = chart.testFindTarget(canvasX, canvasY);
const offsets = { offsetX: x, offsetY: y };
// A double click is always preceded by two single clicks, simulate here to ensure correct handling
target?.dispatchEvent(mouseDownEvent(offsets));
Expand All @@ -370,7 +355,7 @@ export function doubleClickAction(canvasX: number, canvasY: number): (chart: Cha
export function contextMenuAction(canvasX: number, canvasY: number): (chart: ChartOrProxy) => Promise<void> {
return async (chartOrProxy) => {
const chart = deproxy(chartOrProxy);
const { target, x, y } = findTarget(chart, canvasX, canvasY);
const { target, x, y } = chart.testFindTarget(canvasX, canvasY);
checkTargetValid(target);

const offsets = { offsetX: x, offsetY: y };
Expand All @@ -385,8 +370,8 @@ export function dragAction(
): (chart: ChartOrProxy) => Promise<void> {
return async (chartOrProxy) => {
const chart = deproxy(chartOrProxy);
const fromTarget = findTarget(chart, from.x, from.y);
const toTarget = findTarget(chart, to.x, to.y);
const fromTarget = chart.testFindTarget(from.x, from.y);
const toTarget = chart.testFindTarget(to.x, to.y);
const fromOffsets = {
offsetX: fromTarget.x,
offsetY: fromTarget.y,
Expand Down Expand Up @@ -420,7 +405,7 @@ export function scrollAction(
): (chart: ChartOrProxy) => Promise<void> {
return async (chartOrProxy) => {
const chart = deproxy(chartOrProxy);
const { target, x, y } = findTarget(chart, canvasX, canvasY);
const { target, x, y } = chart.testFindTarget(canvasX, canvasY);
target?.dispatchEvent(wheelEvent({ clientX: x, clientY: y, deltaY, deltaX, deltaMode }));
await delay(50);
};
Expand Down

0 comments on commit 3f6ac1b

Please sign in to comment.