-
-
Notifications
You must be signed in to change notification settings - Fork 517
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add runtime request handler test
- Loading branch information
1 parent
b11ba9b
commit 852870a
Showing
2 changed files
with
59 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { setupWorker, sse } from 'msw/browser' | ||
import { test, expect } from '../playwright.extend' | ||
|
||
declare namespace window { | ||
export const msw: { | ||
setupWorker: typeof setupWorker | ||
sse: typeof sse | ||
} | ||
} | ||
|
||
test('supports server-sent event handler overrides', async ({ | ||
loadExample, | ||
page, | ||
}) => { | ||
await loadExample(require.resolve('./sse.mocks.ts'), { | ||
skipActivation: true, | ||
}) | ||
|
||
const workerRef = await page.evaluateHandle(async () => { | ||
const { setupWorker, sse } = window.msw | ||
|
||
const worker = setupWorker( | ||
sse('http://localhost/stream', ({ client }) => { | ||
client.send({ data: 'happy-path' }) | ||
}), | ||
) | ||
await worker.start() | ||
return worker | ||
}) | ||
|
||
await page.evaluate((worker) => { | ||
const { sse } = window.msw | ||
|
||
worker.use( | ||
// Adding this runtime request handler will make it | ||
// take precedence over the happy path handler above. | ||
sse('http://localhost/stream', ({ client }) => { | ||
// Queue the data for the next tick to rule out | ||
// the happy path handler from executing. | ||
queueMicrotask(() => { | ||
client.send({ data: 'override' }) | ||
}) | ||
}), | ||
) | ||
}, workerRef) | ||
|
||
const message = await page.evaluate(() => { | ||
return new Promise((resolve, reject) => { | ||
const source = new EventSource('http://localhost/stream') | ||
source.addEventListener('message', (event) => { | ||
resolve(event.data) | ||
}) | ||
source.onerror = reject | ||
}) | ||
}) | ||
|
||
expect(message).toBe('override') | ||
}) |