Skip to content

Commit

Permalink
fix: support native URLSearchParams (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiri-prokop-pb authored Oct 21, 2024
1 parent 31c8fcc commit cd24594
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ This project "fixes" the following global APIs, overriding whichever polyfills t
- `TextDecoderStream`
- `structuredClone()`
- `URL`
- `URLSearchParams`

## Getting started

Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class FixedJSDOMEnvironment extends JSDOMEnvironment {
this.global.fetch = fetch
this.global.structuredClone = structuredClone
this.global.URL = URL
this.global.URLSearchParams = URLSearchParams
}
}

Expand Down
10 changes: 10 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,13 @@ test('exposes "URL"', () => {
expect(globalThis).toHaveProperty('URL')
expect(new URL('http://localhost')).toBeInstanceOf(BuiltinURL)
})

test('exposes "URLSearchParams" and makes it mockable', () => {
jest
.spyOn(URLSearchParams.prototype, 'has')
.mockImplementation((key) => key === 'mocked_flag')

expect(globalThis).toHaveProperty('URLSearchParams')
expect(new URL('http://localhost?other_non_mocked_flag').searchParams.has('other_non_mocked_flag')).toBe(false)
expect(new URL('http://localhost?other_non_mocked_flag').searchParams.has('mocked_flag')).toBe(true)
})

0 comments on commit cd24594

Please sign in to comment.