cropper.spec.js
10.5 KB
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
/**
* @author NHN Ent. FE Development Team <dl_javascript@nhn.com>
* @fileoverview Test cases of "src/js/component/cropper.js"
*/
import snippet from 'tui-code-snippet';
import fabric from 'fabric';
import $ from 'jquery';
import Cropper from '../src/js/component/cropper';
import Graphics from '../src/js/graphics';
import { eventNames, CROPZONE_DEFAULT_OPTIONS } from '../src/js/consts';
describe('Cropper', () => {
let cropper, graphics, canvas;
beforeEach(() => {
graphics = new Graphics($('<canvas>')[0]);
canvas = graphics.getCanvas();
cropper = new Cropper(graphics);
});
describe('start()', () => {
it('should create a cropzone', () => {
cropper.start();
expect(cropper._cropzone).toBeDefined();
});
it('should be applied predefined default options When creating a cropzone', () => {
cropper.start();
const cropzone = cropper._cropzone;
snippet.forEach(CROPZONE_DEFAULT_OPTIONS, (optionValue, optionName) => {
expect(cropzone[optionName]).toBe(optionValue);
});
});
it('should add a cropzone to canvas', () => {
spyOn(canvas, 'add');
cropper.start();
expect(canvas.add).toHaveBeenCalledWith(cropper._cropzone);
});
it('should no action if a croppzone has been defined', () => {
cropper._cropzone = {};
spyOn(canvas, 'add');
cropper.start();
expect(canvas.add).not.toHaveBeenCalled();
});
it('should set "evented" of all objects to false', () => {
const objects = [
new fabric.Rect({ evented: true }),
new fabric.Rect({ evented: true }),
new fabric.Rect({ evented: true }),
];
canvas.add(objects[0], objects[1], objects[2]);
cropper.start();
expect(objects[0].evented).toBe(false);
expect(objects[1].evented).toBe(false);
expect(objects[2].evented).toBe(false);
});
});
describe('"onFabricMouseDown()"', () => {
let fEvent;
beforeEach(() => {
fEvent = {
e: {},
};
spyOn(canvas, 'getPointer').and.returnValue({
x: 10,
y: 20,
});
});
it('should set "selection" to false', () => {
cropper._onFabricMouseDown(fEvent);
expect(canvas.selection).toBe(false);
});
it('should set "startX, startY"', () => {
// canvas.getPointer will return object{x: 10, y: 20}
cropper._onFabricMouseDown(fEvent);
expect(cropper._startX).toEqual(10);
expect(cropper._startY).toEqual(20);
});
});
describe('"onFabricMouseMove()', () => {
beforeEach(() => {
spyOn(canvas, 'getPointer').and.returnValue({
x: 10,
y: 20,
});
spyOn(canvas, 'getWidth').and.returnValue(100);
spyOn(canvas, 'getHeight').and.returnValue(200);
});
it(
'should re-render(remove->set->add) cropzone ' +
'if the mouse moving is over the threshold(=10)',
() => {
cropper._startX = 0;
cropper._startY = 0;
cropper.start();
spyOn(canvas, 'remove');
spyOn(cropper._cropzone, 'set');
spyOn(canvas, 'add');
cropper._onFabricMouseMove({ e: {} });
expect(canvas.remove).toHaveBeenCalled();
expect(cropper._cropzone.set).toHaveBeenCalled();
expect(canvas.add).toHaveBeenCalled();
}
);
it('should not re-render cropzone ' + 'if the mouse moving is under the threshold', () => {
cropper._startX = 14;
cropper._startY = 18;
cropper.start();
spyOn(canvas, 'remove');
spyOn(cropper._cropzone, 'set');
spyOn(canvas, 'add');
cropper._onFabricMouseMove({ e: {} });
expect(canvas.remove).not.toHaveBeenCalled();
expect(cropper._cropzone.set).not.toHaveBeenCalled();
expect(canvas.add).not.toHaveBeenCalled();
});
});
describe('_calcRectDimensionFromPoint()', () => {
beforeEach(() => {
cropper._startX = 10;
cropper._startY = 20;
snippet.extend(canvas, {
getWidth() {
return 100;
},
getHeight() {
return 200;
},
});
});
it('should return cropzone-left&top (min: 0, max: startX,Y)', () => {
const x = 20,
y = -1,
expected = {
left: 10,
top: 0,
width: jasmine.any(Number),
height: jasmine.any(Number),
},
actual = cropper._calcRectDimensionFromPoint(x, y);
expect(actual).toEqual(expected);
});
it('should calculate and return cropzone-width&height', () => {
let x, y, expected, actual;
x = 30;
y = 40;
expected = {
left: 10,
top: 20,
width: 20,
height: 20,
};
actual = cropper._calcRectDimensionFromPoint(x, y);
expect(actual).toEqual(expected);
x = 300;
y = 400;
expected = {
left: 10,
top: 20,
width: 90,
height: 180,
};
actual = cropper._calcRectDimensionFromPoint(x, y);
expect(actual).toEqual(expected);
});
it('should create cropzone that has fixed ratio during shift key is pressed.', () => {
const x = 100;
const y = 200;
const expected = {
left: 10,
top: 20,
width: 180,
height: 180,
};
cropper._withShiftKey = true;
const actual = cropper._calcRectDimensionFromPoint(x, y);
expect(actual).toEqual(expected);
});
it('should create cropzone that inverted current mouse position during shift key is pressed.', () => {
const x = -10;
const y = -20;
const expected = {
left: -10,
top: 0,
width: 20,
height: 20,
};
cropper._withShiftKey = true;
const actual = cropper._calcRectDimensionFromPoint(x, y);
expect(actual).toEqual(expected);
});
});
it('"onFabricMouseUp()" should activate cropzone', () => {
canvas.setActiveObject = jasmine.createSpy();
cropper.start();
cropper._onFabricMouseUp();
expect(canvas.setActiveObject).toHaveBeenCalledWith(cropper._cropzone);
});
describe('"crop()"', () => {
it('should return cropzone rect', () => {
cropper.start();
spyOn(cropper._cropzone, 'isValid').and.returnValue(true);
expect(cropper.getCropzoneRect()).toBeTruthy();
cropper.end();
});
it('should return cropzone data if the cropzone is valid', () => {
cropper.start();
spyOn(cropper._cropzone, 'isValid').and.returnValue(true);
expect(cropper.getCroppedImageData(cropper.getCropzoneRect())).toEqual({
imageName: jasmine.any(String),
url: jasmine.any(String),
});
cropper.end();
});
});
describe('"presets - setCropzoneRect()"', () => {
beforeEach(() => {
cropper.start();
});
afterEach(() => {
cropper.end();
});
it('should return cropzone rect as a square', () => {
spyOn(cropper._cropzone, 'isValid').and.returnValue(true);
cropper.setCropzoneRect(1 / 1);
expect(cropper.getCropzoneRect().width).toBe(cropper.getCropzoneRect().height);
});
it('should return cropzone rect as a 3:2 aspect box', () => {
spyOn(cropper._cropzone, 'isValid').and.returnValue(true);
cropper.setCropzoneRect(3 / 2);
expect((cropper.getCropzoneRect().width / cropper.getCropzoneRect().height).toFixed(1)).toBe(
(3 / 2).toFixed(1)
);
});
it('should return cropzone rect as a 4:3 aspect box', () => {
spyOn(cropper._cropzone, 'isValid').and.returnValue(true);
cropper.setCropzoneRect(4 / 3);
expect((cropper.getCropzoneRect().width / cropper.getCropzoneRect().height).toFixed(1)).toBe(
(4 / 3).toFixed(1)
);
});
it('should return cropzone rect as a 5:4 aspect box', () => {
spyOn(cropper._cropzone, 'isValid').and.returnValue(true);
cropper.setCropzoneRect(5 / 4);
expect((cropper.getCropzoneRect().width / cropper.getCropzoneRect().height).toFixed(1)).toBe(
(5 / 4).toFixed(1)
);
});
it('should return cropzone rect as a 7:5 aspect box', () => {
spyOn(cropper._cropzone, 'isValid').and.returnValue(true);
cropper.setCropzoneRect(7 / 5);
expect((cropper.getCropzoneRect().width / cropper.getCropzoneRect().height).toFixed(1)).toBe(
(7 / 5).toFixed(1)
);
});
it('should return cropzone rect as a 16:9 aspect box', () => {
spyOn(cropper._cropzone, 'isValid').and.returnValue(true);
cropper.setCropzoneRect(16 / 9);
expect((cropper.getCropzoneRect().width / cropper.getCropzoneRect().height).toFixed(1)).toBe(
(16 / 9).toFixed(1)
);
});
it('Even in situations with floating point problems, should calculate the exact width you expect.', () => {
spyOn(canvas, 'getWidth').and.returnValue(408);
spyOn(canvas, 'getHeight').and.returnValue(312);
spyOn(cropper._cropzone, 'set').and.callThrough();
cropper.setCropzoneRect(16 / 9);
expect(cropper._cropzone.set.calls.first().args[0].width).toBe(408);
});
it('should remove cropzone of cropper when falsy is passed', () => {
cropper.setCropzoneRect();
expect(cropper.getCropzoneRect()).toBeFalsy();
cropper.setCropzoneRect(0);
expect(cropper.getCropzoneRect()).toBeFalsy();
cropper.setCropzoneRect(null);
expect(cropper.getCropzoneRect()).toBeFalsy();
});
});
describe('"end()"', () => {
it('should set cropzone of cropper to null', () => {
cropper.start();
cropper.end();
expect(cropper._cropzone).toBe(null);
});
it('should set "evented" of all obejcts to true', () => {
const objects = [
new fabric.Rect({ evented: false }),
new fabric.Rect({ evented: false }),
new fabric.Rect({ evented: false }),
];
canvas.add(objects[0], objects[1], objects[2]);
cropper.start();
cropper.end();
expect(objects[0].evented).toBe(true);
expect(objects[1].evented).toBe(true);
expect(objects[2].evented).toBe(true);
});
});
describe('canvas event delegator', () => {
it('The event of an object with an eventDelegator must fire the graphics.fire registered with the trigger.', () => {
cropper.start();
spyOn(graphics, 'fire');
const events = eventNames;
const fEvent = {
target: cropper._cropzone,
};
const cropzone = cropper._cropzone;
canvas.fire('object:scaling', fEvent);
expect(graphics.fire.calls.count()).toBe(0);
cropzone.canvasEventTrigger[events.OBJECT_SCALED](cropzone);
expect(graphics.fire.calls.count()).toBe(1);
});
});
});