Skip to content

Commit

Permalink
More missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jassmith committed Sep 29, 2023
1 parent 532de91 commit fb8f084
Showing 1 changed file with 222 additions and 1 deletion.
223 changes: 222 additions & 1 deletion packages/core/test/data-grid-lib.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { remapForDnDState, type MappedGridColumn } from "../src/data-grid/data-grid-lib";
/* eslint-disable sonarjs/no-duplicate-string */
import type { BaseDrawArgs } from "../src";
import { getDataEditorTheme, type Theme } from "../src/common/styles";
import { remapForDnDState, type MappedGridColumn, drawImage } from "../src/data-grid/data-grid-lib";
import { GridCellKind, type ImageWindowLoader, type Rectangle } from "../src/data-grid/data-grid-types";

describe("remapForDnDState", () => {
const sampleColumns: MappedGridColumn[] = [
Expand Down Expand Up @@ -44,3 +48,220 @@ describe("remapForDnDState", () => {
}
});
});

describe("drawImage", () => {
let mockCtx: jest.Mocked<CanvasRenderingContext2D>;
let mockImageLoader: jest.Mocked<ImageWindowLoader>;
let mockTheme: Theme;
let mockRect: Rectangle;

beforeEach(() => {
// Initialize your mocks here
mockCtx = {
drawImage: jest.fn(),
moveTo: jest.fn(),
arcTo: jest.fn(),
save: jest.fn(),
restore: jest.fn(),
clip: jest.fn(),
} as any;

mockImageLoader = {
loadOrGetImage: jest.fn((_a, _b, _c) => new HTMLImageElement()),
setCallback: jest.fn(),
setWindow: jest.fn(),
};

mockTheme = getDataEditorTheme();

mockRect = {
x: 0,
y: 0,
width: 100,
height: 56,
};
});

test("renders with basic input", () => {
const baseDrawArgs: BaseDrawArgs = {
ctx: mockCtx,
theme: mockTheme,
col: 0,
row: 0,
rect: mockRect,
imageLoader: mockImageLoader,
cell: {
kind: GridCellKind.Image as const,
allowAdd: false,
allowOverlay: false,
data: ["sample-url"],
},
highlighted: false,
hoverAmount: 0,
hoverX: undefined,
hoverY: undefined,
hyperWrapping: false,
requestAnimationFrame: jest.fn(),
spriteManager: {} as any,
};

const mockImage = new Image();
mockImage.width = 50;
mockImage.height = 50;
mockImageLoader.loadOrGetImage.mockReturnValueOnce(mockImage);

drawImage(baseDrawArgs, ["sample-url"]);

expect(mockCtx.drawImage).toHaveBeenCalledWith(
mockImage,
expect.any(Number),
expect.any(Number),
expect.any(Number),
expect.any(Number)
);
});

test("renders with rounding", () => {
const baseDrawArgs = {
ctx: mockCtx,
theme: mockTheme,
col: 0,
row: 0,
rect: mockRect,
imageLoader: mockImageLoader,
cell: {
kind: GridCellKind.Image as const,
allowAdd: false,
allowOverlay: false,
data: ["sample-url"],
},
highlighted: false,
hoverAmount: 0,
hoverX: undefined,
hoverY: undefined,
hyperWrapping: false,
requestAnimationFrame: jest.fn(),
spriteManager: {} as any,
};

const mockImage = new Image();
mockImage.width = 50;
mockImage.height = 50;
mockImageLoader.loadOrGetImage.mockReturnValueOnce(mockImage);

drawImage(baseDrawArgs, ["sample-url"], 5);

expect(mockCtx.save).toHaveBeenCalled();
expect(mockCtx.clip).toHaveBeenCalled();
expect(mockCtx.restore).toHaveBeenCalled();
expect(mockCtx.arcTo).toHaveBeenCalled();
});

test("renders aligned to the right", () => {
const baseDrawArgs = {
ctx: mockCtx,
theme: mockTheme,
col: 0,
row: 0,
rect: mockRect,
imageLoader: mockImageLoader,
cell: {
kind: GridCellKind.Image as const,
allowAdd: false,
allowOverlay: false,
data: ["sample-url"],
},
highlighted: false,
hoverAmount: 0,
hoverX: undefined,
hoverY: undefined,
hyperWrapping: false,
requestAnimationFrame: jest.fn(),
spriteManager: {} as any,
};

const mockImage = new Image();
mockImage.width = 50;
mockImage.height = 50;
mockImageLoader.loadOrGetImage.mockReturnValueOnce(mockImage);

drawImage(baseDrawArgs, ["sample-url"], 0, "right");

const expectedX = mockRect.x + mockRect.width - mockTheme.cellHorizontalPadding - mockImage.width;
expect(mockCtx.drawImage).toHaveBeenCalledWith(
mockImage,
expectedX,
expect.any(Number),
expect.any(Number),
expect.any(Number)
);
});

test("renders aligned to the center", () => {
const baseDrawArgs = {
ctx: mockCtx,
theme: mockTheme,
col: 0,
row: 0,
rect: mockRect,
imageLoader: mockImageLoader,
cell: {
kind: GridCellKind.Image as const,
allowAdd: false,
allowOverlay: false,
data: ["sample-url"],
},
highlighted: false,
hoverAmount: 0,
hoverX: undefined,
hoverY: undefined,
hyperWrapping: false,
requestAnimationFrame: jest.fn(),
spriteManager: {} as any,
};

const mockImage = new Image();
mockImage.width = 50;
mockImage.height = 50;
mockImageLoader.loadOrGetImage.mockReturnValueOnce(mockImage);

drawImage(baseDrawArgs, ["sample-url"], 0, "center");

const expectedX = mockRect.x + mockRect.width / 2 - mockImage.width / 2;
expect(mockCtx.drawImage).toHaveBeenCalledWith(
mockImage,
expectedX,
expect.any(Number),
expect.any(Number),
expect.any(Number)
);
});

test("does not render with invalid data", () => {
const baseDrawArgs = {
ctx: mockCtx,
theme: mockTheme,
col: 0,
row: 0,
rect: mockRect,
imageLoader: mockImageLoader,
cell: {
kind: GridCellKind.Image as const,
allowAdd: false,
allowOverlay: false,
data: ["sample-url"],
},
highlighted: false,
hoverAmount: 0,
hoverX: undefined,
hoverY: undefined,
hyperWrapping: false,
requestAnimationFrame: jest.fn(),
spriteManager: {} as any,
};

drawImage(baseDrawArgs, [""]);

expect(mockCtx.drawImage).not.toHaveBeenCalled();
});
});

0 comments on commit fb8f084

Please sign in to comment.