Skip to content

Commit

Permalink
Test font helper
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Nov 15, 2024
1 parent 8e5dfc0 commit 7469b44
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 50 deletions.
7 changes: 7 additions & 0 deletions test/font.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { join } from 'path';
import { GlobalFonts } from '@napi-rs/canvas';

GlobalFonts.registerFromPath(
join(__dirname, 'Geist-Regular.otf'),
'Geist Sans',
);
46 changes: 3 additions & 43 deletions test/group.test.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import './font';
import React from 'react';
import { join } from 'path';
import { test, expect } from 'vitest';
import config, { Canvas, GlobalFonts } from '@napi-rs/canvas';
import { Group, Rect, Text, useDimensions, LayoutContext } from '../src';
import config, { Canvas } from '@napi-rs/canvas';
import { Group, Rect, Text, useDimensions } from '../src';
import { render } from '../src/render';

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

test('should render <Group>', async () => {
const canvas = new Canvas(300, 100);
let dims: { width: number; height: number };
Expand Down Expand Up @@ -42,38 +37,3 @@ test('should render <Group>', async () => {
});
expect(canvas.toBuffer('image/png')).toMatchImageSnapshot();
});

test('should render <Group> with parent layout context', async () => {
const canvas = new Canvas(300, 100);
let dims: { width: number; height: number };
function Inner() {
dims = useDimensions();
return (
<>
<Rect
width={dims.width}
height={dims.height}
fill='orange'
alpha={0.5}
/>
<Text x={10} y={15} fontSize={32} fontFamily='Geist Sans' fill='black'>
Hello world!
</Text>
</>
);
}
await render(
<LayoutContext.Provider value={{ x: 15, y: 25, width: 200, height: 50 }}>
<Group rotate={10}>
<Inner />
</Group>
</LayoutContext.Provider>,
canvas,
config,
);
expect(dims!).toEqual({
width: 200,
height: 50,
});
expect(canvas.toBuffer('image/png')).toMatchImageSnapshot();
});
9 changes: 2 additions & 7 deletions test/text.test.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import './font';
import React from 'react';
import { join } from 'path';
import { test, expect } from 'vitest';
import config, { Canvas, GlobalFonts } from '@napi-rs/canvas';
import config, { Canvas } from '@napi-rs/canvas';
import { Text } from '../src';
import { render } from '../src/render';

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

test('should render <Text>', async () => {
const canvas = new Canvas(300, 100);
await render(
Expand Down

0 comments on commit 7469b44

Please sign in to comment.