From 26d8246352df412de1ed3f02f34bcd0e33072ab9 Mon Sep 17 00:00:00 2001 From: Christian Schulz Date: Wed, 15 Feb 2023 13:02:20 +0100 Subject: [PATCH] #755@patch: Replace properties by getter in Navigator. --- packages/happy-dom/src/navigator/Navigator.ts | 93 ++++++++++++++----- packages/happy-dom/test/window/Window.test.ts | 8 +- 2 files changed, 75 insertions(+), 26 deletions(-) diff --git a/packages/happy-dom/src/navigator/Navigator.ts b/packages/happy-dom/src/navigator/Navigator.ts index 540a0ef2a..31c2c3f49 100644 --- a/packages/happy-dom/src/navigator/Navigator.ts +++ b/packages/happy-dom/src/navigator/Navigator.ts @@ -11,76 +11,121 @@ import PluginArray from './PluginArray'; */ export default class Navigator { // False if setting a cookie will be ignored and true otherwise. - public readonly cookieEnabled: boolean = true; + public get cookieEnabled(): boolean { +return true; +} // TODO: Not implemented. - public readonly credentials: string = null; + public get credentials(): string { +return null; +} // TODO: Not implemented. - public readonly geolocation: string = null; + public get geolocation(): string { +return null; +} // String representing the preferred language of the user, usually the language of the browser UI. - public readonly language: string = 'en-US'; + public get language(): string { +return 'en-US'; +} // Array of string representing the user's preferred languages. - public readonly languages: string[] = ['en-US', 'en']; + public get languages(): string[] { +return ['en-US', 'en']; +} // TODO: Not implemented. - public readonly locks: string = null; + public get locks(): string { +return null; +} // Maximum number of simultaneous touch contact points are supported by the current device. - public readonly maxTouchPoints: number = 0; + public get maxTouchPoints(): number { +return 0; +} // Number of logical processors available to run threads on the user's computer. - public readonly hardwareConcurrency: number = 8; + public get hardwareConcurrency(): number { +return 8; +} // Browser app code name. - public readonly appCodeName: string = 'Mozilla'; + public get appCodeName(): string { +return 'Mozilla'; +} // Browser app name. - public readonly appName: string = 'Netscape'; + public get appName(): string { +return 'Netscape'; +} // Browser app version. - public readonly appVersion: string = '5.0 (Windows)'; + public get appVersion(): string { +return '5.0 (Windows)'; +} // Browser platform. - public readonly platform: string = 'Win32'; + public get platform(): string { +return 'Win32'; +} // Browser product. - public readonly product: string = 'Gecko'; + public get product(): string { +return 'Gecko'; +} // Browser product sub. - public readonly productSub: string = '20100101'; + public get productSub(): string { +return '20100101'; +} // Browser vendor. - public readonly vendor: string = ''; + public get vendor(): string { +return ''; +} // Browser vendor sub. - public readonly vendorSub: string = ''; + public get vendorSub(): string { +return ''; +} // Browser user agent. // "appCodeName/appVersion number (Platform; Security; OS-or-CPU; Localization; rv: revision-version-number) product/productSub Application-Name Application-Name-version". - public readonly userAgent: string = - 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0'; + public get userAgent(): string { + return 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0'; + } // Boolean value indicating whether the browser is working online. - public readonly onLine: boolean = true; + public get onLine(): boolean { +return true; +} // TODO: Not implemented. - public readonly permissions: string = null; + public get permissions(): string { +return null; +} // Boolean Indicates whether the user agent is controlled by automation. - public readonly webdriver: boolean = true; + public get webdriver(): boolean { +return true; +} // The user's Do Not Track setting, which indicates whether the user is requesting web sites and advertisers to not track them. // The value of the property reflects that of the DNT HTTP header, i.e. Values of "1", "0", or "unspecified". - public readonly doNotTrack: string = 'unspecified'; + public get doNotTrack(): string { +return 'unspecified'; +} // Browser mime-types. - public readonly mimeTypes: MimeTypeArray = new MimeTypeArray([]); + public get mimeTypes(): MimeTypeArray { +return new MimeTypeArray([]); +} // Browser plugins. - public readonly plugins: PluginArray = new PluginArray([]); + public get plugins(): PluginArray { +return new PluginArray([]); +} /** * Returns the object as a string. diff --git a/packages/happy-dom/test/window/Window.test.ts b/packages/happy-dom/test/window/Window.test.ts index 99304c204..3304ad719 100644 --- a/packages/happy-dom/test/window/Window.test.ts +++ b/packages/happy-dom/test/window/Window.test.ts @@ -185,7 +185,7 @@ describe('Window', () => { describe('get navigator()', () => { it('Returns an instance of Navigator with browser data.', () => { expect(window.navigator instanceof Navigator).toBe(true); - expect(window.navigator).toEqual({ + const referenceValues = { appCodeName: 'Mozilla', appName: 'Netscape', appVersion: '5.0 (Windows)', @@ -213,7 +213,11 @@ describe('Window', () => { vendor: '', vendorSub: '', webdriver: true - }); + }; + + for (const propertyKey in referenceValues) { + expect(window.navigator[propertyKey]).toEqual(referenceValues[propertyKey]); + } }); });