-
-
Notifications
You must be signed in to change notification settings - Fork 534
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add isolated response test for iframes (#2205)
- Loading branch information
1 parent
1a0b39d
commit e52f10b
Showing
5 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
test/browser/msw-api/setup-worker/scenarios/iframe-isolated-response/app.html
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,2 @@ | ||
<iframe id="frame-one" src="./one.html"></iframe> | ||
<iframe id="frame-two" src="./two.html"></iframe> |
11 changes: 11 additions & 0 deletions
11
...msw-api/setup-worker/scenarios/iframe-isolated-response/iframe-isolated-response.mocks.ts
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,11 @@ | ||
import { http } from 'msw' | ||
import { setupWorker } from 'msw/browser' | ||
|
||
const worker = setupWorker() | ||
worker.start() | ||
|
||
// @ts-ignore | ||
window.msw = { | ||
worker, | ||
http, | ||
} |
51 changes: 51 additions & 0 deletions
51
.../msw-api/setup-worker/scenarios/iframe-isolated-response/iframe-isolated-response.test.ts
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,51 @@ | ||
import * as path from 'path' | ||
import * as express from 'express' | ||
import type { Frame, Page } from '@playwright/test' | ||
import { test, expect } from '../../../../playwright.extend' | ||
|
||
const staticMiddleware = (router: express.Router) => { | ||
router.use(express.static(__dirname)) | ||
} | ||
|
||
function getFrameById(id: string, page: Page): Frame { | ||
const frame = page | ||
.mainFrame() | ||
.childFrames() | ||
.find((frame) => frame.name() === id) | ||
|
||
if (!frame) { | ||
throw new Error(`Unable to find frame with id "${id}" on the page`) | ||
} | ||
|
||
return frame | ||
} | ||
|
||
test('responds with different responses for the same request based on request referrer (frame url)', async ({ | ||
loadExample, | ||
page, | ||
}) => { | ||
await loadExample(require.resolve('./iframe-isolated-response.mocks.ts'), { | ||
markup: path.resolve(__dirname, 'app.html'), | ||
beforeNavigation(compilation) { | ||
compilation.use(staticMiddleware) | ||
}, | ||
}) | ||
|
||
const frameOne = getFrameById('frame-one', page)! | ||
const frameTwo = getFrameById('frame-two', page)! | ||
|
||
await Promise.all([ | ||
frameOne.evaluate(() => window.request()), | ||
frameTwo.evaluate(() => window.request()), | ||
]) | ||
|
||
/** | ||
* @note Each frame is able to receive a unique response | ||
* because it uses the `isolatedResolver` utility. | ||
* It's IMPORTANT it runs in the frame's context. We cannot | ||
* ship that logic in MSW because MSW always runs in the | ||
* main thread (the top-level client, which is the parent). | ||
*/ | ||
await expect(frameOne.getByText('one')).toBeVisible() | ||
await expect(frameTwo.getByText('two')).toBeVisible() | ||
}) |
34 changes: 34 additions & 0 deletions
34
test/browser/msw-api/setup-worker/scenarios/iframe-isolated-response/one.html
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 @@ | ||
<script> | ||
const { worker, http } = window.parent.msw | ||
|
||
function isolatedResolver(resolver) { | ||
return (info) => { | ||
if (info.request.referrer !== location.href) { | ||
return | ||
} | ||
return resolver(info) | ||
} | ||
} | ||
|
||
worker.use( | ||
http.get( | ||
'./resource', | ||
isolatedResolver(({ request }) => { | ||
return new Response('one') | ||
}), | ||
), | ||
) | ||
|
||
window.request = () => { | ||
return fetch('./resource') | ||
.then((response) => response.text()) | ||
.then((data) => { | ||
const node = document.createElement('p') | ||
node.innerText = data | ||
document.body.appendChild(node) | ||
}) | ||
.catch((error) => console.error(error)) | ||
} | ||
|
||
document.addEventListener('click', window.request) | ||
</script> |
34 changes: 34 additions & 0 deletions
34
test/browser/msw-api/setup-worker/scenarios/iframe-isolated-response/two.html
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 @@ | ||
<script> | ||
const { worker, http } = window.parent.msw | ||
|
||
function isolatedResolver(resolver) { | ||
return (info) => { | ||
if (info.request.referrer !== location.href) { | ||
return | ||
} | ||
return resolver(info) | ||
} | ||
} | ||
|
||
worker.use( | ||
http.get( | ||
'./resource', | ||
isolatedResolver(({ request }) => { | ||
return new Response('two') | ||
}), | ||
), | ||
) | ||
|
||
window.request = () => { | ||
return fetch('./resource') | ||
.then((response) => response.text()) | ||
.then((data) => { | ||
const node = document.createElement('p') | ||
node.innerText = data | ||
document.body.appendChild(node) | ||
}) | ||
.catch((error) => console.error(error)) | ||
} | ||
|
||
document.addEventListener('click', window.request) | ||
</script> |