g6/tests/unit/behavior/index-spec.ts

109 lines
3.1 KiB
TypeScript
Raw Normal View History

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-02 21:19:23 +08:00
describe('Behavior', () => {
2019-12-03 20:20:10 +08:00
it('register signle behavior', () => {
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
},
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);
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();
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
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');
});
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();
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);
expect(keys.length).toBe(9);
2020-02-14 10:10:54 +08:00
});
});