Skip to content

Commit

Permalink
feat: attach static properties on load
Browse files Browse the repository at this point in the history
Closes #58
  • Loading branch information
gregberge committed Apr 5, 2018
1 parent 96333ca commit d383fab
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@
"standard-version": "^4.3.0"
},
"dependencies": {
"babel-plugin-syntax-dynamic-import": "^6.18.0"
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"hoist-non-react-statics": "^2.5.0"
},
"jest": {
"setupFiles": [
Expand Down
2 changes: 2 additions & 0 deletions src/__fixtures__/Dummy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ import React from 'react'

const Dummy = () => <div />

Dummy.dummyStaticProp = true

export default Dummy
7 changes: 7 additions & 0 deletions src/loadable.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable react/sort-comp */
import React from 'react'
import hoistNonReactStatics from 'hoist-non-react-statics'
import { LOADABLE } from './constants'
import resolveModuleDefault from './utils/resolveModuleDefault'
import * as componentTracker from './componentTracker'
Expand All @@ -26,6 +27,12 @@ function loadable(
.then(module => {
const Component = resolveModuleDefault(module)
LoadableComponent.Component = Component
hoistNonReactStatics(LoadableComponent, Component, {
Component: true,
loadingPromise: true,
load: true,
[LOADABLE]: true,
})
return Component
})
.catch(error => {
Expand Down
5 changes: 5 additions & 0 deletions src/loadable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ describe('#loadable', () => {
expect(wrapper.contains(<Dummy />)).toBe(true)
})

it('should hoist static variables', async () => {
await Loadable.load()
expect(Loadable.dummyStaticProp).toBe(true)
})

it('should not load it two times', async () => {
const wrapper = mount(<Loadable />)
expect(wrapper.find('EmptyComponent').exists()).toBe(true)
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2777,6 +2777,10 @@ hoist-non-react-statics@^2.3.0:
version "2.3.1"
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-2.3.1.tgz#343db84c6018c650778898240135a1420ee22ce0"

hoist-non-react-statics@^2.5.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-2.5.0.tgz#d2ca2dfc19c5a91c5a6615ce8e564ef0347e2a40"

home-or-tmp@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/home-or-tmp/-/home-or-tmp-2.0.0.tgz#e36c3f2d2cae7d746a857e38d18d5f32a7882db8"
Expand Down

0 comments on commit d383fab

Please sign in to comment.