Skip to content

Commit

Permalink
refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiroki Osame committed Jul 7, 2019
1 parent 29807ff commit 3b1d32a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 30 deletions.
2 changes: 1 addition & 1 deletion dist/PseudoWindow.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 24 additions & 28 deletions src/PseudoWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,40 @@ export default {
},

mounted() {
this.bindEventListeners();
// Bind events
Object.entries(this.$listeners).forEach(($l) => {
const e = this.normalizeEvent(
this.document ? window.document : window,
$l[0], // event name
$l[1], // event handler
);
e.target.addEventListener(e.name, e.handler, e.opts);
this.handlers.push(e);
});
},

destroyed() {
this.unbindEventListeners();
// Unbind events
while (this.handlers.length) {
const e = this.handlers.shift();
e.target.removeEventListener(e.name, e.handler, e.opts);
}
},

methods: {
bindEventListeners() {
const { $listeners } = this;
for (const event in $listeners) {
if (!$listeners.hasOwnProperty(event)) {
continue;
}
const e = this.normalizeEvent(
this.document ? window.document : window,
event,
$listeners[event],
);
e.target.addEventListener(e.name, e.handler, e.opts);
this.handlers.push(e);
}
},

unbindEventListeners() {
while (this.handlers.length) {
const e = this.handlers.shift();
e.target.removeEventListener(e.name, e.handler, e.opts);
}
},
// https://github.com/vuejs/vue/blob/6fe07ebf5ab3fea1860c59fe7cdd2ec1b760f9b0/src/core/vdom/helpers/update-listeners.js#L14
normalizeEvent(target, rawName, handler) {
let name = rawName;

normalizeEvent(target, name, handler) {
const passive = name.charAt(0) === '&';
const passive = name[0] === '&';
name = passive ? name.slice(1) : name;
const once = name.charAt(0) === '~'; // Prefixed last, checked first

const once = name[0] === '~'; // Prefixed last, checked first
name = once ? name.slice(1) : name;
const capture = name.charAt(0) === '!';

const capture = name[0] === '!';
name = capture ? name.slice(1) : name;

return {
target,
name,
Expand Down
29 changes: 28 additions & 1 deletion test/PseudoWindow.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ import { mount } from '@vue/test-utils';
import PseudoWindow from '@/PseudoWindow';

describe('Window', () => {
it('should not catch "click" event', () => {
const myMockFn = jest.fn();
const wrapper = mount(PseudoWindow, {
attachToDocument: true,
});

global.window.dispatchEvent(new Event('click'));
expect(myMockFn).not.toBeCalled();
expect(wrapper.element).toMatchSnapshot();
});

it('should catch "resize" event', () => {
const myMockFn = jest.fn();
const wrapper = mount(PseudoWindow, {
Expand All @@ -16,7 +27,7 @@ describe('Window', () => {
expect(wrapper.element).toMatchSnapshot();
});

it('should not catch "resize" event', () => {
it('should cleanup event handler and not catch "resize" event', () => {
const myMockFn = jest.fn();
const wrapper = mount(PseudoWindow, {
attachToDocument: true,
Expand Down Expand Up @@ -46,6 +57,22 @@ describe('Window', () => {
expect(myMockFn.mock.calls.length).toBe(1);
expect(wrapper.element).toMatchSnapshot();
});

it('should not catch "resize" event', () => {
const myMockFn = jest.fn();
const wrapper = mount(PseudoWindow, {
attachToDocument: true,
listeners: {
'~resize': myMockFn,
},
});

wrapper.destroy();

global.window.dispatchEvent(new Event('resize'));
expect(myMockFn).not.toBeCalled();
expect(wrapper.element).toMatchSnapshot();
});
});

describe('Document', () => {
Expand Down
4 changes: 4 additions & 0 deletions test/__snapshots__/PseudoWindow.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ exports[`Window should catch "resize" event 1`] = `<!---->`;

exports[`Window should catch "resize" event once 1`] = `<!---->`;

exports[`Window should cleanup event handler and not catch "resize" event 1`] = `<!---->`;

exports[`Window should not catch "click" event 1`] = `<!---->`;

exports[`Window should not catch "resize" event 1`] = `<!---->`;

0 comments on commit 3b1d32a

Please sign in to comment.