This repository has been archived by the owner on Jan 28, 2020. It is now read-only.
forked from nhn/tui.image-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shape.spec.js
389 lines (297 loc) · 11.6 KB
/
shape.spec.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/**
* @author NHN Ent. FE Development Team <[email protected]>
* @fileoverview Test cases of "src/js/component/line.js"
*/
import {fabric} from 'fabric';
import $ from 'jquery';
import Graphics from '../src/js/graphics';
import Shape from '../src/js/component/shape';
describe('Shape', () => {
let canvas, graphics, mockImage, fEvent, shape, shapeObj;
beforeAll(() => {
graphics = new Graphics($('<canvas>')[0]);
canvas = graphics.getCanvas();
shape = new Shape(graphics);
});
beforeEach(() => {
mockImage = new fabric.Image();
graphics.setCanvasImage('mockImage', mockImage);
fEvent = {
e: {}
};
});
afterEach(() => {
canvas.forEachObject(obj => {
canvas.remove(obj);
});
});
it('The rectagle object is created on canvas.', () => {
shape.add('rect');
[shapeObj] = canvas.getObjects();
expect(shapeObj.type).toBe('rect');
});
it('The circle object(ellipse) is created on canvas.', () => {
shape.add('circle');
[shapeObj] = canvas.getObjects();
expect(shapeObj.type).toBe('circle');
});
it('The triangle object is created on canvas.', () => {
shape.add('triangle');
[shapeObj] = canvas.getObjects();
expect(shapeObj.type).toBe('triangle');
});
it('When add() is called with no options, the default options set the rectangle object.', () => {
shape.add('rect');
[shapeObj] = canvas.getObjects();
expect(shapeObj.getWidth()).toBe(2); // strokeWidth: 1, width: 1
expect(shapeObj.getHeight()).toBe(2); // strokeWidth: 1, height: 1
});
it('When add() is called with no options, the default options set the circle object.', () => {
shape.add('circle');
[shapeObj] = canvas.getObjects();
expect(shapeObj.getWidth()).toBe(1);
expect(shapeObj.getHeight()).toBe(1);
});
it('When add() is called with no options, the default options set the triangle object.', () => {
shape.add('triangle');
[shapeObj] = canvas.getObjects();
expect(shapeObj.getWidth()).toBe(2); // strokeWidth: 1, width: 1
expect(shapeObj.getHeight()).toBe(2); // strokeWidth: 1, height: 1
});
it('When add() is called with the options, this options set the rectagle object.', () => {
const settings = {
fill: 'blue',
stroke: 'red',
strokeWidth: 10,
type: 'rect',
width: 100,
height: 100
};
shape.add('rect', settings);
[shapeObj] = canvas.getObjects();
expect(shapeObj.getFill()).toBe('blue');
expect(shapeObj.getStroke()).toBe('red');
expect(shapeObj.getStrokeWidth()).toBe(10);
expect(shapeObj.getWidth()).toBe(110); // width + storkeWidth
expect(shapeObj.getHeight()).toBe(110); // height + storkeWidth
});
it('When add() is called with the options, this options set the circle object.', () => {
const settings = {
fill: 'blue',
stroke: 'red',
strokeWidth: 3,
type: 'circle',
rx: 100,
ry: 50
};
shape.add('circle', settings);
[shapeObj] = canvas.getObjects();
expect(shapeObj.getFill()).toBe('blue');
expect(shapeObj.getStroke()).toBe('red');
expect(shapeObj.getStrokeWidth()).toBe(3);
expect(shapeObj.getWidth()).toBe(203); // rx * 2 + stokeWidth
expect(shapeObj.getHeight()).toBe(103); // ry * 2 + stokeWidth
});
it('When add() is called with the options, this options set the triangle object.', () => {
const settings = {
fill: 'blue',
stroke: 'red',
strokeWidth: 0,
type: 'triangle',
width: 100,
height: 100
};
shape.add('triangle', settings);
[shapeObj] = canvas.getObjects();
expect(shapeObj.getFill()).toBe('blue');
expect(shapeObj.getStroke()).toBe('red');
expect(shapeObj.getStrokeWidth()).toBe(0);
expect(shapeObj.getWidth()).toBe(100);
expect(shapeObj.getHeight()).toBe(100);
});
it('When change() is called, the style of the rectagle object is changed.', () => {
shape.add('rect');
[shapeObj] = canvas.getObjects();
shape.change(shapeObj, {
fill: 'blue',
stroke: 'red',
width: 10,
height: 20
});
expect(shapeObj.getFill()).toBe('blue');
expect(shapeObj.getStroke()).toBe('red');
expect(shapeObj.getWidth()).toBe(11);
expect(shapeObj.getHeight()).toBe(21);
});
it('When change() is called, the style of the circle object is changed.', () => {
shape.add('circle');
[shapeObj] = canvas.getObjects();
shape.change(shapeObj, {
fill: 'blue',
stroke: 'red',
rx: 10,
ry: 20
});
expect(shapeObj.getFill()).toBe('blue');
expect(shapeObj.getStroke()).toBe('red');
expect(shapeObj.getWidth()).toBe(21);
expect(shapeObj.getHeight()).toBe(41);
});
it('When change() is called, the style of the triangle object is changed.', () => {
shape.add('triangle');
[shapeObj] = canvas.getObjects();
shape.change(shapeObj, {
width: 10,
height: 20
});
expect(shapeObj.getFill()).toBe('#ffffff');
expect(shapeObj.getStroke()).toBe('#000000');
expect(shapeObj.getWidth()).toBe(11);
expect(shapeObj.getHeight()).toBe(21);
});
describe('_onFabricMouseMove()', () => {
beforeEach(() => {
shape.add('rect', {
left: 100,
top: 100
});
[shapeObj] = canvas.getObjects();
shape._shapeObj = shapeObj;
});
it('When the mouse direction is in 1th quadrant,' +
'the origin values of shape set to "left" and "top".', () => {
spyOn(canvas, 'getPointer').and.returnValue({
x: 200,
y: 120
});
shape._onFabricMouseMove(fEvent);
expect(shapeObj.getOriginX()).toBe('left');
expect(shapeObj.getOriginY()).toBe('top');
});
it('When the mouse direction is in 2th quadrant,' +
'the origin values of shape set to "right" and "top".', () => {
spyOn(canvas, 'getPointer').and.returnValue({
x: 80,
y: 100
});
shape._onFabricMouseMove(fEvent);
expect(shapeObj.getOriginX()).toBe('right');
expect(shapeObj.getOriginY()).toBe('top');
});
it('When the mouse direction is in 3th quadrant,' +
'the origin values of shape set to "right" and "bottom".', () => {
spyOn(canvas, 'getPointer').and.returnValue({
x: 80,
y: 80
});
shape._onFabricMouseMove(fEvent);
expect(shapeObj.getOriginX()).toBe('right');
expect(shapeObj.getOriginY()).toBe('bottom');
});
it('When the mouse direction is in 4th quadrant,' +
'the origin values of shape set to "left" and "bottom".', () => {
spyOn(canvas, 'getPointer').and.returnValue({
x: 200,
y: 80
});
shape._onFabricMouseMove(fEvent);
expect(shapeObj.getOriginX()).toBe('left');
expect(shapeObj.getOriginY()).toBe('bottom');
});
});
describe('_onFabricMouseUp()', () => {
let startPoint, expectedPoint;
beforeEach(() => {
shape.add('circle', {
left: 100,
top: 100
});
[shapeObj] = canvas.getObjects();
shape._shapeObj = shapeObj;
});
it('When the drawing shape is in 1th quadrant, "left" and "top" are the same as start point.', () => {
spyOn(canvas, 'getPointer').and.returnValue({
x: 200,
y: 120
});
startPoint = shapeObj.getPointByOrigin('left', 'top');
shape._onFabricMouseMove(fEvent);
shape._onFabricMouseUp();
expectedPoint = shapeObj.getPointByOrigin('left', 'top');
expect(expectedPoint.x).toBe(startPoint.x);
expect(expectedPoint.y).toBe(startPoint.y);
});
it('When the drawing shape is in 2th quadrant, "right" and "top" are the same as start point.', () => {
spyOn(canvas, 'getPointer').and.returnValue({
x: 80,
y: 120
});
startPoint = shapeObj.getPointByOrigin('right', 'top');
shape._onFabricMouseMove(fEvent);
shape._onFabricMouseUp();
expectedPoint = shapeObj.getPointByOrigin('right', 'top');
expect(expectedPoint.x).toBe(startPoint.x);
expect(expectedPoint.y).toBe(startPoint.y);
});
it('When the drawing shape is in 3th quadrant, "right" and "bottom" are the same as start point.', () => {
spyOn(canvas, 'getPointer').and.returnValue({
x: 80,
y: 80
});
startPoint = shapeObj.getPointByOrigin('right', 'bottom');
shape._onFabricMouseMove(fEvent);
shape._onFabricMouseUp();
expectedPoint = shapeObj.getPointByOrigin('right', 'bottom');
expect(expectedPoint.x).toBe(startPoint.x);
expect(expectedPoint.y).toBe(startPoint.y);
});
it('When the drawing shape is in 4th quadrant, "left" and "bottom" are the same as start point.', () => {
spyOn(canvas, 'getPointer').and.returnValue({
x: 120,
y: 80
});
startPoint = shapeObj.getPointByOrigin('left', 'bottom');
shape._onFabricMouseMove(fEvent);
shape._onFabricMouseUp();
expectedPoint = shapeObj.getPointByOrigin('left', 'bottom');
expect(expectedPoint.x).toBe(startPoint.x);
expect(expectedPoint.y).toBe(startPoint.y);
});
});
it('When drawing the shape with mouse and the "isRegular" option set to true, ' +
'the created rectangle shape has the same "width" and "height" values.', () => {
shape.add('rect', {
left: 0,
top: 0
});
shape._withShiftKey = true;
[shapeObj] = canvas.getObjects();
shape._shapeObj = shapeObj;
spyOn(canvas, 'getPointer').and.returnValue({
x: 200,
y: 100
});
shape._onFabricMouseMove(fEvent);
shape._onFabricMouseUp();
expect(shapeObj.getWidth()).toBe(201); // has 1 storkeWidth
expect(shapeObj.getHeight()).toBe(201); // has 1 storkeWidth
});
it('When drawing the shape with mouse and the "isRegular" option set to true, ' +
'the created rectangle shape has the same "width" and "height" values.', () => {
shape.add('rect', {
left: 0,
top: 0
});
shape._withShiftKey = true;
[shapeObj] = canvas.getObjects();
shape._shapeObj = shapeObj;
spyOn(canvas, 'getPointer').and.returnValue({
x: 100,
y: 200
});
shape._onFabricMouseMove(fEvent);
shape._onFabricMouseUp();
expect(shapeObj.getWidth()).toBe(201); // has 1 storkeWidth
expect(shapeObj.getHeight()).toBe(201); // has 1 storkeWidth
});
});