Skip to content

Commit

Permalink
capricorn86#755@patch: Replace properties by getter in Navigator.
Browse files Browse the repository at this point in the history
  • Loading branch information
CSchulz committed Feb 15, 2023
1 parent 0a633d4 commit 26d8246
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 26 deletions.
93 changes: 69 additions & 24 deletions packages/happy-dom/src/navigator/Navigator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 6 additions & 2 deletions packages/happy-dom/test/window/Window.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)',
Expand Down Expand Up @@ -213,7 +213,11 @@ describe('Window', () => {
vendor: '',
vendorSub: '',
webdriver: true
});
};

for (const propertyKey in referenceValues) {
expect(window.navigator[propertyKey]).toEqual(referenceValues[propertyKey]);
}
});
});

Expand Down

0 comments on commit 26d8246

Please sign in to comment.