zarm
Version:
基于 React 的移动端UI库
128 lines (126 loc) • 5.15 kB
JavaScript
/* eslint-disable @typescript-eslint/no-var-requires */
describe('events', function () {
var events;
beforeEach(function () {
jest.resetModules();
});
afterEach(function () {
jest.restoreAllMocks();
});
describe('supportsPassiveEvents', function () {
it('supportsPassiveEvents: true', function () {
jest.spyOn(window, 'addEventListener').mockImplementationOnce(function (_, __, options) {
var passive = options.passive;
console.info(passive);
});
events = require('../events').default;
expect(events.supportsPassiveEvents).toBeTruthy();
});
it('supportsPassiveEvents: false', function () {
jest.spyOn(window, 'addEventListener').mockImplementationOnce(function (_, __, useCapture) {
Boolean(useCapture);
});
events = require('../events').default;
expect(events.supportsPassiveEvents).toBeFalsy();
});
});
describe('#on', function () {
it('should call addEventListener if element supports addEventListener method', function () {
jest.spyOn(window, 'addEventListener').mockImplementationOnce(function (_, __, options) {
var passive = options.passive;
console.info(passive);
});
var button = {
addEventListener: jest.fn()
};
var callback = jest.fn();
events = require('../events').default;
events.on(button, 'click', callback);
expect(button.addEventListener).toBeCalledWith('click', callback, {
passive: false
});
});
it("should call attachEvent if element does't support addEventListener method", function () {
var button = {
attachEvent: jest.fn().mockImplementationOnce(function (_, callback) {
callback();
})
};
var callback = jest.fn();
events = require('../events').default;
events.on(button, 'click', callback);
expect(button.attachEvent).toBeCalledWith('onclick', expect.any(Function));
expect(callback).toBeCalled();
expect(callback.mock.instances[0]).toEqual(button);
});
});
describe('#off', function () {
it('should call removeEventListener if element supports removeEventListener method', function () {
jest.spyOn(window, 'addEventListener').mockImplementationOnce(function (_, __, useCapture) {
Boolean(useCapture);
});
var button = {
removeEventListener: jest.fn()
};
var callback = jest.fn();
events = require('../events').default;
events.off(button, 'click', callback);
expect(button.removeEventListener).toBeCalledWith('click', callback, false);
});
it('should call detachEvent if element does not supports removeEventListener method', function () {
var button = {
detachEvent: jest.fn().mockImplementationOnce(function (_, callback) {
callback();
})
};
var callback = jest.fn();
events = require('../events').default;
events.off(button, 'click', callback);
expect(button.detachEvent).toBeCalledWith('onclick', callback);
expect(callback).toBeCalled();
});
});
describe('#once', function () {
it('should remove existed event handler on event target before binding multiple events', function () {
var _mEvent$target, _mEvent$target2;
jest.spyOn(window, 'addEventListener').mockImplementationOnce(function (_, __, useCapture) {
Boolean(useCapture);
});
var mEvent = {
type: '',
target: {
removeEventListener: jest.fn()
}
};
var button = {};
events = require('../events').default;
var onSpy = jest.spyOn(events, 'on').mockImplementation(function (_, type, callback) {
Object.defineProperty(mEvent, 'type', {
value: type
});
callback(mEvent);
});
var callback = jest.fn();
events.once(button, 'click dblclick', callback);
expect(onSpy).toBeCalledWith(button, 'dblclick', expect.any(Function));
expect(onSpy).toBeCalledWith(button, 'click', expect.any(Function));
expect((_mEvent$target = mEvent.target) === null || _mEvent$target === void 0 ? void 0 : _mEvent$target.removeEventListener).toBeCalledWith('dblclick', expect.any(Function), false);
expect((_mEvent$target2 = mEvent.target) === null || _mEvent$target2 === void 0 ? void 0 : _mEvent$target2.removeEventListener).toBeCalledWith('click', expect.any(Function), false);
expect(callback).toBeCalledWith(mEvent);
});
it('should call callback after binding multiple events if the events are triggered', function () {
var mEvent = {};
var button = {};
events = require('../events').default;
var onSpy = jest.spyOn(events, 'on').mockImplementation(function (_, __, callback) {
callback(mEvent);
});
var callback = jest.fn();
events.once(button, 'click dblclick', callback);
expect(onSpy).toBeCalledWith(button, 'click', expect.any(Function));
expect(onSpy).toBeCalledWith(button, 'dblclick', expect.any(Function));
expect(callback).toBeCalledWith(mEvent);
});
});
});
;