Skip to content

Commit

Permalink
test: added applyMagnet & reorganized tests files; added config check…
Browse files Browse the repository at this point in the history
…ing helpers to tests
  • Loading branch information
artus9033 committed Aug 17, 2024
1 parent 5391539 commit f7223f0
Show file tree
Hide file tree
Showing 10 changed files with 874 additions and 426 deletions.
7 changes: 6 additions & 1 deletion tests/__utils__/testsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ export type UnitTestCategory =
| "pluginRegistration"
| "dragListenersRegistration"
| "calcCartesian"
| "checkDraggingConfiguration";
| "calcRadialLinear"
| "checkDraggingConfiguration"
| "clipValue"
| "applyMagnet"
| "getElement"
| "plugin";

export type UnitConfig = {
whitelistedTestCategories: Whitelist<UnitTestCategory> | undefined;
Expand Down
39 changes: 39 additions & 0 deletions tests/unit/__fixtures__/mockedEventUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { DragDataEvent } from "../../../src";

export type TestEventType = "MouseEvent" | "TouchEvent";

export function getEventX(event: DragDataEvent, eventType: TestEventType) {
return eventType === "MouseEvent"
? (event as MouseEvent).clientX
: (event as TouchEvent).touches[0].clientX;
}

export function getEventY(event: DragDataEvent, eventType: TestEventType) {
return eventType === "MouseEvent"
? (event as MouseEvent).clientY
: (event as TouchEvent).touches[0].clientY;
}

export function setEventX(
event: DragDataEvent,
eventType: TestEventType,
x: number,
) {
if (eventType === "MouseEvent") {
((event as MouseEvent).clientX as number) = x;
} else {
((event as TouchEvent).touches[0].clientX as number) = x;
}
}

export function setEventY(
event: DragDataEvent,
eventType: TestEventType,
y: number,
) {
if (eventType === "MouseEvent") {
((event as MouseEvent).clientY as number) = y;
} else {
((event as TouchEvent).touches[0].clientY as number) = y;
}
}
13 changes: 10 additions & 3 deletions tests/unit/__utils__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,12 @@ export function setupChartInstance<T extends keyof ChartTypeRegistry>(
}: SetupChartInstanceOptions<T> = {},
): TChart<T> {
const canvas = document.createElement("canvas");
canvas.width = canvasWidth;
canvas.height = canvasHeight;
canvas.style.width = `${canvasWidth}px`;
canvas.style.height = `${canvasHeight}px`;

var ctx = canvas.getContext("2d")!;

return new Chart<T>(ctx, {
const chart = new Chart<T>(ctx, {
type: chartType,
data: _.cloneDeep(chartData),
options: _.merge(
Expand All @@ -62,6 +60,15 @@ export function setupChartInstance<T extends keyof ChartTypeRegistry>(
_.cloneDeep(customOptions ?? {}),
) as any,
});

// ensure these properties are initialized for chart.js helpers to be able to work properly
chart.width = canvasWidth;
chart.height = canvasHeight;

canvas.width = canvasWidth;
canvas.height = canvasHeight;

return chart;
}

/**
Expand Down
17 changes: 15 additions & 2 deletions tests/unit/applyMagnet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@ import { Chart, ChartType } from "chart.js";

import { applyMagnet } from "../../dist/test/chartjs-plugin-dragdata-test";
import { OptionalPluginConfiguration } from "../../src";
import { isTestsConfigWhitelistItemAllowed } from "../__utils__/testsConfig";
import { UNIT_TEST_CHART_TYPES } from "./__utils__/constants";
import { setupChartInstance } from "./__utils__/utils";

describe("applyMagnet", () => {
(isTestsConfigWhitelistItemAllowed(
"unit",
"whitelistedTestCategories",
"applyMagnet",
)
? describe
: describe.skip)("applyMagnet", () => {
for (const chartType of UNIT_TEST_CHART_TYPES) {
let chartInstance: Chart;

describe(`${chartType} chart`, () => {
(isTestsConfigWhitelistItemAllowed(
"unit",
"whitelistedTestedChartTypes",
chartType,
)
? describe
: describe.skip)(`${chartType} chart`, () => {
beforeEach(() => {
chartInstance = setupChartInstance(chartType as ChartType, {
plugins: { dragData: {} },
Expand Down
Loading

0 comments on commit f7223f0

Please sign in to comment.