cropzone.spec.js
3.87 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
/**
* @author NHN Ent. FE Development Team <dl_javascript@nhn.com>
* @fileoverview Test cases of "src/js/extension/cropzone.js"
*/
import snippet from 'tui-code-snippet';
import fabric from 'fabric';
import Cropzone from '../src/js/extension/cropzone';
describe('Cropzone', () => {
const options = {
left: 10,
top: 10,
width: 100,
height: 100,
cornerSize: 10,
strokeWidth: 0,
cornerColor: 'black',
fill: 'transparent',
hasRotatingPoint: false,
hasBorders: false,
lockScalingFlip: true,
lockRotation: true,
};
const canvas = new fabric.Canvas();
canvas.height = 400;
canvas.width = 300;
it('"_getCoordinates()" should return outer&inner rect coordinates(array)', () => {
const cropzone = new Cropzone(canvas, options, {});
const coords = cropzone._getCoordinates();
expect(coords).toEqual({
x: [-60, -50, 50, 240],
y: [-60, -50, 50, 340],
});
});
it('"_onMoving()" should set left and top between 0 and canvas size', () => {
const cropzone = new Cropzone(canvas, options, {});
const mockFabricCanvas = {
getWidth() {
return 300;
},
getHeight() {
return 400;
},
};
cropzone.canvas = mockFabricCanvas;
cropzone.left = -1;
cropzone.top = -1;
cropzone._onMoving();
expect(cropzone.top).toEqual(0);
expect(cropzone.left).toEqual(0);
cropzone.left = 1000;
cropzone.top = 1000;
cropzone._onMoving();
expect(cropzone.left).toEqual(200);
expect(cropzone.top).toEqual(300);
});
it('"isValid()" should return whether the cropzone has real area or not', () => {
const cropzone = new Cropzone(canvas, options, {});
cropzone.left = -1;
expect(cropzone.isValid()).toBe(false);
cropzone.left = 1;
expect(cropzone.isValid()).toBe(true);
cropzone.height = -1;
expect(cropzone.isValid()).toBe(false);
cropzone.height = 1;
expect(cropzone.isValid()).toBe(true);
});
it('"_resizeTL" should give the expected value at run', () => {
const cropzone = new Cropzone(canvas, options, {});
expect(
cropzone._resizeCropZone(
{
x: 30,
y: 40,
},
'tl'
)
).toEqual({
left: 30,
top: 40,
width: 80,
height: 70,
});
});
it('"_resizeTR" should give the expected value at run', () => {
const cropzone = new Cropzone(canvas, options, {});
expect(
cropzone._resizeCropZone(
{
x: 80,
y: 50,
},
'tr'
)
).toEqual({
left: 10,
top: 50,
width: 70,
height: 60,
});
});
it('"_resizeBL" should give the expected value at run', () => {
const cropzone = new Cropzone(canvas, options, {});
expect(
cropzone._resizeCropZone(
{
x: 30,
y: 40,
},
'bl'
)
).toEqual({
left: 30,
top: 10,
width: 80,
height: 30,
});
});
it('"_resizeBR" should give the expected value at run', () => {
const cropzone = new Cropzone(canvas, options, {});
expect(
cropzone._resizeCropZone(
{
x: 30,
y: 40,
},
'br'
)
).toEqual({
left: 10,
top: 10,
width: 20,
height: 30,
});
});
it('should yield the result of maintaining the ratio at running the resize function at a fixed rate', () => {
const presetRatio = 5 / 4;
const cropzone = new Cropzone(
canvas,
snippet.extend({}, options, {
width: 50,
height: 40,
presetRatio,
}),
{}
);
snippet.forEach(['tl', 'tr', 'mt', 'ml', 'mr', 'mb', 'bl', 'br'], (cornerType) => {
const { width, height } = cropzone._resizeCropZone(
{
x: 20,
y: 20,
},
cornerType
);
expect(width / height).toEqual(presetRatio);
});
});
});