import { createElement, render } from 'preact'; import { setupScratch, teardown, supportsPassiveEvents } from '../_util/helpers'; /** @jsx createElement */ describe('event handling', () => { let scratch, proto; function fireEvent(on, type) { let e = document.createEvent('Event'); e.initEvent(type, true, true); on.dispatchEvent(e); } beforeEach(() => { scratch = setupScratch(); proto = document.createElement('div').constructor.prototype; sinon.spy(proto, 'addEventListener'); sinon.spy(proto, 'removeEventListener'); }); afterEach(() => { teardown(scratch); proto.addEventListener.restore(); proto.removeEventListener.restore(); }); it('should only register on* functions as handlers', () => { let click = () => {}, onclick = () => {}; render(
, scratch); expect(scratch.childNodes[0].attributes.length).to.equal(0); expect( proto.addEventListener ).to.have.been.calledOnce.and.to.have.been.calledWithExactly( 'click', sinon.match.func, false ); }); it('should only register truthy values as handlers', () => { function fooHandler() {} const falsyHandler = false; render(, scratch); expect( proto.addEventListener ).to.have.been.calledOnce.and.to.have.been.calledWithExactly( 'OtherClick', sinon.match.func, false ); expect(proto.addEventListener).not.to.have.been.calledWith('Click'); expect(proto.addEventListener).not.to.have.been.calledWith('click'); }); it('should support native event names', () => { let click = sinon.spy(), mousedown = sinon.spy(); render(