graphics.spec.js
6.79 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
/**
* @author NHN Ent. FE Development Team <dl_javascript@nhn.com>
* @fileoverview Tests command with command-factory
*/
import snippet from 'tui-code-snippet';
import fabric from 'fabric';
import Graphics from '../src/js/graphics';
import { drawingModes, componentNames as components } from '../src/js/consts';
describe('Graphics', () => {
const cssMaxWidth = 900;
const cssMaxHeight = 700;
let graphics, canvas;
beforeEach(() => {
graphics = new Graphics(document.createElement('canvas'), {
cssMaxWidth,
cssMaxHeight,
});
canvas = graphics.getCanvas();
});
it('has several properties', () => {
expect(canvas).not.toBe(null);
expect(canvas).toEqual(jasmine.any(fabric.Canvas));
expect(graphics.cssMaxWidth).toBe(900);
expect(graphics.cssMaxHeight).toBe(700);
expect(graphics.canvasImage).toBe(null);
expect(graphics.imageName).toBe('');
expect(graphics._drawingMode).toBe(drawingModes.NORMAL);
expect(graphics._componentMap).not.toBe(null);
});
it('After the path has been drawn, "origin" should change to "left top-> center center" and "position" should change to the center coordinates of path.', () => {
const pathObj = new fabric.Path('M 0 0 L 100 0 L 100 100 L 0 100 z');
const expectPosition = pathObj.getCenterPoint();
const expectX = expectPosition.x;
const expectY = expectPosition.y;
graphics._onPathCreated({ path: pathObj });
expect(pathObj.originX).toBe('center');
expect(pathObj.originY).toBe('center');
expect(pathObj.left).toBe(expectX);
expect(pathObj.top).toBe(expectY);
});
it('can attach canvas events', () => {
const onMousedown = jasmine.createSpy('onMousedown');
const onObjectAdded = jasmine.createSpy('onObjectAdded');
const onObjectSelected = jasmine.createSpy('onObjectSelected');
graphics.on({
mousedown: onMousedown,
'object:added': onObjectAdded,
});
graphics.once('object:selected', onObjectSelected);
graphics.fire('mousedown');
graphics.fire('mousedown');
graphics.fire('object:added');
graphics.fire('object:added');
graphics.fire('object:selected');
graphics.fire('object:selected');
expect(onMousedown.calls.count()).toBe(2);
expect(onObjectAdded.calls.count()).toBe(2);
expect(onObjectSelected.calls.count()).toBe(1);
});
it('deactivates all objects', () => {
const triangle = new fabric.Triangle({
width: 20,
height: 30,
});
canvas.add(triangle).setActiveObject(triangle);
expect(canvas.getActiveObject()).not.toBe(null);
graphics.deactivateAll();
expect(canvas.getActiveObject()).toBe(null);
});
it('renders objects', (done) => {
let beforeRender = false;
const triangle = new fabric.Triangle({
width: 20,
height: 30,
});
canvas.add(triangle);
canvas.on('before:render', () => {
beforeRender = true;
});
canvas.on('after:render', () => {
expect(beforeRender).toBe(true);
done();
});
graphics.renderAll();
});
it('removes a object or group by id', () => {
const triangle = new fabric.Triangle({
width: 20,
height: 30,
});
graphics.add(triangle);
const objectId = snippet.stamp(triangle);
graphics.removeObjectById(objectId);
expect(graphics.getObjects().length).toBe(0);
});
it('switches drawing modes', () => {
let modeName;
for (modeName in drawingModes) {
if (drawingModes.hasOwnProperty(modeName)) {
graphics.startDrawingMode(modeName);
expect(graphics.getDrawingMode()).toBe(modeName);
graphics.stopDrawingMode();
expect(graphics.getDrawingMode()).toBe(drawingModes.NORMAL);
}
}
});
it('can get the cropped image data', () => {
graphics.startDrawingMode(drawingModes.CROPPER);
spyOn(graphics.getComponent(components.CROPPER)._cropzone, 'isValid').and.returnValue(true);
expect(graphics.getCropzoneRect()).toBeTruthy();
expect(graphics.getCroppedImageData(graphics.getCropzoneRect())).toEqual({
imageName: jasmine.any(String),
url: jasmine.any(String),
});
graphics.stopDrawingMode();
});
it('Cropzone must be hidden initially and then redisplayed after completion at toDataURL is executed with a cropzone present', () => {
const cropper = graphics.getComponent(components.CROPPER);
spyOn(cropper, 'changeVisibility');
graphics.startDrawingMode(drawingModes.CROPPER);
graphics.toDataURL();
expect(cropper.changeVisibility.calls.allArgs()).toEqual([[false], [true]]);
});
it('can set brush setting into LINE_DRAWING, FREE_DRAWING', () => {
graphics.startDrawingMode(drawingModes.LINE_DRAWING);
graphics.setBrush({
width: 12,
color: 'FFFF00',
});
const brush = canvas.freeDrawingBrush;
expect(brush.width).toBe(12);
expect(brush.color).toBe('rgba(255,255,0,1)');
graphics.stopDrawingMode();
});
it('can change a drawing shape', () => {
const shapeComp = graphics.getComponent(components.SHAPE);
graphics.setDrawingShape('circle', {
fill: 'transparent',
stroke: 'blue',
strokeWidth: 3,
rx: 10,
ry: 100,
});
expect(shapeComp._type).toBe('circle');
expect(shapeComp._options).toEqual({
strokeWidth: 3,
stroke: 'blue',
fill: 'transparent',
width: 1,
height: 1,
rx: 10,
ry: 100,
lockSkewingX: true,
lockSkewingY: true,
bringForward: true,
isRegular: false,
});
});
it('can register custom icon', () => {
const iconComp = graphics.getComponent(components.ICON);
graphics.registerPaths({
customIcon: 'M 0 0 L 20 20 L 10 10 Z',
});
expect(iconComp._pathMap).toEqual(
jasmine.objectContaining({
customIcon: 'M 0 0 L 20 20 L 10 10 Z',
})
);
});
it('has the filter', () => {
expect(graphics.hasFilter('Grayscale')).toBe(false);
});
describe('pasteObject()', () => {
let targetObject1, targetObject2;
beforeEach(() => {
targetObject1 = new fabric.Object({});
targetObject2 = new fabric.Object({});
canvas.add(targetObject1);
canvas.add(targetObject2);
});
it('Group objects must be duplicated as many as the number of objects in the group.', (done) => {
const groupObject = graphics.getActiveSelectionFromObjects(canvas.getObjects());
graphics.setActiveObject(groupObject);
graphics.resetTargetObjectForCopyPaste();
graphics.pasteObject().then(() => {
expect(canvas.getObjects().length).toBe(4);
done();
});
});
it('Only one object should be duplicated.', (done) => {
graphics.setActiveObject(targetObject1);
graphics.resetTargetObjectForCopyPaste();
graphics.pasteObject().then(() => {
expect(canvas.getObjects().length).toBe(3);
done();
});
});
});
});