-
-
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.
feat: add
HttpResponse.html()
static method (#2140)
Co-authored-by: Artem Zakharchenko <kettanaito@gmail.com>
- Loading branch information
1 parent
40b17fd
commit 8c5580a
Showing
5 changed files
with
113 additions
and
0 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
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,13 @@ | ||
import { http, HttpResponse } from 'msw' | ||
import { setupWorker } from 'msw/browser' | ||
|
||
const worker = setupWorker( | ||
http.get('/user', () => { | ||
return HttpResponse.html(` | ||
<p class="user" id="abc-123"> | ||
Jane Doe | ||
</p>`) | ||
}), | ||
) | ||
|
||
worker.start() |
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,17 @@ | ||
import { test, expect } from '../../../playwright.extend' | ||
|
||
test('responds with an HTML response body', async ({ loadExample, fetch }) => { | ||
await loadExample(require.resolve('./body-html.mocks.ts')) | ||
|
||
const res = await fetch('/user') | ||
const status = res.status() | ||
const headers = await res.allHeaders() | ||
const text = await res.text() | ||
|
||
expect(status).toBe(200) | ||
expect(headers['content-type']).toBe('text/html') | ||
expect(text).toEqual(` | ||
<p class="user" id="abc-123"> | ||
Jane Doe | ||
</p>`) | ||
}) |
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,34 @@ | ||
/** | ||
* @vitest-environment node | ||
*/ | ||
import { HttpResponse, http } from 'msw' | ||
import { setupServer } from 'msw/node' | ||
|
||
const server = setupServer( | ||
http.get('http://localhost/html', () => { | ||
return HttpResponse.html(` | ||
<p class="user" id="abc-123"> | ||
Jane Doe | ||
</p>`) | ||
}), | ||
) | ||
|
||
beforeAll(() => { | ||
server.listen() | ||
}) | ||
|
||
afterAll(() => { | ||
server.close() | ||
}) | ||
|
||
test('responds with an HTML response body', async () => { | ||
const res = await fetch('http://localhost/html') | ||
const text = await res.text() | ||
|
||
expect(res.status).toBe(200) | ||
expect(res.headers.get('content-type')).toBe('text/html') | ||
expect(text).toEqual(` | ||
<p class="user" id="abc-123"> | ||
Jane Doe | ||
</p>`) | ||
}) |