-
Notifications
You must be signed in to change notification settings - Fork 1
/
fitText.test.ts
55 lines (43 loc) · 1.49 KB
/
fitText.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { expect, test } from 'bun:test';
import fitText from './fitText';
import { createCanvas } from '@napi-rs/canvas';
test('empty', () => {
const canvas = createCanvas(640, 480);
const context = canvas.getContext('2d');
expect(fitText('', 100, context)).toEqual({ text: '', width: 0 });
});
test('fit', () => {
const canvas = createCanvas(640, 480);
const context = canvas.getContext('2d');
expect(fitText('short', 100, context)).toEqual({ text: 'short', width: 22 });
});
test('one break', () => {
const canvas = createCanvas(640, 480);
const context = canvas.getContext('2d');
let text = 'short but long enough to break';
const slice = fitText(text, 100, context);
expect(slice).toEqual({
text: 'short but long enough ',
width: 99,
});
const rest = text.slice(slice.text.length);
expect(fitText(rest, 100, context)).toEqual({ text: 'to break', width: 36 });
});
test('two breaks', () => {
const canvas = createCanvas(640, 480);
const context = canvas.getContext('2d');
let text = 'short but long enough to break and then break again';
const slice1 = fitText(text, 100, context);
expect(slice1).toEqual({
text: 'short but long enough ',
width: 99,
});
text = text.slice(slice1.text.length);
const slice2 = fitText(text, 100, context);
expect(slice2).toEqual({
text: 'to break and then brea',
width: 100,
});
const rest = text.slice(slice2.text.length);
expect(fitText(rest, 100, context)).toEqual({ text: 'k again', width: 32 });
});