Skip to content

Commit 5105fa2

Browse files
committed
Adds a test to ensure an async component is only resolved once.
1 parent e6ebcfd commit 5105fa2

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/__tests__/__snapshots__/integration.test.js.snap

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1-
exports[`integration works 1`] = `"<div><div><div><span>In Render.</span></div><div><span>In Boundary but outside an AsyncComponent, server render me!</span></div></div></div>"`;
1+
exports[`integration tests render server and client 1`] = `"<div><div><div><span>In Render.</span></div><div><span>In Boundary but outside an AsyncComponent, server render me!</span></div></div></div>"`;
22

3-
exports[`integration works 2`] = `
3+
exports[`integration tests render server and client 2`] = `
4+
Object {
5+
"resolved": Object {
6+
"1": true,
7+
"2": true,
8+
"4": true,
9+
},
10+
}
11+
`;
12+
13+
exports[`integration tests render server and client 3`] = `
414
<AsyncComponentProvider
515
execContext={
616
Object {
@@ -41,7 +51,7 @@ exports[`integration works 2`] = `
4151
</AsyncComponentProvider>
4252
`;
4353

44-
exports[`integration works 3`] = `
54+
exports[`integration tests render server and client 4`] = `
4555
<AsyncComponentProvider
4656
execContext={
4757
Object {

src/__tests__/integration.test.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import React from 'react'
44
import { renderToStaticMarkup } from 'react-dom/server'
55
import { mount } from 'enzyme'
6+
67
import { createAsyncComponent, withAsyncComponents } from '../'
78
import { STATE_IDENTIFIER } from '../constants'
89

@@ -58,7 +59,7 @@ const app = (
5859
</AsyncBob>
5960
)
6061

61-
describe('integration', () => {
62+
describe('integration tests', () => {
6263
afterEach(() => {
6364
delete global.window[STATE_IDENTIFIER]
6465
})
@@ -73,6 +74,7 @@ describe('integration', () => {
7374
.then(({ appWithAsyncComponents, state, STATE_IDENTIFIER: STATE_ID }) => {
7475
const serverString = renderToStaticMarkup(appWithAsyncComponents)
7576
expect(serverString).toMatchSnapshot()
77+
expect(state).toMatchSnapshot()
7678
// Restore the window and attach the state to the "window" for the client
7779
global.window = windowTemp
7880
global.window[STATE_ID] = state
@@ -97,4 +99,22 @@ describe('integration', () => {
9799
),
98100
)
99101
})
102+
103+
it('a component only gets resolved once', () => {
104+
let resolveCount = 0
105+
106+
const Foo = createAsyncComponent({
107+
resolve: () => {
108+
resolveCount += 1
109+
return () => <div>foo</div>
110+
},
111+
})
112+
113+
withAsyncComponents(<Foo />).then(({ appWithAsyncComponents }) => {
114+
mount(appWithAsyncComponents)
115+
expect(resolveCount).toEqual(1)
116+
mount(appWithAsyncComponents)
117+
expect(resolveCount).toEqual(1)
118+
})
119+
})
100120
})

0 commit comments

Comments
 (0)