line.spec.js
1.96 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
/**
* @author NHN Ent. FE Development Team <dl_javascript@nhn.com>
* @fileoverview Test cases of "src/js/component/line.js"
*/
import fabric from 'fabric';
import $ from 'jquery';
import Graphics from '../src/js/graphics';
import Line from '../src/js/component/line';
import { eventNames } from '../src/js/consts';
describe('Line', () => {
let canvas, graphics, mockImage, line, fEvent;
beforeAll(() => {
graphics = new Graphics($('<canvas>')[0]);
canvas = graphics.getCanvas();
line = new Line(graphics);
});
beforeEach(() => {
mockImage = new fabric.Image();
graphics.setCanvasImage('mockImage', mockImage);
fEvent = {
e: {},
};
});
afterEach(() => {
canvas.forEachObject((obj) => {
canvas.remove(obj);
});
});
it('_onFabricMouseDown() should insert the line.', () => {
line._onFabricMouseDown(fEvent);
expect(canvas.getObjects().length).toEqual(1);
});
it('_onFabricMouseMove() should draw line located by mouse pointer.', () => {
line._line = new fabric.Line([10, 20, 10, 20]);
canvas.add(line._line);
spyOn(canvas, 'getPointer').and.returnValue({
x: 30,
y: 60,
});
expect(canvas.getObjects()[0].get('x2')).toEqual(10);
expect(canvas.getObjects()[0].get('y2')).toEqual(20);
line._onFabricMouseMove(fEvent);
expect(canvas.getObjects()[0].get('x2')).toEqual(30);
expect(canvas.getObjects()[0].get('y2')).toEqual(60);
});
it('end() should restore all drawing objects activated.', () => {
const path = new fabric.Path();
canvas.add(path);
line.start();
expect(canvas.getObjects()[0].get('evented')).toEqual(false);
line.end();
expect(canvas.getObjects()[0].get('evented')).toEqual(true);
});
it('"objectAdded" event should fire after the line is drawn.', () => {
spyOn(line, 'fire').and.callThrough();
line._onFabricMouseUp(fEvent);
expect(line.fire.calls.mostRecent().args[0]).toBe(eventNames.OBJECT_ADDED);
});
});