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

104 lines
2.8 KiB
TypeScript
Raw Normal View History

2019-12-09 16:15:41 +08:00
import '../../../src/behavior'
2019-12-09 14:05:09 +08:00
import Behavior from '../../../src/behavior/behavior'
2019-12-09 16:15:41 +08:00
import { IBehavior } from '../../../types';
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 {
click: 'onClick'
}
},
onClick() {
console.log('trigger click')
2019-12-03 20:20:10 +08:00
},
shouldBegin() {
return false
}
});
expect(Behavior.hasBehavior('first-behavior')).toBe(true)
expect(Behavior.hasBehavior('test')).toBe(false)
const BehaviorInstance = Behavior.getBehavior('first-behavior')
2019-12-09 16:15:41 +08:00
2019-12-09 14:05:09 +08:00
const instance = new BehaviorInstance()
const events = instance.getEvents()
2019-12-03 20:20:10 +08:00
expect(Object.keys(events)).toEqual(['click'])
2019-12-09 14:05:09 +08:00
expect(instance.shouldBegin()).toEqual(false);
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',
"edge:click": 'onEdgeClick',
contextmenu: 'onContextMenu'
}
},
onClick() {
return 'onclick'
},
onContextMenu() {
return 'onContextMenu'
2019-12-02 21:19:23 +08:00
}
});
2019-12-03 20:20:10 +08:00
Behavior.registerBehavior('second', {
getDefaultCfg() {
return {
style: {
fill: 'red'
}
}
},
getEvents() {
return {
drag: 'onDrag'
}
},
onDrag() {
return 'drag'
}
})
2019-12-09 16:15:41 +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-09 14:05:09 +08:00
const config1 = firstInstance.getDefaultCfg();
2019-12-03 20:20:10 +08:00
expect(config1.style).toBe(undefined);
2019-12-09 14:05:09 +08:00
const events1 = firstInstance.getEvents()
2019-12-03 20:20:10 +08:00
expect(Object.keys(events1).length).toEqual(3);
expect(Object.keys(events1)).toEqual(['click', 'edge:click', 'contextmenu'])
const config = secondBehavior.getDefaultCfg();
expect(config.style.fill).toEqual('red');
expect(config.style.fill).not.toEqual('blue');
const drag = secondBehavior.onDrag()
expect(drag).toEqual('drag')
2019-12-02 21:19:23 +08:00
})
2019-12-09 16:15:41 +08:00
})
describe('Default Behavior', () => {
it('drag-canvas', () => {
const DragCanvas = Behavior.getBehavior('drag-canvas')
expect(DragCanvas).not.toBe(undefined)
const dragCanvas: IBehavior = new DragCanvas()
const config = dragCanvas.getDefaultCfg()
expect(config).toEqual({ direction: 'both' })
const events = dragCanvas.getEvents()
const keys = Object.keys(events)
expect(keys.length).toBe(7)
})
2019-12-02 21:19:23 +08:00
})