Browse Source

Extended getComponent: system availability

bubble
Kyle Shockey 7 years ago
parent
commit
88848d071b
2 changed files with 23 additions and 15 deletions
  1. +19
    -11
      src/core/system.js
  2. +4
    -4
      test/core/system/wrapComponent.js

+ 19
- 11
src/core/system.js View File

@@ -227,8 +227,17 @@ export default class Store {
} }


getComponents(component) { getComponents(component) {
if(typeof component !== "undefined")
const res = this.system.components[component]

if(Array.isArray(res)) {
return res.reduce((ori, wrapper) => {
return wrapper(ori, this.getSystem())
})
}
if(typeof component !== "undefined") {
return this.system.components[component] return this.system.components[component]
}

return this.system.components return this.system.components
} }


@@ -322,17 +331,16 @@ function systemExtend(dest={}, src={}) {
} }


// Wrap components // Wrap components
// Parses existing components in the system, injects new wrappers into the dest,
// and removes wrapComponents from the src so it doesn't make it into the final system
// Parses existing components in the system, and prepares them for wrapping via getComponents
if(src.wrapComponents) { if(src.wrapComponents) {
objMap(src.wrapComponents, (wrapper, key) => {
if(src.components && src.components[key]) {
// eslint-disable-next-line no-console
console.warn("Warning: providing and wrapping the same component simultaneously is not supported.")
}
if(dest.components[key]) {
dest.components[key] = wrapper(dest.components[key])
objMap(src.wrapComponents, (wrapperFn, key) => {
const ori = dest.components[key]
if(ori && Array.isArray(ori)) {
dest.components[key] = ori.concat([wrapperFn])
} else if(ori) {
dest.components[key] = [ori, wrapperFn]
} else {
dest.components[key] = null
} }
}) })




+ 4
- 4
test/core/system/wrapComponent.js View File

@@ -86,7 +86,7 @@ describe("wrapComponents", () => {
}) })
}) })


it.skip("should provide a reference to the system to the wrapper", function(done){
it("should provide a reference to the system to the wrapper", function(){


// Given // Given


@@ -116,7 +116,7 @@ describe("wrapComponents", () => {
wow: (OriginalComponent, system) => (props) => { wow: (OriginalComponent, system) => (props) => {
return <container> return <container>
<OriginalComponent {...props}></OriginalComponent> <OriginalComponent {...props}></OriginalComponent>
<OriginalComponent name="Wrapped"></OriginalComponent>
<div>{system.dogeSelectors.wow()}</div>
</container> </container>
} }
} }
@@ -125,7 +125,7 @@ describe("wrapComponents", () => {
}) })


// Then // Then
var Component = system.getSystem().getComponents("wow")
var Component = mySystem.getSystem().getComponents("wow")
const wrapper = render(<Component name="Normal" />) const wrapper = render(<Component name="Normal" />)


const container = wrapper.children().first() const container = wrapper.children().first()
@@ -134,6 +134,6 @@ describe("wrapComponents", () => {
const children = container.children() const children = container.children()
expect(children.length).toEqual(2) expect(children.length).toEqual(2)
expect(children.eq(0).text()).toEqual("Original component") expect(children.eq(0).text()).toEqual("Original component")
expect(children.eq(1).text()).toEqual("Wrapped component")
expect(children.eq(1).text()).toEqual("WOW much data")
}) })
}) })

Loading…
Cancel
Save