Spaces:
Running
Running
File size: 12,133 Bytes
b456468 |
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 390 |
import { fabric } from 'fabric';
import Graphics from '@/graphics';
import Shape from '@/component/shape';
import { resize } from '@/helper/shapeResizeHelper';
import { getFillImageFromShape, getCachedCanvasImageElement } from '@/helper/shapeFilterFillHelper';
describe('Shape', () => {
let canvas, graphics, mockImage, fEvent, shape, shapeObj;
beforeAll(() => {
graphics = new Graphics(document.createElement('canvas'));
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('should be calculated correctly.', () => {
const pointer = canvas.getPointer(fEvent.e);
const settings = {
strokeWidth: 0,
type: 'rect',
left: 150,
top: 200,
width: 40,
height: 40,
originX: 'center',
originY: 'center',
};
shape.add('rect', settings);
[shapeObj] = canvas.getObjects();
const setSpy = jest.spyOn(shapeObj, 'set');
resize(shapeObj, pointer);
expect(setSpy).toHaveBeenCalledWith(
expect.objectContaining({
left: settings.left - settings.width / 2,
top: settings.top - settings.height / 2,
})
);
});
it('should be created on canvas(rect)', () => {
shape.add('rect');
[shapeObj] = canvas.getObjects();
expect(shapeObj.get('type')).toBe('rect');
});
it('should be created on canvas(circle)', () => {
shape.add('circle');
[shapeObj] = canvas.getObjects();
expect(shapeObj.get('type')).toBe('circle');
});
it('should be created on canvas(triangle)', () => {
shape.add('triangle');
[shapeObj] = canvas.getObjects();
expect(shapeObj.type).toBe('triangle');
});
it('should be set the rectangle object when add() is called with no options', () => {
shape.add('rect');
[shapeObj] = canvas.getObjects();
expect(shapeObj).toMatchObject({ width: 1, height: 1 }); // strokeWidth: 1, width: 1, height: 1
});
it('should be set the circle object when add() is called with no options', () => {
shape.add('circle');
[shapeObj] = canvas.getObjects();
expect(shapeObj).toMatchObject({ width: 0, height: 0 });
});
it('should be set the triangle object when add() is called with no options', () => {
shape.add('triangle');
[shapeObj] = canvas.getObjects();
expect(shapeObj).toMatchObject({ width: 1, height: 1 }); // strokeWidth: 1, width: 1, height: 1
});
it('should be set the rectangle object when add() is called with the options', () => {
const settings = {
fill: 'blue',
stroke: 'red',
strokeWidth: 10,
type: 'rect',
width: 100,
height: 100,
};
shape.add('rect', settings);
[shapeObj] = canvas.getObjects();
expect(shapeObj).toMatchObject(settings);
});
it('should be set the circle object when add() is called with the options', () => {
const settings = {
fill: 'blue',
stroke: 'red',
strokeWidth: 3,
type: 'circle',
rx: 100,
ry: 50,
};
shape.add('circle', settings);
[shapeObj] = canvas.getObjects();
expect(shapeObj).toMatchObject(settings);
});
it('should be set the triangle object when add() is called with the options', () => {
const settings = {
fill: 'blue',
stroke: 'red',
strokeWidth: 0,
type: 'triangle',
width: 100,
height: 100,
};
shape.add('triangle', settings);
[shapeObj] = canvas.getObjects();
expect(shapeObj).toMatchObject(settings);
});
it('should be changed when change() is called(rect)', () => {
const settings = { fill: 'blue', stroke: 'red', width: 10, height: 20 };
shape.add('rect');
[shapeObj] = canvas.getObjects();
shape.change(shapeObj, settings);
expect(shapeObj).toMatchObject(settings);
});
it('should be changed when change() is called(circle)', () => {
const settings = { fill: 'blue', stroke: 'red', rx: 10, ry: 20 };
shape.add('circle');
[shapeObj] = canvas.getObjects();
shape.change(shapeObj, settings);
expect(shapeObj).toMatchObject(settings);
});
it('should be changed when change() is called(triangle)', () => {
const settings = { fill: 'blue', stroke: 'red', width: 10, height: 20 };
shape.add('triangle');
[shapeObj] = canvas.getObjects();
shape.change(shapeObj, settings);
expect(shapeObj).toMatchObject(settings);
});
describe('Fill - filter type', () => {
beforeEach(() => {
getCachedCanvasImageElement(canvas, true);
mockImage = new fabric.Image();
graphics.setCanvasImage('mockImage', mockImage);
shape.add('rect', {
strokeWidth: 0,
left: 20,
top: 30,
width: 100,
height: 80,
fill: {
type: 'filter',
filter: [{ pixelate: 20 }],
},
});
[shapeObj] = canvas.getObjects();
});
it('should be executed when a movement, rotation, and scaling event of a filter type fill is applied', () => {
jest.spyOn(canvas, 'getPointer').mockReturnValue({ x: 10, y: 10 });
const _resetPositionFillFilterSpy = jest.spyOn(shape, '_resetPositionFillFilter');
shapeObj.fire('moving');
shapeObj.fire('rotating');
shapeObj.fire('scaling');
expect(_resetPositionFillFilterSpy).toHaveBeenCalledTimes(3);
});
it('should be changed cropX and cropY values of the image filled with the shape background', () => {
shape._resetPositionFillFilter(shapeObj);
const fillImage = getFillImageFromShape(shapeObj);
expect(fillImage).toMatchObject({ cropX: -30, cropY: -10 });
});
it('should be the same size as the shape', () => {
shape._resetPositionFillFilter(shapeObj);
const fillImage = getFillImageFromShape(shapeObj);
expect(fillImage).toMatchObject({ width: 100, height: 80 });
});
it('should be the same size as the rectangle that draws the rotated object border', () => {
shapeObj.set({ angle: 40 });
shape._resetPositionFillFilter(shapeObj);
const fillImage = getFillImageFromShape(shapeObj);
expect(fillImage).toMatchSnapshot();
});
it('should have the shape reverse rotation value if repositioning is performed while the angle is changed', () => {
shapeObj.set({ angle: 40 });
shape._resetPositionFillFilter(shapeObj);
const { angle } = getFillImageFromShape(shapeObj);
expect(angle).toBe(-40);
});
it('should give the expected result for shapes that go outside the bottom right area of the canvas', async () => {
const obj = await shape.add('rect', {
strokeWidth: 0,
left: 250,
top: 100,
width: 200,
height: 200,
fill: {
type: 'filter',
filter: [{ pixelate: 20 }],
},
});
shapeObj = graphics.getObject(obj.id);
const fillImage = getFillImageFromShape(shapeObj);
expect(fillImage).toMatchObject({ top: 75, left: 75, width: 150, height: 150 });
});
it('should give the expected result for shapes that go outside the top left area of the canvas', async () => {
const obj = await shape.add('rect', {
strokeWidth: 0,
left: 50,
top: 30,
width: 200,
height: 70,
fill: {
type: 'filter',
filter: [{ pixelate: 20 }],
},
});
shapeObj = graphics.getObject(obj.id);
const fillImage = getFillImageFromShape(shapeObj);
expect(fillImage).toMatchSnapshot();
});
it('should have the applied filter', () => {
const fillImage = getFillImageFromShape(shapeObj);
expect(fillImage.filters).not.toHaveLength(0);
});
});
describe('_onFabricMouseMove()', () => {
beforeEach(() => {
shape.add('rect', { left: 100, top: 100 });
[shapeObj] = canvas.getObjects();
shape._shapeObj = shapeObj;
});
it('should be set to "left" and "top" when the mouse direction is in 1th quadrant', () => {
jest.spyOn(canvas, 'getPointer').mockReturnValue({ x: 200, y: 120 });
shape._onFabricMouseMove(fEvent);
expect(shapeObj).toMatchObject({ originX: 'left', originY: 'top' });
});
it('should be set to "right" and "top" when the mouse direction is in 2th quadrant', () => {
jest.spyOn(canvas, 'getPointer').mockReturnValue({ x: 80, y: 100 });
shape._onFabricMouseMove(fEvent);
expect(shapeObj).toMatchObject({ originX: 'right', originY: 'top' });
});
it('should be set to "right" and "bottom" when the mouse direction is in 3th quadrant', () => {
jest.spyOn(canvas, 'getPointer').mockReturnValue({ x: 80, y: 80 });
shape._onFabricMouseMove(fEvent);
expect(shapeObj).toMatchObject({ originX: 'right', originY: 'bottom' });
});
it('should be set to "left" and "bottom" when the mouse direction is in 4th quadrant', () => {
jest.spyOn(canvas, 'getPointer').mockReturnValue({ x: 200, y: 80 });
shape._onFabricMouseMove(fEvent);
expect(shapeObj).toMatchObject({ originX: 'left', originY: 'bottom' });
});
});
describe('_onFabricMouseUp()', () => {
let startPoint;
beforeEach(() => {
shape.add('circle', { left: 100, top: 100 });
[shapeObj] = canvas.getObjects();
shape._shapeObj = shapeObj;
});
it('should be the same as start point when the drawing shape is in 1th quadrant', () => {
jest.spyOn(canvas, 'getPointer').mockReturnValue({ x: 200, y: 120 });
startPoint = shapeObj.getPointByOrigin('left', 'top');
shape._onFabricMouseMove(fEvent);
shape._onFabricMouseUp();
expect(shapeObj.getPointByOrigin('left', 'top')).toEqual(startPoint);
});
it('should be the same as start point when the drawing shape is in 2th quadrant', () => {
jest.spyOn(canvas, 'getPointer').mockReturnValue({ x: 80, y: 120 });
startPoint = shapeObj.getPointByOrigin('right', 'top');
shape._onFabricMouseMove(fEvent);
shape._onFabricMouseUp();
expect(shapeObj.getPointByOrigin('right', 'top')).toEqual(startPoint);
});
it('should be the same as start point when the drawing shape is in 3th quadrant', () => {
jest.spyOn(canvas, 'getPointer').mockReturnValue({ x: 80, y: 80 });
startPoint = shapeObj.getPointByOrigin('right', 'bottom');
shape._onFabricMouseMove(fEvent);
shape._onFabricMouseUp();
expect(shapeObj.getPointByOrigin('right', 'bottom')).toEqual(startPoint);
});
it('should be the same as start point when the drawing shape is in 4th quadrant', () => {
jest.spyOn(canvas, 'getPointer').mockReturnValue({ x: 120, y: 80 });
startPoint = shapeObj.getPointByOrigin('left', 'bottom');
shape._onFabricMouseMove(fEvent);
shape._onFabricMouseUp();
expect(shapeObj.getPointByOrigin('left', 'bottom')).toEqual(startPoint);
});
});
it('should have the same "width" and "height" values when drawing the shape with mouse and the "isRegular" option set to true(x-axis)', () => {
shape.add('rect', { left: 0, top: 0 });
shape._withShiftKey = true;
[shapeObj] = canvas.getObjects();
shape._shapeObj = shapeObj;
jest.spyOn(canvas, 'getPointer').mockReturnValue({ x: 200, y: 100 });
shape._onFabricMouseMove(fEvent);
shape._onFabricMouseUp();
expect(shapeObj).toMatchObject({ width: 200, height: 200 });
});
it('should have the same "width" and "height" values when drawing the shape with mouse and the "isRegular" option set to true(y-axis)', () => {
shape.add('rect', { left: 0, top: 0 });
shape._withShiftKey = true;
[shapeObj] = canvas.getObjects();
shape._shapeObj = shapeObj;
jest.spyOn(canvas, 'getPointer').mockReturnValue({ x: 100, y: 200 });
shape._onFabricMouseMove(fEvent);
shape._onFabricMouseUp();
expect(shapeObj).toMatchObject({ width: 200, height: 200 });
});
});
|