From 1921e6ac9f67a853fd788eb3a8dce8a58e2ebc0f Mon Sep 17 00:00:00 2001 From: Alexey Plutalov Date: Wed, 3 May 2017 15:55:22 +0300 Subject: [PATCH] Add `createEvent` helper for specs --- .eslintrc | 3 ++- build/jasmine/helpers.js | 7 +++++++ build/karma/base.js | 5 +++++ spec/on.spec.js | 30 +++++++++--------------------- spec/once.spec.js | 40 ++++++++++++---------------------------- 5 files changed, 35 insertions(+), 50 deletions(-) create mode 100644 build/jasmine/helpers.js diff --git a/.eslintrc b/.eslintrc index ca564fc..6df1e62 100644 --- a/.eslintrc +++ b/.eslintrc @@ -26,7 +26,8 @@ }, "globals": { "useFixture": false, - "clearFixtures": false + "clearFixtures": false, + "createEvent": false }, "parser": "babel-eslint" } diff --git a/build/jasmine/helpers.js b/build/jasmine/helpers.js new file mode 100644 index 0000000..081905a --- /dev/null +++ b/build/jasmine/helpers.js @@ -0,0 +1,7 @@ +window.createEvent = function createEvent(type) { + const event = document.createEvent("HTMLEvents"); + + event.initEvent(type, true, true); + + return event; +}; diff --git a/build/karma/base.js b/build/karma/base.js index 7e2e207..3bf3477 100644 --- a/build/karma/base.js +++ b/build/karma/base.js @@ -16,6 +16,10 @@ module.exports = { pattern: "build/jasmine/fixtures.js", watched: process.env.CI !== "true" }, + { + pattern: "build/jasmine/helpers.js", + watched: process.env.CI !== "true" + }, { pattern: "src/index.js", watched: process.env.CI !== "true", @@ -30,6 +34,7 @@ module.exports = { preprocessors: { "build/jasmine/setup.js": ["rollup"], "build/jasmine/fixtures.js": ["rollup"], + "build/jasmine/helpers.js": ["rollup"], "spec/**/*.spec.js": ["rollup", "sourcemap"] }, diff --git a/spec/on.spec.js b/spec/on.spec.js index 658bac2..6394957 100644 --- a/spec/on.spec.js +++ b/spec/on.spec.js @@ -13,8 +13,7 @@ describe("on", () => { expect(listener).not.toHaveBeenCalled(); - const event = document.createEvent("HTMLEvents"); - event.initEvent("click", true, true); + const event = createEvent("click"); subject.dispatchEvent(event); @@ -32,8 +31,7 @@ describe("on", () => { expect(listener).not.toHaveBeenCalled(); - const listenedEvent = document.createEvent("HTMLEvents"); - listenedEvent.initEvent("click", true, true); + const listenedEvent = createEvent("click"); subject.dispatchEvent(listenedEvent); @@ -42,8 +40,7 @@ describe("on", () => { off(); - const event = document.createEvent("HTMLEvents"); - event.initEvent("click", true, true); + const event = createEvent("click"); subject.dispatchEvent(event); @@ -62,11 +59,8 @@ describe("on", () => { expect(listener).not.toHaveBeenCalled(); - const clickEvent = document.createEvent("HTMLEvents"); - clickEvent.initEvent("click", true, true); - - const customEvent = document.createEvent("HTMLEvents"); - customEvent.initEvent("click", true, true); + const clickEvent = createEvent("click"); + const customEvent = createEvent("custom-event"); subject.dispatchEvent(clickEvent); subject.dispatchEvent(customEvent); @@ -86,11 +80,8 @@ describe("on", () => { expect(listener).not.toHaveBeenCalled(); - const listenedClickEvent = document.createEvent("HTMLEvents"); - listenedClickEvent.initEvent("click", true, true); - - const listenedCustomEvent = document.createEvent("HTMLEvents"); - listenedCustomEvent.initEvent("custom-event", true, true); + const listenedClickEvent = createEvent("click"); + const listenedCustomEvent = createEvent("custom-event"); subject.dispatchEvent(listenedClickEvent); subject.dispatchEvent(listenedCustomEvent); @@ -101,11 +92,8 @@ describe("on", () => { off(); - const clickEvent = document.createEvent("HTMLEvents"); - clickEvent.initEvent("click", true, true); - - const customEvent = document.createEvent("HTMLEvents"); - customEvent.initEvent("custom-event", true, true); + const clickEvent = createEvent("click"); + const customEvent = createEvent("custom-event"); subject.dispatchEvent(clickEvent); subject.dispatchEvent(customEvent); diff --git a/spec/once.spec.js b/spec/once.spec.js index dbf6796..75a0226 100644 --- a/spec/once.spec.js +++ b/spec/once.spec.js @@ -13,8 +13,7 @@ describe("once", () => { expect(listener).not.toHaveBeenCalled(); - const event = document.createEvent("HTMLEvents"); - event.initEvent("click", true, true); + const event = createEvent("click"); subject.dispatchEvent(event); @@ -32,16 +31,14 @@ describe("once", () => { expect(listener).not.toHaveBeenCalled(); - const listenedEvent = document.createEvent("HTMLEvents"); - listenedEvent.initEvent("click", true, true); + const listenedEvent = createEvent("click"); subject.dispatchEvent(listenedEvent); expect(listener).toHaveBeenCalledTimes(1); expect(listener).toHaveBeenCalledWith(listenedEvent); - const event = document.createEvent("HTMLEvents"); - event.initEvent("click", true, true); + const event = createEvent("click"); subject.dispatchEvent(event); @@ -61,8 +58,7 @@ describe("once", () => { off(); - const event = document.createEvent("HTMLEvents"); - event.initEvent("click", true, true); + const event = createEvent("click"); subject.dispatchEvent(event); @@ -80,11 +76,8 @@ describe("once", () => { expect(listener).not.toHaveBeenCalled(); - const clickEvent = document.createEvent("HTMLEvents"); - clickEvent.initEvent("click", true, true); - - const customEvent = document.createEvent("HTMLEvents"); - customEvent.initEvent("custom-event", true, true); + const clickEvent = createEvent("click"); + const customEvent = createEvent("custom-event"); subject.dispatchEvent(clickEvent); subject.dispatchEvent(customEvent); @@ -104,11 +97,8 @@ describe("once", () => { expect(listener).not.toHaveBeenCalled(); - const listenedClickEvent = document.createEvent("HTMLEvents"); - listenedClickEvent.initEvent("click", true, true); - - const listenedCustomEvent = document.createEvent("HTMLEvents"); - listenedCustomEvent.initEvent("custom-event", true, true); + const listenedClickEvent = createEvent("click"); + const listenedCustomEvent = createEvent("custom-event"); subject.dispatchEvent(listenedClickEvent); subject.dispatchEvent(listenedCustomEvent); @@ -117,11 +107,8 @@ describe("once", () => { expect(listener).toHaveBeenCalledWith(listenedClickEvent); expect(listener).toHaveBeenCalledWith(listenedCustomEvent); - const clickEvent = document.createEvent("HTMLEvents"); - clickEvent.initEvent("click", true, true); - - const customEvent = document.createEvent("HTMLEvents"); - customEvent.initEvent("custom-event", true, true); + const clickEvent = createEvent("click"); + const customEvent = createEvent("custom-event"); subject.dispatchEvent(clickEvent); subject.dispatchEvent(customEvent); @@ -141,11 +128,8 @@ describe("once", () => { expect(listener).not.toHaveBeenCalled(); - const clickEvent = document.createEvent("HTMLEvents"); - clickEvent.initEvent("click", true, true); - - const customEvent = document.createEvent("HTMLEvents"); - customEvent.initEvent("custom-event", true, true); + const clickEvent = createEvent("click"); + const customEvent = createEvent("custom-event"); off();