Skip to content

Commit

Permalink
fix(snapshot): fix snap usage
Browse files Browse the repository at this point in the history
Closes #40
  • Loading branch information
gregberge committed Feb 6, 2018
1 parent 0abf679 commit 3445bea
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ You need to instruct your snapshot solution to save state of `loadable-component
For example, to do this with [`react-snap`](https://github.com/stereobooster/react-snap) you can use following code:

```js
import { getState } from 'loadable-components/snap'
import { getState } from 'loadable-components'

// Set up for react-snap.
window.snapSaveState = () => getState()
Expand Down
8 changes: 5 additions & 3 deletions src/snap/index.js → src/getState.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-env browser */
/* eslint-disable import/prefer-default-export */
import { getAll as getAllComponents } from '../componentTracker'
import { LOADABLE_STATE } from '../constants'
import { getAll as getAllComponents } from './componentTracker'
import { LOADABLE_STATE } from './constants'

export function getState() {
function getState() {
const componentByIds = getAllComponents()
const children = Object.keys(componentByIds).reduce((ids, id) => {
const component = componentByIds[id]
Expand All @@ -12,3 +12,5 @@ export function getState() {
}, [])
return { [LOADABLE_STATE]: { children } }
}

export default getState
16 changes: 16 additions & 0 deletions src/getState.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { getState } from './'
import loadable from './loadable'

describe('#getState', () => {
it('should return only loaded components', () => {
const getComponent = jest.fn(() => import('./__fixtures__/Dummy'))
const Loadable = loadable(getComponent, {
modules: ['./__fixtures__/Dummy'],
})
expect(getState()).toEqual({ __LOADABLE_STATE__: { children: [] } })
Loadable.load()
expect(getState()).toEqual({
__LOADABLE_STATE__: { children: [{ id: './__fixtures__/Dummy' }] },
})
})
})
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as tracker from './componentTracker'

export { default as loadComponents } from './loadComponents'
export { default as getState } from './getState'
export { LOADABLE } from './constants'
export { default } from './loadable'
export const componentTracker = tracker
18 changes: 0 additions & 18 deletions src/snap/index.test.js

This file was deleted.

0 comments on commit 3445bea

Please sign in to comment.