UNPKG

js-web-tools

Version:
82 lines (77 loc) 2.62 kB
"use strict"; var _domEventTrigger = _interopRequireDefault(require("../domEventTrigger")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function triggerAndCatch(element, event, options, fn) { // If no options are specified, assume the last param is the callback if (!fn) { fn = options; } element.addEventListener(event, fn, options); (0, _domEventTrigger["default"])(element, event, options); element.removeEventListener(event, fn); } describe('Trigger events', function () { describe('trigger HTMLEvents', function () { it('triggers single scroll event', function () { var fn = jest.fn(); triggerAndCatch(document.body, 'scroll', fn); expect(fn).toHaveBeenCalledTimes(1); }); }); describe('KeyboardEvent', function () { it('triggers keydown event', function () { var fn = jest.fn(); triggerAndCatch(document.body, 'keydown', fn); expect(fn).toHaveBeenCalledTimes(1); }); it('triggers keyup event', function () { var fn = jest.fn(); triggerAndCatch(document.body, 'keyup', fn); expect(fn).toHaveBeenCalledTimes(1); }); }); describe('MouseEvents', function () { it('triggers single click event', function () { var fn = jest.fn(); triggerAndCatch(document.body, 'click', fn); expect(fn).toHaveBeenCalledTimes(1); }); it('sets screenX/Y to clientX/Y if not present', function (done) { var options = { clientX: 10, clientY: 20 }; triggerAndCatch(document.body, 'click', options, function (params) { expect(params.clientX).toEqual(options.clientX); expect(params.clientY).toEqual(options.clientY); done(); }); }); it('keeps screenX/Y if not present', function (done) { var options = { clientX: 10, clientY: 20, screenX: 15, screenY: 25 }; triggerAndCatch(document.body, 'click', options, function (params) { expect(params.clientX).toEqual(options.clientX); expect(params.clientY).toEqual(options.clientY); expect(params.screenX).toEqual(options.screenX); expect(params.screenY).toEqual(options.screenY); done(); }); }); }); describe('CustomEvents', function () { it('triggers single custom event with detail', function (done) { triggerAndCatch(document.body, 'some-custom-event', { bubbles: true, detail: 'expected' }, function (evt) { expect(evt.detail).toEqual('expected'); done(); }); }); }); });