-
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(xhr): support environments with missing "location" (#482)
- Loading branch information
1 parent
e868e46
commit da1cc71
Showing
4 changed files
with
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* React Native-like environment for Vitest. | ||
*/ | ||
import type { Environment } from 'vitest' | ||
import { builtinEnvironments } from 'vitest/environments' | ||
|
||
export default <Environment>{ | ||
name: 'react-native-like', | ||
async setup(global, options) { | ||
const { teardown } = await builtinEnvironments.jsdom.setup(global, options) | ||
|
||
// React Native does not have the global "location" property. | ||
Reflect.deleteProperty(globalThis, 'window') | ||
Reflect.deleteProperty(globalThis, 'location') | ||
|
||
return { | ||
teardown, | ||
} | ||
}, | ||
} |
45 changes: 45 additions & 0 deletions
45
test/modules/XMLHttpRequest/regressions/xhr-location-undefined.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,45 @@ | ||
// @vitest-environment react-native-like | ||
import { it, expect, beforeAll, afterAll } from 'vitest' | ||
import { XMLHttpRequestInterceptor } from '../../../../src/interceptors/XMLHttpRequest' | ||
import { createXMLHttpRequest } from '../../../helpers' | ||
|
||
const interceptor = new XMLHttpRequestInterceptor() | ||
|
||
beforeAll(() => { | ||
interceptor.apply() | ||
}) | ||
|
||
afterAll(() => { | ||
interceptor.dispose() | ||
}) | ||
|
||
it('responds to a request with an absolute URL', async () => { | ||
interceptor.once('request', ({ request }) => { | ||
request.respondWith(new Response('Hello world')) | ||
}) | ||
|
||
const request = await createXMLHttpRequest((request) => { | ||
request.open('GET', 'https://example.com/resource') | ||
request.send() | ||
}) | ||
|
||
expect(request.status).toBe(200) | ||
expect(request.response).toBe('Hello world') | ||
}) | ||
|
||
it('throws on a request with a relative URL', async () => { | ||
const createRequest = () => { | ||
return createXMLHttpRequest((request) => { | ||
/** | ||
* @note Since the "location" is not present in React Native, | ||
* relative requests will throw (nothing to be relative to). | ||
* This is the correct behavior in React Native, where relative | ||
* requests are a no-op. | ||
*/ | ||
request.open('GET', '/relative/url') | ||
request.send() | ||
}) | ||
} | ||
|
||
expect(createRequest).toThrow('Invalid URL: /relative/url') | ||
}) |
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