From cd2459463607edde7f11d5fb27aab62334a63faa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Prokop?= Date: Mon, 21 Oct 2024 16:08:07 +0200 Subject: [PATCH] fix: support native `URLSearchParams` (#15) --- README.md | 1 + index.js | 1 + index.test.js | 10 ++++++++++ 3 files changed, 12 insertions(+) diff --git a/README.md b/README.md index 8b882f4..7c60512 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ This project "fixes" the following global APIs, overriding whichever polyfills t - `TextDecoderStream` - `structuredClone()` - `URL` +- `URLSearchParams` ## Getting started diff --git a/index.js b/index.js index f0a0e75..37a7cc2 100644 --- a/index.js +++ b/index.js @@ -18,6 +18,7 @@ class FixedJSDOMEnvironment extends JSDOMEnvironment { this.global.fetch = fetch this.global.structuredClone = structuredClone this.global.URL = URL + this.global.URLSearchParams = URLSearchParams } } diff --git a/index.test.js b/index.test.js index c7a6bfe..493b939 100644 --- a/index.test.js +++ b/index.test.js @@ -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) +})