2019-12-09 14:05:09 +08:00
|
|
|
import Behavior from '../../../src/behavior/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 {
|
|
|
|
click: 'onClick'
|
|
|
|
}
|
|
|
|
},
|
2019-12-06 18:14:25 +08:00
|
|
|
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 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 14:05:09 +08:00
|
|
|
it.only('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 14:05:09 +08:00
|
|
|
const firstInstance = Behavior.getBehavior('first')
|
2019-12-03 20:20:10 +08:00
|
|
|
const secondBehavior = Behavior.getBehavior('second')
|
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();
|
|
|
|
console.log('config1.style', config1)
|
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()
|
|
|
|
console.log(events1, Object.keys(events1), events1)
|
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');
|
2019-12-09 14:05:09 +08:00
|
|
|
console.log('config.style', config.style)
|
2019-12-03 20:20:10 +08:00
|
|
|
|
|
|
|
const drag = secondBehavior.onDrag()
|
|
|
|
expect(drag).toEqual('drag')
|
2019-12-02 21:19:23 +08:00
|
|
|
})
|
|
|
|
})
|