Skip to content

Commit d32c74c

Browse files
damassigregberge
authored andcommitted
fix: fix error handling in loadComponents (#87)
1 parent 48303ba commit d32c74c

File tree

2 files changed

+52
-29
lines changed

2 files changed

+52
-29
lines changed

src/loadComponents.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,26 @@ function loadState(rootState) {
1010
const component = componentTracker.get(state.id)
1111

1212
if (!component) {
13-
console.warn(
13+
console.warn( // eslint-disable-line
1414
'loadable-component client modules:',
1515
componentTracker.getAll(),
1616
)
17-
console.warn(
17+
console.warn( // eslint-disable-line
1818
'loadable-component server modules:',
1919
window[LOADABLE_STATE],
2020
)
21-
throw new Error(
22-
`loadable-components: module "${
23-
state.id
24-
}" is not found, client and server modules are not sync. You are probably not using the same resolver on server and client.`,
25-
)
21+
22+
return Promise.reject(new Error(`loadable-components: module "${
23+
state.id
24+
}" is not found, client and server modules are not sync. You are probably not using the same resolver on server and client.`))
2625
}
2726

2827
const getLoadable = component[LOADABLE]
2928

3029
if (typeof getLoadable !== 'function') {
31-
throw new Error(
32-
`loadable-components: module "${
33-
state.id
34-
}" is not a loadable component, please verify your SSR setup`,
35-
)
30+
return Promise.reject(new Error(`loadable-components: module "${
31+
state.id
32+
}" is not a loadable component, please verify your SSR setup`))
3633
}
3734

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

4542
function loadComponents() {
4643
if (typeof window === 'undefined') {
47-
throw new Error(
48-
'loadable-components: `loadComponents` must be called client-side: `window` is undefined',
49-
)
44+
return Promise.reject(new Error('loadable-components: `loadComponents` must ' +
45+
'be called client-side: `window` is undefined'))
5046
}
5147

5248
const state = window[LOADABLE_STATE]
5349
if (!state) {
54-
throw new Error(
55-
'loadable-components state not found. ' +
56-
'You have a problem server-side. ' +
57-
'Please verify that you have called `loadableState.getScriptTag()` server-side.',
58-
)
50+
return Promise.reject(new Error('loadable-components state not found. ' +
51+
'You have a problem server-side. ' +
52+
'Please verify that you have called `loadableState.getScriptTag()` server-side.'))
5953
}
6054

6155
return loadState(state)

src/loadComponents.test.js

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ describe('loadComponents', () => {
1010
let Component3
1111

1212
beforeEach(() => {
13+
global.console.warn = jest.fn() // hide logs
1314
Component1 = loadable(async () => () => null)
1415
jest.spyOn(Component1, 'load')
1516
Component2 = loadable(async () => () => null)
@@ -24,21 +25,49 @@ describe('loadComponents', () => {
2425
}
2526
})
2627

27-
it('should load all components', async () => {
28-
await loadComponents()
29-
expect(Component1.load).toHaveBeenCalled()
30-
expect(Component2.load).toHaveBeenCalled()
31-
expect(Component3.load).toHaveBeenCalled()
28+
it('rejects when no LOADABLE_STATE', async () => {
29+
delete window[LOADABLE_STATE]
30+
31+
expect.assertions(1)
32+
try {
33+
await loadComponents()
34+
} catch (error) {
35+
expect(error.message).toMatch(/loadable-components state not found/)
36+
}
3237
})
3338

34-
it('should handle no LOADABLE_STATE', async () => {
35-
delete window[LOADABLE_STATE]
39+
it('rejects when no component is found in componentTracker', async () => {
40+
window[LOADABLE_STATE] = {
41+
children: [{ id: null } ]
42+
}
43+
3644
expect.assertions(1)
45+
try {
46+
await loadComponents()
47+
} catch (error) {
48+
expect(error.message).toMatch(/loadable-components: module "null" is not found/)
49+
}
50+
})
3751

52+
it('rejects when found component is not a function', async () => {
53+
const BadComponent = -1
54+
const badId = componentTracker.track(BadComponent, ['./BadComponent'])
55+
window[LOADABLE_STATE] = {
56+
children: [{ id: badId } ]
57+
}
58+
59+
expect.assertions(1)
3860
try {
3961
await loadComponents()
40-
} catch (err) {
41-
expect(err.message).toMatch(/loadable-components state not found/)
62+
} catch (error) {
63+
expect(error.message).toMatch(/loadable-components: module ".\/BadComponent" is not a loadable component/)
4264
}
4365
})
66+
67+
it('should load all components', async () => {
68+
await loadComponents()
69+
expect(Component1.load).toHaveBeenCalled()
70+
expect(Component2.load).toHaveBeenCalled()
71+
expect(Component3.load).toHaveBeenCalled()
72+
})
4473
})

0 commit comments

Comments
 (0)