Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jassmith committed Oct 3, 2023
1 parent 2a7a3c7 commit 2b2cff1
Showing 1 changed file with 153 additions and 1 deletion.
154 changes: 153 additions & 1 deletion packages/core/test/data-grid-lib.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* 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 { remapForDnDState, type MappedGridColumn, drawImage, drawWithLastUpdate } from "../src/data-grid/data-grid-lib";
import { GridCellKind, type ImageWindowLoader, type Rectangle } from "../src/data-grid/data-grid-types";

describe("remapForDnDState", () => {
Expand Down Expand Up @@ -265,3 +265,155 @@ describe("drawImage", () => {
expect(mockCtx.drawImage).not.toHaveBeenCalled();
});
});

describe("drawWithLastUpdate", () => {
const mockCtx: jest.Mocked<CanvasRenderingContext2D> = {} as any;
let mockTheme: Theme;
let mockRect: Rectangle;
let mockDraw: jest.Mock;

beforeEach(() => {
mockCtx.fillRect = jest.fn();
mockCtx.fillStyle = "";
mockCtx.globalAlpha = 1;

mockTheme = {
...getDataEditorTheme(),
bgSearchResult: "some-color",
};

mockRect = {
x: 10,
y: 20,
width: 50,
height: 60,
};

mockDraw = jest.fn();
});

it("should do nothing if lastUpdate is undefined", () => {
const result = drawWithLastUpdate(
{
ctx: mockCtx,
theme: mockTheme,
rect: mockRect,
cell: { kind: GridCellKind.Text, allowOverlay: false, data: "Test", displayData: "Test" },
col: 0,
row: 0,
highlighted: false,
hoverAmount: 0,
hoverX: undefined,
hoverY: undefined,
hyperWrapping: false,
requestAnimationFrame: jest.fn(),
imageLoader: {} as any,
spriteManager: {} as any,
},
undefined,
1000,
undefined,
mockDraw
);

expect(mockCtx.fillStyle).toBe("");
expect(mockDraw).toHaveBeenCalled();
expect(result).toBe(false);
});

it("should not animate if progress is >= animTime", () => {
const lastUpdate = 400;
const frameTime = 1000;

const result = drawWithLastUpdate(
{
ctx: mockCtx,
theme: mockTheme,
rect: mockRect,
cell: { kind: GridCellKind.Text, allowOverlay: false, data: "Test", displayData: "Test" },
col: 0,
row: 0,
highlighted: false,
hoverAmount: 0,
hoverX: undefined,
hoverY: undefined,
hyperWrapping: false,
requestAnimationFrame: jest.fn(),
imageLoader: {} as any,
spriteManager: {} as any,
},
lastUpdate,
frameTime,
undefined,
mockDraw
);

expect(mockCtx.fillStyle).toBe("");
expect(mockDraw).toHaveBeenCalled();
expect(result).toBe(false);
});

it("should animate if progress is < animTime", () => {
const lastUpdate = 600;
const frameTime = 1000;

const result = drawWithLastUpdate(
{
ctx: mockCtx,
theme: mockTheme,
rect: mockRect,
cell: { kind: GridCellKind.Text, allowOverlay: false, data: "Test", displayData: "Test" },
col: 0,
row: 0,
highlighted: false,
hoverAmount: 0,
hoverX: undefined,
hoverY: undefined,
hyperWrapping: false,
requestAnimationFrame: jest.fn(),
imageLoader: {} as any,
spriteManager: {} as any,
},
lastUpdate,
frameTime,
undefined,
mockDraw
);

expect(mockCtx.fillStyle).toBe(mockTheme.bgSearchResult);
expect(mockCtx.fillRect).toHaveBeenCalledWith(mockRect.x, mockRect.y, mockRect.width, mockRect.height);
expect(mockDraw).toHaveBeenCalled();
expect(result).toBe(true);
});

it("should update lastPrep's fillStyle if defined", () => {
const lastUpdate = 600;
const frameTime = 1000;
const mockLastPrep = { fillStyle: "", deprep: jest.fn(), font: "some-font", renderer: {} };

drawWithLastUpdate(
{
ctx: mockCtx,
theme: mockTheme,
rect: mockRect,
cell: { kind: GridCellKind.Text, allowOverlay: false, data: "Test", displayData: "Test" },
col: 0,
row: 0,
highlighted: false,
hoverAmount: 0,
hoverX: undefined,
hoverY: undefined,
hyperWrapping: false,
requestAnimationFrame: jest.fn(),
imageLoader: {} as any,
spriteManager: {} as any,
},
lastUpdate,
frameTime,
mockLastPrep,
mockDraw
);

expect(mockLastPrep.fillStyle).toBe(mockTheme.bgSearchResult);
});
});

0 comments on commit 2b2cff1

Please sign in to comment.