Skip to content

Commit

Permalink
fix: fix loadComponents without valid state
Browse files Browse the repository at this point in the history
Fix #34
  • Loading branch information
gregberge committed Feb 3, 2018
1 parent 8076cb3 commit 35f81a6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/loadComponents.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { LOADABLE, LOADABLE_STATE } from './constants'
import * as componentTracker from './componentTracker'

function loadState(rootState) {
if (!rootState.children) return null
if (!rootState.children) return Promise.resolve(null)

return Promise.all(
rootState.children.map(state => {
Expand Down Expand Up @@ -50,7 +50,7 @@ function loadComponents() {
}

const state = window[LOADABLE_STATE]
if (!state) {
if (!state || !state.children) {
throw new Error(
'loadable-components state not found. ' +
'You have a problem server-side. ' +
Expand Down
22 changes: 22 additions & 0 deletions src/loadComponents.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,26 @@ describe('loadComponents', () => {
expect(Component2.load).toHaveBeenCalled()
expect(Component3.load).toHaveBeenCalled()
})

it('should handle no LOADABLE_STATE', async () => {
delete window[LOADABLE_STATE]
expect.assertions(1)

try {
await loadComponents()
} catch (err) {
expect(err.message).toMatch(/loadable-components state not found/)
}
})

it('should handle no children', async () => {
delete window[LOADABLE_STATE].children
expect.assertions(1)

try {
await loadComponents()
} catch (err) {
expect(err.message).toMatch(/loadable-components state not found/)
}
})
})

0 comments on commit 35f81a6

Please sign in to comment.