Skip to content

Commit

Permalink
Add <Rect> "click" event test
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed May 3, 2024
1 parent 7760390 commit 1e5408b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 4 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions test/helpers/event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function enableEvents(canvas: any) {
const ev = new EventTarget();
canvas.addEventListener = ev.addEventListener.bind(ev);
canvas.removeEventListener = ev.removeEventListener.bind(ev);
canvas.dispatchEvent = ev.dispatchEvent.bind(ev);
}

export function dispatchEvent(target: any, event: Event) {
target.dispatchEvent(event);
}
52 changes: 51 additions & 1 deletion test/rect.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react';
import React, { useState } from 'react';
import { test, expect } from 'vitest';
import config, { Canvas } from '@napi-rs/canvas';
import { Rect } from '../src';
import { render } from '../src/render';
import { enableEvents, dispatchEvent } from './helpers/event';

test('should render <Rect>', async () => {
const canvas = new Canvas(150, 100);
Expand All @@ -13,3 +14,52 @@ test('should render <Rect>', async () => {
);
expect(canvas.toBuffer('image/png')).toMatchImageSnapshot();
});

test('should receive "click" event', async () => {
const canvas = new Canvas(150, 100);
enableEvents(canvas);
let event: MouseEvent;

function ClickRect() {
const [color, setColor] = useState('red');
return (
<Rect
x={10}
y={10}
width={50}
height={50}
fill={color}
onClick={(e) => {
setColor('blue');
event = e;
}}
/>
);
}

const root = render(<ClickRect />, canvas, config);
expect(root.renderCount).toEqual(0);

// Initial state, Rect is red
await root;
expect(root.renderCount).toEqual(1);
expect(canvas.toBuffer('image/png')).toMatchImageSnapshot();

// Simulate "click" event
dispatchEvent(
canvas,
Object.assign(new Event('click'), {
layerX: 50,
layerY: 40,
}),
);

// Rect should be blue now
await root;
expect(root.renderCount).toEqual(2);
expect(canvas.toBuffer('image/png')).toMatchImageSnapshot();

// Check event values
expect(event!.layerX).toEqual(40);
expect(event!.layerY).toEqual(30);
});
6 changes: 3 additions & 3 deletions test/text.test.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';
import { join } from 'path';
import { test, expect } from 'vitest';
import config, { Canvas } from '@napi-rs/canvas';
import config, { Canvas, GlobalFonts } from '@napi-rs/canvas';
import { Text } from '../src';
import { render } from '../src/render';
import { join } from 'path';

config.GlobalFonts.registerFromPath(
GlobalFonts.registerFromPath(
join(__dirname, 'Geist-Regular.otf'),
'Geist Sans',
);
Expand Down

0 comments on commit 1e5408b

Please sign in to comment.