Skip to content

Commit

Permalink
Added slot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Mar 24, 2020
1 parent c51b5db commit b30047e
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 61 deletions.
2 changes: 1 addition & 1 deletion dist/pseudo-window.js

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

5 changes: 1 addition & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ module.exports = {
'/node_modules/',
],
moduleNameMapper: {
'vue-pseudo-window': '<rootDir>/src/pseudo-window',
'vue-pseudo-window': '<rootDir>/dist/pseudo-window',
},
snapshotSerializers: [
'jest-serializer-vue',
],
};
9 changes: 0 additions & 9 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"eslint-plugin-import": "^2.20.1",
"husky": "^4.2.3",
"jest": "^25.1.0",
"jest-serializer-vue": "^2.0.2",
"rollup": "^2.1.0",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-filesize": "^6.2.1",
Expand Down
48 changes: 29 additions & 19 deletions src/pseudo-window.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const { hasOwnProperty } = Object.prototype;
const hasOwn = (obj, prop) => hasOwnProperty.call(obj, prop);

// From: https://github.com/vuejs/vue/blob/6fe07ebf5ab3fea1860c59fe7cdd2ec1b760f9b0/src/core/vdom/helpers/update-listeners.js#L14
const normalizeEvent = (M_target, M_name, M_handler) => {
const passive = M_name[0] === '&';
Expand All @@ -21,8 +24,26 @@ const normalizeEvent = (M_target, M_name, M_handler) => {
};
};

const { hasOwnProperty } = Object;
const hasOwn = (obj, prop) => hasOwnProperty.call(obj, prop);
const bindEventListners = ($listeners, element, handlers) => {
for (const eventName in $listeners) {
if (!hasOwn($listeners, eventName)) { continue; }
const eventHandler = $listeners[eventName];
const e = /* @__PURE__ */normalizeEvent(
element,
eventName,
eventHandler,
);
e.M_target.addEventListener(e.M_name, e.M_handler, e.M_opts);
handlers.push(e);
}
};

const unbindEventListeners = (handlers) => {
while (handlers.length) {
const e = handlers.shift();
e.M_target.removeEventListener(e.M_name, e.M_handler, e.M_opts);
}
};

export default {
name: 'pseudo-window',
Expand All @@ -41,25 +62,14 @@ export default {
},

mounted() {
// Bind events
for (const $k in this.$listeners) {
if (!hasOwn(this.$listeners, $k)) { continue; }
const $v = this.$listeners[$k];
const e = normalizeEvent(
this.document ? window.document : window,
$k, // event name
$v, // event handler
);
e.M_target.addEventListener(e.M_name, e.M_handler, e.M_opts);
this.M_handlers.push(e);
}
bindEventListners(
this.$listeners,
this.document ? window.document : window,
this.M_handlers,
);
},

destroyed() {
// Unbind events
while (this.M_handlers.length) {
const e = this.M_handlers.shift();
e.M_target.removeEventListener(e.M_name, e.M_handler, e.M_opts);
}
unbindEventListeners(this.M_handlers);
},
};
62 changes: 50 additions & 12 deletions test/PseudoWindow.spec.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,65 @@
import Vue from 'vue';
import { mount } from '@vue/test-utils';
import PseudoWindow from 'vue-pseudo-window';

describe('Slot', () => {
it('pass through nothing', () => {
const wrapper = mount({
template: '<pseudo-window />',
components: {
PseudoWindow,
},
});

expect(wrapper.html()).toBe('');
});

it('pass through one element', () => {
const wrapper = mount({
template: '<pseudo-window><div>Hello world</div></pseudo-window>',
components: {
PseudoWindow,
},
});

expect(wrapper.html()).toBe('<div>Hello world</div>');
});

it('should warn on multiple children', () => {
const warnHandler = jest.fn();
Vue.config.warnHandler = warnHandler;
const wrapper = mount({
template: `
<div>
<pseudo-window>
<div>Hello world</div>
<div>Hello world</div>
</pseudo-window>
</div>
`,
components: {
PseudoWindow,
},
});
expect(wrapper.html()).toBe('<div>\n <!---->\n</div>');
expect(warnHandler).toBeCalled();
});
});

describe('Window', () => {
it('should not catch "click" event', () => {
const myMockFn = jest.fn();
const wrapper = mount(PseudoWindow, {
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, {
mount(PseudoWindow, {
attachToDocument: true,
listeners: {
resize: myMockFn,
Expand All @@ -24,7 +68,6 @@ describe('Window', () => {

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

it('should cleanup event handler and not catch "resize" event', () => {
Expand All @@ -40,12 +83,11 @@ describe('Window', () => {

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

it('should catch "resize" event once', () => {
const myMockFn = jest.fn();
const wrapper = mount(PseudoWindow, {
mount(PseudoWindow, {
attachToDocument: true,
listeners: {
'~resize': myMockFn,
Expand All @@ -55,7 +97,6 @@ describe('Window', () => {
global.window.dispatchEvent(new Event('resize'));
global.window.dispatchEvent(new Event('resize'));
expect(myMockFn.mock.calls.length).toBe(1);
expect(wrapper.element).toMatchSnapshot();
});

it('should not catch "resize" event', () => {
Expand All @@ -71,14 +112,13 @@ describe('Window', () => {

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

describe('Document', () => {
it('should not catch "resize" on window', () => {
const myMockFn = jest.fn();
const wrapper = mount(PseudoWindow, {
mount(PseudoWindow, {
attachToDocument: true,
propsData: {
document: true,
Expand All @@ -91,12 +131,11 @@ describe('Document', () => {
global.window.dispatchEvent(new Event('resize'));

expect(myMockFn).not.toHaveBeenCalled();
expect(wrapper.element).toMatchSnapshot();
});

it('should catch "click" on document', () => {
const myMockFn = jest.fn();
const wrapper = mount(PseudoWindow, {
mount(PseudoWindow, {
attachToDocument: true,
propsData: {
document: true,
Expand All @@ -109,6 +148,5 @@ describe('Document', () => {
global.window.document.dispatchEvent(new Event('click'));

expect(myMockFn).toHaveBeenCalled();
expect(wrapper.element).toMatchSnapshot();
});
});
15 changes: 0 additions & 15 deletions test/__snapshots__/PseudoWindow.spec.js.snap

This file was deleted.

0 comments on commit b30047e

Please sign in to comment.