From dc9e5ad9cd18333db91a5e7a97bf00a70fcecc52 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Tue, 5 Dec 2017 18:30:39 -0800 Subject: [PATCH] Fix a core system issue blocking preset-plugin wrapComponents cc: 3968 --- src/core/system.js | 13 +++++--- test/core/system/wrapComponent.js | 54 +++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/src/core/system.js b/src/core/system.js index e61f3841..22707ae4 100644 --- a/src/core/system.js +++ b/src/core/system.js @@ -336,17 +336,22 @@ function systemExtend(dest={}, src={}) { // Parses existing components in the system, and prepares them for wrapping via getComponents if(src.wrapComponents) { objMap(src.wrapComponents, (wrapperFn, key) => { - const ori = dest.components[key] + const ori = dest.components && dest.components[key] if(ori && Array.isArray(ori)) { dest.components[key] = ori.concat([wrapperFn]) + delete src.wrapComponents[key] } else if(ori) { dest.components[key] = [ori, wrapperFn] - } else { - dest.components[key] = null + delete src.wrapComponents[key] } }) - delete src.wrapComponents + if(!Object.keys(src.wrapComponents).length) { + // only delete wrapComponents if we've matched all of our wrappers to components + // this handles cases where the component to wrap may be out of our scope, + // but a higher recursive `combinePlugins` call will be able to handle it. + delete src.wrapComponents + } } diff --git a/test/core/system/wrapComponent.js b/test/core/system/wrapComponent.js index aa491a0a..e7306a96 100644 --- a/test/core/system/wrapComponent.js +++ b/test/core/system/wrapComponent.js @@ -136,4 +136,58 @@ describe("wrapComponents", () => { expect(children.eq(0).text()).toEqual("Original component") expect(children.eq(1).text()).toEqual("WOW much data") }) + + it("should wrap correctly when registering more plugins", function(){ + + // Given + + const mySystem = new System({ + plugins: [ + () => { + return { + statePlugins: { + doge: { + selectors: { + wow: () => () => { + return "WOW much data" + } + } + } + }, + components: { + wow: () =>
Original component
+ } + } + } + ] + }) + + mySystem.register([ + function() { + return { + // Wrap the component and use the system + wrapComponents: { + wow: (OriginalComponent, system) => (props) => { + return + +
{system.dogeSelectors.wow()}
+
+ } + } + } + } + ]) + + // Then + var Component = mySystem.getSystem().getComponents("wow") + const wrapper = render() + + const container = wrapper.children().first() + expect(container[0].name).toEqual("container") + + const children = container.children() + expect(children.length).toEqual(2) + expect(children.eq(0).text()).toEqual("Original component") + expect(children.eq(1).text()).toEqual("WOW much data") + }) })