From 99069e55ceec4810d7ded813d3cfa1471b22f89b Mon Sep 17 00:00:00 2001 From: Tushar Mathur Date: Tue, 6 Sep 2016 21:39:23 +0530 Subject: [PATCH] feat(core): add support for stopPropagation and preventDefault --- src/createWCProto.js | 7 +++++-- test/test.createWCProto.js | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/createWCProto.js b/src/createWCProto.js index 7571a98..66acb47 100644 --- a/src/createWCProto.js +++ b/src/createWCProto.js @@ -27,10 +27,13 @@ export default (virtualDOMPatcher, component) => { const {update, view, init, props = []} = component return { - __dispatchActions (type) { + __dispatchActions (type, options = {}) { if (!this.__handlers[type]) { - this.__handlers[type] = (params) => + this.__handlers[type] = (params) => { + if (options.preventDefault) params.preventDefault() + if (options.stopPropagation) params.stopPropagation() this.__store.dispatch({type, params}) + } } return this.__handlers[type] }, diff --git a/test/test.createWCProto.js b/test/test.createWCProto.js index ab0a3cd..2034e81 100644 --- a/test/test.createWCProto.js +++ b/test/test.createWCProto.js @@ -174,3 +174,26 @@ test('setProps', t => { {type: '@@prop/B', params: {a: 1, b: 2}} ]) }) +test('__dispatchActions({preventDefault: true})', t => { + const mockPatcher = createMockPatcher() + const mockEV = {preventDefault: spy()} + const attachShadow = () => '@ROOT' + const component = createMockComponent() + const wc = rwc.createWCProto(mockPatcher.patcher, component) + wc.attachShadow = attachShadow + wc.createdCallback() + wc.__dispatchActions('MOVE', {preventDefault: true})(mockEV) + t.true(mockEV.preventDefault.called) +}) +test('__dispatchActions({stopPropagation: true})', t => { + const mockPatcher = createMockPatcher() + const mockEV = {stopPropagation: spy()} + const attachShadow = () => '@ROOT' + const component = createMockComponent() + const wc = rwc.createWCProto(mockPatcher.patcher, component) + wc.attachShadow = attachShadow + wc.createdCallback() + wc.__dispatchActions('MOVE', {stopPropagation: true})(mockEV) + t.true(mockEV.stopPropagation.called) +}) +