Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On client-side init error return rejected promise rather than throw #87

Merged
merged 1 commit into from
May 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 14 additions & 20 deletions src/loadComponents.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,26 @@ function loadState(rootState) {
const component = componentTracker.get(state.id)

if (!component) {
console.warn(
console.warn( // eslint-disable-line
'loadable-component client modules:',
componentTracker.getAll(),
)
console.warn(
console.warn( // eslint-disable-line
'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.`,
)

return Promise.reject(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 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`,
)
return Promise.reject(new Error(`loadable-components: module "${
state.id
}" is not a loadable component, please verify your SSR setup`))
}

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

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

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.',
)
return Promise.reject(new Error('loadable-components state not found. ' +
'You have a problem server-side. ' +
'Please verify that you have called `loadableState.getScriptTag()` server-side.'))
}

return loadState(state)
Expand Down
47 changes: 38 additions & 9 deletions src/loadComponents.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('loadComponents', () => {
let Component3

beforeEach(() => {
global.console.warn = jest.fn() // hide logs
Component1 = loadable(async () => () => null)
jest.spyOn(Component1, 'load')
Component2 = loadable(async () => () => null)
Expand All @@ -24,21 +25,49 @@ 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 () => {
delete window[LOADABLE_STATE]

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

it('should handle no LOADABLE_STATE', async () => {
delete window[LOADABLE_STATE]
it('rejects when no component is found in componentTracker', async () => {
window[LOADABLE_STATE] = {
children: [{ id: null } ]
}

expect.assertions(1)
try {
await loadComponents()
} catch (error) {
expect(error.message).toMatch(/loadable-components: module "null" is not found/)
}
})

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

expect.assertions(1)
try {
await loadComponents()
} catch (err) {
expect(err.message).toMatch(/loadable-components state not found/)
} catch (error) {
expect(error.message).toMatch(/loadable-components: module ".\/BadComponent" is not a loadable component/)
}
})

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