mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
2a2a56ad2b
* Solve the "once-off" problem * Revert CallbacksInvoker * Update * Optimize
38 lines
949 B
TypeScript
38 lines
949 B
TypeScript
import { Eventify } from '../../cocos/core';
|
|
|
|
test('Eventify', () => {
|
|
class Base {
|
|
public _nickName: string;
|
|
|
|
constructor(nickName: string) {
|
|
this._nickName = nickName;
|
|
}
|
|
|
|
public getNickName() {
|
|
return this._nickName;
|
|
}
|
|
}
|
|
|
|
const hookOnOff = jest.fn(() => {});
|
|
|
|
class Derived extends Eventify(Base) {
|
|
public off(type: string, callback?: Function, target?: object) {
|
|
super.off(type, callback, target);
|
|
hookOnOff();
|
|
}
|
|
}
|
|
|
|
const nickName = 'Jack';
|
|
const derived = new Derived(nickName);
|
|
expect(derived.getNickName()).toEqual(nickName);
|
|
|
|
const eventName = 'test';
|
|
const eventCallback = jest.fn(() => {});
|
|
|
|
derived.off(eventName, eventCallback);
|
|
expect(hookOnOff).toBeCalledTimes(1);
|
|
|
|
derived.once(eventName, eventCallback);
|
|
derived.emit(eventName);
|
|
expect(hookOnOff).toBeCalledTimes(2);
|
|
}); |