aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2020-04-08 08:47:16 +1000
committerUlrich-Matthias Schäfer <ulima.ums@googlemail.com>2020-04-08 08:47:16 +1000
commita4a532d5b886d1e59a96cee7453f3b2ffb8f9285 (patch)
treeaad52d48b9ff6872ed541d53049a75b4c528a74a /spec
parentd927f2d225d74fd3b3f41b298a19f6ba075702cf (diff)
downloadsvg.js-a4a532d5b886d1e59a96cee7453f3b2ffb8f9285.tar.gz
svg.js-a4a532d5b886d1e59a96cee7453f3b2ffb8f9285.zip
tests for EventTarget
Diffstat (limited to 'spec')
-rw-r--r--spec/spec/types/Base.js2
-rw-r--r--spec/spec/types/EventTarget.js132
2 files changed, 133 insertions, 1 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()
+ })
+ })
+})