diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2020-04-08 08:47:16 +1000 |
---|---|---|
committer | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2020-04-08 08:47:16 +1000 |
commit | a4a532d5b886d1e59a96cee7453f3b2ffb8f9285 (patch) | |
tree | aad52d48b9ff6872ed541d53049a75b4c528a74a | |
parent | d927f2d225d74fd3b3f41b298a19f6ba075702cf (diff) | |
download | svg.js-a4a532d5b886d1e59a96cee7453f3b2ffb8f9285.tar.gz svg.js-a4a532d5b886d1e59a96cee7453f3b2ffb8f9285.zip |
tests for EventTarget
-rw-r--r-- | spec/spec/types/Base.js | 2 | ||||
-rw-r--r-- | spec/spec/types/EventTarget.js | 132 | ||||
-rw-r--r-- | src/elements/Dom.js | 2 | ||||
-rw-r--r-- | src/types/EventTarget.js | 13 |
4 files changed, 138 insertions, 11 deletions
diff --git a/spec/spec/types/Base.js b/spec/spec/types/Base.js index f0cdfaa..9a5cf16 100644 --- a/spec/spec/types/Base.js +++ b/spec/spec/types/Base.js @@ -5,7 +5,7 @@ import Base from '../../../src/types/Base.js' const { any } = jasmine describe('Base.js', () => { - it('holds the base class', () => { + it('creates a new object of type Base', () => { expect(new Base()).toEqual(any(Base)) }) }) diff --git a/spec/spec/types/EventTarget.js b/spec/spec/types/EventTarget.js new file mode 100644 index 0000000..cfc7f02 --- /dev/null +++ b/spec/spec/types/EventTarget.js @@ -0,0 +1,132 @@ +/* globals describe, expect, it, spyOn, jasmine */ + +import { + EventTarget +} from '../../../src/main.js' +import { getWindow } from '../../../src/utils/window.js' + +const { any, objectContaining, createSpy } = jasmine + +const event = (name) => { + const CustomEvent = getWindow().CustomEvent + return new CustomEvent(name) +} + +describe('EventTarget.js', () => { + describe('()', () => { + it('creates a new object of type EventTarget', () => { + expect(new EventTarget()).toEqual(any(EventTarget)) + }) + }) + + describe('addEventListener()', () => { + it('is a noop', () => { + const target = new EventTarget() + const frozen = Object.freeze(target) + frozen.addEventListener() + }) + }) + + describe('dispatch()', () => { + it('evemtually calls dispatchEvent on the target and returns the event', () => { + const target = new EventTarget() + const spy = spyOn(target, 'dispatchEvent').and.callThrough() + const options = { cancelable: false } + const event = target.dispatch('bla', 'foo', options) + expect(spy).toHaveBeenCalledWith(event) + expect(event).toEqual(objectContaining({ type: 'bla', detail: 'foo', cancelable: false })) + }) + }) + + describe('dispatchEvent()', () => { + it('returns true if no events are bound', () => { + const target = new EventTarget() + expect(target.dispatchEvent(event('event'))).toBe(true) + }) + + it('calls the handler for a bound event', () => { + const target = new EventTarget() + const spy = createSpy('event') + const ev = event('event') + target.on('event', spy) + const ret = target.dispatchEvent(ev) + expect(spy).toHaveBeenCalledWith(ev) + expect(ret).toBe(!ev.defaultPrevented) + }) + + it('returns negative default prevented', () => { + const target = new EventTarget() + const ev = event('event') + ev.preventDefault() + const ret = target.dispatchEvent(ev) + expect(ret).toBe(!ev.defaultPrevented) + }) + }) + + describe('fire()', () => { + it('calls dispatch and returns the element', () => { + const target = new EventTarget() + const spy = spyOn(target, 'dispatch') + expect(target.fire('event', 'foo', 'bar')).toBe(target) + expect(spy).toHaveBeenCalledWith('event', 'foo', 'bar') + }) + }) + + describe('getEventHolder()', () => { + it('returns itself', () => { + const target = new EventTarget() + expect(target.getEventHolder()).toBe(target) + }) + }) + + describe('getEventTarget()', () => { + it('returns itself', () => { + const target = new EventTarget() + expect(target.getEventTarget()).toBe(target) + }) + }) + + describe('off()', () => { + it('returns itself', () => { + const target = new EventTarget() + const spy = createSpy() + expect(target.on('event', spy)).toBe(target) + }) + + it('removes an event binding from the target', () => { + const target = new EventTarget() + const spy = createSpy() + target.on('event', spy) + target.dispatch('event') + expect(spy.calls.count()).toBe(1) + target.off('event', spy) + target.dispatch('event') + expect(spy.calls.count()).toBe(1) + }) + }) + + describe('on()', () => { + it('returns itself', () => { + const target = new EventTarget() + const spy = createSpy() + expect(target.off('event', spy)).toBe(target) + }) + + it('adds an event binding to the target', () => { + const target = new EventTarget() + const spy = createSpy() + expect(spy.calls.count()).toBe(0) + target.on('event', spy) + target.dispatch('event') + expect(spy.calls.count()).toBe(1) + }) + }) + + describe('removeEventListener()', () => { + it('is a noop', () => { + const target = new EventTarget() + const frozen = Object.freeze(target) + frozen.removeEventListener() + }) + }) +}) diff --git a/src/elements/Dom.js b/src/elements/Dom.js index c9e6953..c67dd18 100644 --- a/src/elements/Dom.js +++ b/src/elements/Dom.js @@ -17,7 +17,7 @@ import attr from '../modules/core/attr.js' export default class Dom extends EventTarget { constructor (node, attrs) { - super(node) + super() this.node = node this.type = node.nodeName diff --git a/src/types/EventTarget.js b/src/types/EventTarget.js index b4a137a..6889605 100644 --- a/src/types/EventTarget.js +++ b/src/types/EventTarget.js @@ -3,15 +3,10 @@ import { register } from '../utils/adopter.js' import Base from './Base.js' export default class EventTarget extends Base { - constructor ({ events = {} } = {}) { - super() - this.events = events - } - addEventListener () {} - dispatch (event, data) { - return dispatch(this, event, data) + dispatch (event, data, options) { + return dispatch(this, event, data, options) } dispatchEvent (event) { @@ -30,8 +25,8 @@ export default class EventTarget extends Base { } // Fire given event - fire (event, data) { - this.dispatch(event, data) + fire (event, data, options) { + this.dispatch(event, data, options) return this } |