Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use platform over appversion on global navigator object #5389

Open
wants to merge 5 commits into
base: production
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ const isDarkMode = ({
isRedirecting,
}: PreferencesVisibilityContext): boolean => isDarkMode || isRedirecting;

const altKeyName = globalThis.navigator?.appVersion.includes('Mac')
/**
* REFACTOR: navigator.platform is deprecated. Implement better feature
* detection for browsers using Mac.
* See https://developer.mozilla.org/en-US/docs/Web/API/Navigator/platform
*/
const altKeyName = globalThis.navigator?.platform.startsWith('Mac')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is exactly the use case that was quoted by you from the MDN article, so probably using navigator.platform in the right choice in this case?

? 'Option'
: 'Alt';

Expand Down
13 changes: 6 additions & 7 deletions specifyweb/frontend/js_src/lib/tests/jsdom.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { JSDOM } from 'jsdom';

// Create a new JSDOM instance
/**
* FEATURE: Allow customizing the JSDOM userAgent, platform, and other global
* properties
* See https://github.com/jsdom/jsdom#advanced-configuration
* See https://github.com/specify/specify7/pull/5389
Comment on lines +5 to +8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that should probably be done using mocking:

in beforeAll in some test file set userAgent/platform/...
in afterAll undo the change

*/
const { window } = new JSDOM('<!DOCTYPE html><p>Hello world</p>');

// Define window and other globals
globalThis.window = window;
globalThis.document = window.document;
if (!global.navigator) {
global.navigator = {
userAgent: 'node.js',
appVersion: 'node.js',
platform: 'node.js',
};
}

// You can also define other browser-specific globals as needed
global.HTMLElement = window.HTMLElement;
Expand Down
Loading