Skip to content

Commit

Permalink
Add integration test for Suspense and streaming (#31197)
Browse files Browse the repository at this point in the history
Adds a test to ensure that `<Suspense>` and streaming are working properly with `concurrentFeatures` enabled.

## Bug

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
  • Loading branch information
shuding committed Nov 9, 2021
1 parent a0a08ae commit 57b4134
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Suspense } from 'react'

let result
let promise
function Data() {
if (result) return result
if (!promise)
promise = new Promise((res) => {
setTimeout(() => {
result = 'next_streaming_data'
res()
}, 500)
})
throw promise
}

export default function Page() {
return (
<Suspense fallback="next_streaming_fallback">
<Data />
</Suspense>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,40 @@ async function runBasicTests(context) {
expect(html).toContain('bar.server.js:')
expect(html).toContain('foo.client')
})

it('should support streaming', async () => {
await fetchViaHTTP(context.appPort, '/streaming', null, {}).then(
async (response) => {
let result = ''
let gotFallback = false
let gotData = false

await new Promise((resolve) => {
response.body.on('data', (chunk) => {
result += chunk.toString()

gotData = result.includes('next_streaming_data')
if (!gotFallback) {
gotFallback = result.includes('next_streaming_fallback')
if (gotFallback) {
expect(gotData).toBe(false)
}
}
})

response.body.on('end', () => resolve())
})

expect(gotFallback).toBe(true)
expect(gotData).toBe(true)
}
)

// Should end up with "next_streaming_data".
const browser = await webdriver(context.appPort, '/streaming')
const content = await browser.eval(`window.document.body.innerText`)
expect(content).toMatchInlineSnapshot('"next_streaming_data"')
})
}

function runSuite(suiteName, env, { runTests, before, after }) {
Expand Down

0 comments on commit 57b4134

Please sign in to comment.