forked from pencil-js/pencil.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.test.js
193 lines (162 loc) · 4.67 KB
/
text.test.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import test from "ava";
import Text from "./text";
test.beforeEach((t) => {
t.context = new Text([0, 0], "Hello\nworld");
});
test.cb("constructor", (t) => {
t.deepEqual(t.context.lines, ["Hello", "world"]);
const defaultText = new Text();
t.deepEqual(defaultText.lines, [""]);
const textWithUrl = new Text([0, 0], "test", {
font: "http://test.com",
});
textWithUrl.on("ready", () => {
t.end();
});
});
test("get and set text", (t) => {
t.is(t.context.text, "Hello\nworld");
t.context.text = "Hello world";
t.is(t.context.lines.length, 1);
t.is(t.context.text, "Hello world");
t.context.text = ["Array", "build"];
t.is(t.context.lines.length, 2);
t.is(t.context.text, "Array\nbuild");
t.context.text = ["Mix", "Array\nand line-break"];
t.is(t.context.lines.length, 3);
t.is(t.context.text, "Mix\nArray\nand line-break");
});
test("makePath", (t) => {
const expected = [
["Hello", 0, 0],
["world", 0, 5],
];
const ctx = {
fillText: (...params) => {
t.deepEqual(params, expected[ctx.call]);
},
strokeText: (...params) => {
t.deepEqual(params, expected[ctx.call++]);
},
call: 0,
};
t.plan(8);
t.context.options.fill = "#123456";
t.context.options.stroke = "#456789";
t.context.makePath(ctx);
t.is(ctx.font, Text.getFontDefinition(t.context.options));
t.is(ctx.textAlign, t.context.options.align);
t.is(ctx.fillStyle, t.context.options.fill);
t.is(ctx.strokeStyle, t.context.options.stroke);
});
test("makePath with underscore", (t) => {
t.context.text = " ";
t.context.options.underscore = true;
const ctx = {
beginPath: () => t.pass(),
fillText: () => t.pass(),
moveTo: () => t.pass(),
lineTo: () => t.pass(),
stroke: () => t.pass(),
closePath: () => t.pass(),
};
t.plan(7);
t.context.makePath(ctx);
t.is(ctx.strokeStyle, t.context.options.fill);
});
test("makePath with no text", (t) => {
const ctx = {
fillText: () => t.fail(),
strokeText: () => t.fail(),
};
t.context.text = "";
t.context.makePath(ctx);
t.pass();
});
test("makePath with no fill nor stroke", (t) => {
const ctx = {
fillText: () => t.fail(),
strokeText: () => t.fail(),
};
t.context.options.fill = null;
t.context.options.stroke = null;
t.context.makePath(ctx);
t.pass();
});
test("isHover", (t) => {
t.true(t.context.isHover([1, 1]));
t.false(t.context.isHover([99, 0]));
t.context.options.shown = false;
t.false(t.context.isHover([1, 1]));
});
test("getOriginPosition", (t) => {
t.context.options.align = Text.alignments.center;
t.is(t.context.getOriginPosition().x, 0.5);
t.context.options.align = Text.alignments.right;
t.is(t.context.getOriginPosition().x, 1);
t.context.options.align = "bad";
t.is(t.context.getOriginPosition().x, 0);
});
test("measures and width/height", (t) => {
t.deepEqual(t.context.getMeasures(), {
width: 1,
height: 10,
});
t.is(t.context.width, 1);
t.is(t.context.height, 10);
});
test("toJSON", (t) => {
const json = t.context.toJSON();
t.is(json.text, "Hello\nworld");
t.is(json.constructor, "Text");
});
test("from", (t) => {
const definition = {
text: "whatever",
};
const text = Text.from(definition);
t.is(text.text, "whatever");
});
test.cb("load", (t) => {
const fontUrls = [
"font",
"url",
];
Text.load(fontUrls).then((names) => {
t.deepEqual(names, fontUrls);
t.end();
});
});
test("getFontDefinition", (t) => {
t.is(Text.getFontDefinition(t.context.options), `${t.context.options.fontSize}px ${t.context.options.font}`);
t.context.options.bold = true;
t.context.options.italic = true;
t.true(Text.getFontDefinition(t.context.options).includes("bold"));
t.true(Text.getFontDefinition(t.context.options).includes("italic"));
});
test("measure", (t) => {
t.deepEqual(Text.measure("whatever"), {
width: 1,
height: 5,
});
t.deepEqual(Text.measure("whatever", {}), {
width: 1,
height: 5,
});
});
test("defaultOptions", (t) => {
const opts = Text.defaultOptions;
t.is(opts.font, "sans-serif");
t.is(opts.fontSize, 20);
t.is(opts.align, "start");
t.is(opts.bold, false);
t.is(opts.italic, false);
});
test("alignments", (t) => {
const aligns = Text.alignments;
t.is(aligns.start, "start");
t.is(aligns.end, "end");
t.is(aligns.left, "left");
t.is(aligns.center, "center");
t.is(aligns.right, "right");
});