Fix to convert electron platform name darwin to macos (#3573)

* Fix to convert electron platform name darwin to macos

* Remove unneeded async

* Fix to handle other possible platform strings

* Add electron test for user sidebar menu text

* Fix formatting
This commit is contained in:
Jonathan Tran
2024-08-20 22:08:02 -04:00
committed by GitHub
parent 3543c5f0e7
commit 4a14ca38ab
2 changed files with 54 additions and 3 deletions

View File

@ -0,0 +1,34 @@
import { test, expect } from '@playwright/test'
import { setupElectron, tearDown } from './test-utils'
test.afterEach(async ({ page }, testInfo) => {
await tearDown(page, testInfo)
})
test.describe('Electron user sidebar menu tests', () => {
test(
'User settings has correct shortcut',
{ tag: '@electron' },
async ({ browserName }, testInfo) => {
const { electronApp, page } = await setupElectron({
testInfo,
folderSetupFn: async () => {},
})
await page.setViewportSize({ width: 1200, height: 500 })
// Open the user sidebar menu.
await page.getByTestId('user-sidebar-toggle').click()
// No space after "User settings" since it's textContent.
const text =
process.platform === 'darwin' ? 'User settings⌘,' : 'User settingsCtrl,'
const userSettingsButton = page.getByTestId('user-settings')
await expect(userSettingsButton).toBeVisible()
await expect(userSettingsButton).toHaveText(text)
await electronApp.close()
}
)
})

View File

@ -7,12 +7,29 @@ export default function usePlatform() {
const [platformName, setPlatformName] = useState<Platform>('') const [platformName, setPlatformName] = useState<Platform>('')
useEffect(() => { useEffect(() => {
async function getPlatform() { function getPlatform(): Platform {
setPlatformName((window.electron.platform ?? '') as Platform) const platform = window.electron.platform ?? ''
// https://nodejs.org/api/process.html#processplatform
switch (platform) {
case 'darwin':
return 'macos'
case 'win32':
return 'windows'
// We don't currently care to distinguish between these.
case 'android':
case 'freebsd':
case 'linux':
case 'openbsd':
case 'sunos':
return 'linux'
default:
console.error('Unknown platform:', platform)
return ''
}
} }
if (isDesktop()) { if (isDesktop()) {
void getPlatform() setPlatformName(getPlatform())
} else { } else {
if (navigator.userAgent.indexOf('Mac') !== -1) { if (navigator.userAgent.indexOf('Mac') !== -1) {
setPlatformName('macos') setPlatformName('macos')