Skip to content

Commit

Permalink
Add createEvent helper for specs
Browse files Browse the repository at this point in the history
  • Loading branch information
demiazz committed May 3, 2017
1 parent 9e878c3 commit 1921e6a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 50 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
},
"globals": {
"useFixture": false,
"clearFixtures": false
"clearFixtures": false,
"createEvent": false
},
"parser": "babel-eslint"
}
7 changes: 7 additions & 0 deletions build/jasmine/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
window.createEvent = function createEvent(type) {
const event = document.createEvent("HTMLEvents");

event.initEvent(type, true, true);

return event;
};
5 changes: 5 additions & 0 deletions build/karma/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"]
},

Expand Down
30 changes: 9 additions & 21 deletions spec/on.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -42,8 +40,7 @@ describe("on", () => {

off();

const event = document.createEvent("HTMLEvents");
event.initEvent("click", true, true);
const event = createEvent("click");

subject.dispatchEvent(event);

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
40 changes: 12 additions & 28 deletions spec/once.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -61,8 +58,7 @@ describe("once", () => {

off();

const event = document.createEvent("HTMLEvents");
event.initEvent("click", true, true);
const event = createEvent("click");

subject.dispatchEvent(event);

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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();

Expand Down

0 comments on commit 1921e6a

Please sign in to comment.