2020-02-14 10:10:54 +08:00
|
|
|
import '../../../src/behavior';
|
|
|
|
import Behavior from '../../../src/behavior/behavior';
|
2019-12-10 20:44:43 +08:00
|
|
|
import { IBehavior } from '../../../src/interface/behavior';
|
2019-12-06 18:14:25 +08:00
|
|
|
|
2019-12-02 21:19:23 +08:00
|
|
|
describe('Behavior', () => {
|
2019-12-03 20:20:10 +08:00
|
|
|
it('register signle behavior', () => {
|
2019-12-06 19:27:32 +08:00
|
|
|
Behavior.registerBehavior('first-behavior', {
|
2019-12-02 21:19:23 +08:00
|
|
|
getEvents() {
|
|
|
|
return {
|
2020-02-14 10:10:54 +08:00
|
|
|
click: 'onClick',
|
|
|
|
};
|
2019-12-02 21:19:23 +08:00
|
|
|
},
|
2019-12-06 18:14:25 +08:00
|
|
|
onClick() {
|
2020-02-14 10:10:54 +08:00
|
|
|
console.log('trigger click');
|
2019-12-03 20:20:10 +08:00
|
|
|
},
|
|
|
|
shouldBegin() {
|
2020-02-14 10:10:54 +08:00
|
|
|
return false;
|
|
|
|
},
|
2019-12-03 20:20:10 +08:00
|
|
|
});
|
|
|
|
|
2020-02-14 10:10:54 +08:00
|
|
|
expect(Behavior.hasBehavior('first-behavior')).toBe(true);
|
|
|
|
expect(Behavior.hasBehavior('test')).toBe(false);
|
|
|
|
const BehaviorInstance = Behavior.getBehavior('first-behavior');
|
|
|
|
|
|
|
|
const instance = new BehaviorInstance();
|
|
|
|
const events = instance.getEvents();
|
|
|
|
expect(Object.keys(events)).toEqual(['click']);
|
2019-12-09 14:05:09 +08:00
|
|
|
expect(instance.shouldBegin()).toEqual(false);
|
2020-02-14 10:10:54 +08:00
|
|
|
});
|
2019-12-03 20:20:10 +08:00
|
|
|
|
2019-12-09 16:15:41 +08:00
|
|
|
it('register multiple behavior', () => {
|
2019-12-03 20:20:10 +08:00
|
|
|
Behavior.registerBehavior('first', {
|
|
|
|
getEvents() {
|
|
|
|
return {
|
|
|
|
click: 'onClick',
|
2020-02-14 10:10:54 +08:00
|
|
|
'edge:click': 'onEdgeClick',
|
|
|
|
contextmenu: 'onContextMenu',
|
|
|
|
};
|
2019-12-03 20:20:10 +08:00
|
|
|
},
|
|
|
|
onClick() {
|
2020-02-14 10:10:54 +08:00
|
|
|
return 'onclick';
|
2019-12-03 20:20:10 +08:00
|
|
|
},
|
|
|
|
onContextMenu() {
|
2020-02-14 10:10:54 +08:00
|
|
|
return 'onContextMenu';
|
|
|
|
},
|
2019-12-02 21:19:23 +08:00
|
|
|
});
|
|
|
|
|
2019-12-03 20:20:10 +08:00
|
|
|
Behavior.registerBehavior('second', {
|
|
|
|
getDefaultCfg() {
|
|
|
|
return {
|
|
|
|
style: {
|
2020-02-14 10:10:54 +08:00
|
|
|
fill: 'red',
|
|
|
|
},
|
|
|
|
};
|
2019-12-03 20:20:10 +08:00
|
|
|
},
|
|
|
|
getEvents() {
|
|
|
|
return {
|
2020-02-14 10:10:54 +08:00
|
|
|
drag: 'onDrag',
|
|
|
|
};
|
2019-12-03 20:20:10 +08:00
|
|
|
},
|
|
|
|
onDrag() {
|
2020-02-14 10:10:54 +08:00
|
|
|
return 'drag';
|
|
|
|
},
|
|
|
|
});
|
2019-12-03 20:20:10 +08:00
|
|
|
|
2020-02-14 10:10:54 +08:00
|
|
|
const FirstInstance = Behavior.getBehavior('first');
|
|
|
|
const SecondBehavior = Behavior.getBehavior('second');
|
|
|
|
const firstInstance = new FirstInstance();
|
|
|
|
const secondBehavior = new SecondBehavior();
|
2019-12-09 14:05:09 +08:00
|
|
|
expect(firstInstance).not.toBe(undefined);
|
2019-12-03 20:20:10 +08:00
|
|
|
expect(secondBehavior).not.toBe(undefined);
|
|
|
|
expect(Behavior.getBehavior('three')).toBe(undefined);
|
|
|
|
expect(Behavior.hasBehavior('first')).toBe(true);
|
|
|
|
expect(Behavior.hasBehavior('three')).toBe(false);
|
|
|
|
|
2019-12-18 16:39:19 +08:00
|
|
|
const config1 = firstInstance.getDefaultCfg();
|
|
|
|
expect(config1.style).toBe(undefined);
|
2019-12-03 20:20:10 +08:00
|
|
|
|
2020-02-14 10:10:54 +08:00
|
|
|
const events1 = firstInstance.getEvents();
|
2019-12-18 16:39:19 +08:00
|
|
|
expect(Object.keys(events1).length).toEqual(3);
|
2020-02-14 10:10:54 +08:00
|
|
|
expect(Object.keys(events1)).toEqual(['click', 'edge:click', 'contextmenu']);
|
2019-12-03 20:20:10 +08:00
|
|
|
|
2019-12-18 16:39:19 +08:00
|
|
|
const config = secondBehavior.getDefaultCfg();
|
|
|
|
expect(config.style.fill).toEqual('red');
|
|
|
|
expect(config.style.fill).not.toEqual('blue');
|
2020-02-14 10:10:54 +08:00
|
|
|
const drag = secondBehavior.onDrag();
|
|
|
|
expect(drag).toEqual('drag');
|
|
|
|
});
|
2020-01-02 14:47:41 +08:00
|
|
|
it('register behavior without object', () => {
|
2020-02-14 10:10:54 +08:00
|
|
|
expect(() => {
|
|
|
|
Behavior.registerBehavior('first', undefined);
|
|
|
|
}).toThrow();
|
|
|
|
});
|
|
|
|
});
|
2019-12-09 16:15:41 +08:00
|
|
|
describe('Default Behavior', () => {
|
|
|
|
it('drag-canvas', () => {
|
2020-02-14 10:10:54 +08:00
|
|
|
const DragCanvas = Behavior.getBehavior('drag-canvas');
|
|
|
|
expect(DragCanvas).not.toBe(undefined);
|
2019-12-09 16:15:41 +08:00
|
|
|
|
2020-02-14 10:10:54 +08:00
|
|
|
const dragCanvas: IBehavior = new DragCanvas();
|
|
|
|
const config = dragCanvas.getDefaultCfg();
|
2020-07-22 11:02:49 +08:00
|
|
|
expect(config).toEqual({ direction: 'both', enableOptimize: false });
|
2019-12-09 16:15:41 +08:00
|
|
|
|
2020-02-14 10:10:54 +08:00
|
|
|
const events = dragCanvas.getEvents();
|
|
|
|
const keys = Object.keys(events);
|
2020-07-02 14:30:56 +08:00
|
|
|
expect(keys.length).toBe(9);
|
2020-02-14 10:10:54 +08:00
|
|
|
});
|
|
|
|
});
|