Skip to content

Commit

Permalink
Dont throw on error, but log error
Browse files Browse the repository at this point in the history
  • Loading branch information
damassi committed May 23, 2018
1 parent 5f25f44 commit aef0ce6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 26 deletions.
37 changes: 21 additions & 16 deletions src/loadComponents.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-env browser */
/* eslint no-console: off */
import { LOADABLE, LOADABLE_STATE } from './constants'
import * as componentTracker from './componentTracker'

Expand All @@ -18,21 +19,23 @@ function loadState(rootState) {
'loadable-component server modules:',
window[LOADABLE_STATE],
)
throw new Error(
`loadable-components: module "${
state.id
}" is not found, client and server modules are not sync. You are probably not using the same resolver on server and client.`,
)
const errorMessage = `loadable-components: module "${
state.id
}" is not found, client and server modules are not sync. You are probably not using the same resolver on server and client.`

console.error(errorMessage)
return Promise.reject(errorMessage)
}

const getLoadable = component[LOADABLE]

if (typeof getLoadable !== 'function') {
throw new Error(
`loadable-components: module "${
state.id
}" is not a loadable component, please verify your SSR setup`,
)
const errorMessage = `loadable-components: module "${
state.id
}" is not a loadable component, please verify your SSR setup`

console.error(errorMessage)
return Promise.reject(errorMessage)
}

return getLoadable()
Expand All @@ -44,18 +47,20 @@ function loadState(rootState) {

function loadComponents() {
if (typeof window === 'undefined') {
throw new Error(
console.error(
'loadable-components: `loadComponents` must be called client-side: `window` is undefined',
)
return Promise.reject()
}

const state = window[LOADABLE_STATE]
if (!state) {
throw new Error(
'loadable-components state not found. ' +
'You have a problem server-side. ' +
'Please verify that you have called `loadableState.getScriptTag()` server-side.',
)
const errorMessage = 'loadable-components state not found. ' +
'You have a problem server-side. ' +
'Please verify that you have called `loadableState.getScriptTag()` server-side.'

console.error(errorMessage)
return Promise.reject(errorMessage)
}

return loadState(state)
Expand Down
56 changes: 46 additions & 10 deletions src/loadComponents.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,57 @@ describe('loadComponents', () => {
}
})

it('should load all components', async () => {
await loadComponents()
expect(Component1.load).toHaveBeenCalled()
expect(Component2.load).toHaveBeenCalled()
expect(Component3.load).toHaveBeenCalled()
it('rejects when no LOADABLE_STATE', async () => {
global.console.error = jest.fn()
delete window[LOADABLE_STATE]

expect.assertions(2)
try {
await loadComponents()
} catch (error) {
expect(global.console.error).toHaveBeenCalled()
expect(global.console.error.mock.calls[0][0]).toEqual(error)
}
})

it('should handle no LOADABLE_STATE', async () => {
delete window[LOADABLE_STATE]
expect.assertions(1)
it('rejects when no component is found in componentTracker', async () => {
global.console.warn = jest.fn()
global.console.error = jest.fn()
window[LOADABLE_STATE] = {
children: [{ id: null } ]
}

expect.assertions(3)
try {
await loadComponents()
} catch (error) {
expect(global.console.warn).toHaveBeenCalled()
expect(global.console.error).toHaveBeenCalled()
expect(global.console.error.mock.calls[0][0]).toEqual(error)
}
})

it('rejects when found component is not a function', async () => {
global.console.error = jest.fn()
const BadComponent = 'not a function'
const badId = componentTracker.track(BadComponent, ['./BadComponent'])
window[LOADABLE_STATE] = {
children: [{ id: badId } ]
}

expect.assertions(2)
try {
await loadComponents()
} catch (err) {
expect(err.message).toMatch(/loadable-components state not found/)
} catch (error) {
expect(global.console.error).toHaveBeenCalled()
expect(global.console.error.mock.calls[0][0]).toEqual(error)
}
})

it('should load all components', async () => {
await loadComponents()
expect(Component1.load).toHaveBeenCalled()
expect(Component2.load).toHaveBeenCalled()
expect(Component3.load).toHaveBeenCalled()
})
})

0 comments on commit aef0ce6

Please sign in to comment.