1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-11-26 22:41:17 +02:00

Chore: shim.mobilePlatform: Use a stronger return type (#13415)

This commit is contained in:
Henry Heino
2025-10-09 13:46:04 -07:00
committed by GitHub
parent 191775310e
commit d096a90c0e
8 changed files with 29 additions and 20 deletions

View File

@@ -2,7 +2,7 @@ import PluginRunner from '../../../app/services/plugins/PluginRunner';
import PluginService, { PluginSettings, defaultPluginSetting } from '@joplin/lib/services/plugins/PluginService'; import PluginService, { PluginSettings, defaultPluginSetting } from '@joplin/lib/services/plugins/PluginService';
import { ContentScriptType } from '@joplin/lib/services/plugins/api/types'; import { ContentScriptType } from '@joplin/lib/services/plugins/api/types';
import MdToHtml from '@joplin/renderer/MdToHtml'; import MdToHtml from '@joplin/renderer/MdToHtml';
import shim from '@joplin/lib/shim'; import shim, { MobilePlatform } from '@joplin/lib/shim';
import Setting from '@joplin/lib/models/Setting'; import Setting from '@joplin/lib/models/Setting';
import * as fs from 'fs-extra'; import * as fs from 'fs-extra';
import Note from '@joplin/lib/models/Note'; import Note from '@joplin/lib/models/Note';
@@ -310,7 +310,7 @@ describe('services_PluginService', () => {
let resetPlatformMock = () => {}; let resetPlatformMock = () => {};
if (!isDesktop) { if (!isDesktop) {
resetPlatformMock = mockMobilePlatform('android').reset; resetPlatformMock = mockMobilePlatform(MobilePlatform.Android).reset;
} }
try { try {

View File

@@ -7,6 +7,7 @@ import createMockReduxStore from '../utils/testing/createMockReduxStore';
import setupGlobalStore from '../utils/testing/setupGlobalStore'; import setupGlobalStore from '../utils/testing/setupGlobalStore';
import { act, fireEvent, render, screen } from '@testing-library/react-native'; import { act, fireEvent, render, screen } from '@testing-library/react-native';
import FeedbackBanner from './FeedbackBanner'; import FeedbackBanner from './FeedbackBanner';
import { MobilePlatform } from '@joplin/lib/shim';
interface WrapperProps { } interface WrapperProps { }
@@ -84,7 +85,7 @@ describe('FeedbackBanner', () => {
setupGlobalStore(store); setupGlobalStore(store);
jest.useFakeTimers({ advanceTimers: true }); jest.useFakeTimers({ advanceTimers: true });
mockMobilePlatform('web'); mockMobilePlatform(MobilePlatform.Web);
}); });
afterEach(() => { afterEach(() => {
@@ -93,9 +94,9 @@ describe('FeedbackBanner', () => {
}); });
test.each([ test.each([
{ platform: 'android', shouldShow: false }, { platform: MobilePlatform.Android, shouldShow: false },
{ platform: 'web', shouldShow: true }, { platform: MobilePlatform.Web, shouldShow: true },
{ platform: 'ios', shouldShow: false }, { platform: MobilePlatform.Ios, shouldShow: false },
])('should correctly show/hide the feedback banner on %s', ({ platform, shouldShow }) => { ])('should correctly show/hide the feedback banner on %s', ({ platform, shouldShow }) => {
mockMobilePlatform(platform); mockMobilePlatform(platform);

View File

@@ -6,7 +6,7 @@ import { act, fireEvent, render, screen, userEvent, waitFor } from '../../../../
import PluginService, { PluginSettings, defaultPluginSetting } from '@joplin/lib/services/plugins/PluginService'; import PluginService, { PluginSettings, defaultPluginSetting } from '@joplin/lib/services/plugins/PluginService';
import { writeFile } from 'fs-extra'; import { writeFile } from 'fs-extra';
import { join } from 'path'; import { join } from 'path';
import shim from '@joplin/lib/shim'; import shim, { MobilePlatform } from '@joplin/lib/shim';
import { resetRepoApi } from './utils/useRepoApi'; import { resetRepoApi } from './utils/useRepoApi';
import { Store } from 'redux'; import { Store } from 'redux';
import { AppState } from '../../../../utils/types'; import { AppState } from '../../../../utils/types';
@@ -59,7 +59,7 @@ describe('PluginStates.installed', () => {
mockPluginServiceSetup(reduxStore); mockPluginServiceSetup(reduxStore);
resetRepoApi(); resetRepoApi();
await mockMobilePlatform('android'); await mockMobilePlatform(MobilePlatform.Android);
await mockRepositoryApiConstructor(); await mockRepositoryApiConstructor();
// Fake timers are necessary to prevent a warning. // Fake timers are necessary to prevent a warning.
@@ -73,8 +73,8 @@ describe('PluginStates.installed', () => {
}); });
it.each([ it.each([
'android', MobilePlatform.Android,
'ios', MobilePlatform.Ios,
])('should not allow updating a plugin that is not recommended on iOS, but should on Android (on %s)', async (platform) => { ])('should not allow updating a plugin that is not recommended on iOS, but should on Android (on %s)', async (platform) => {
await mockMobilePlatform(platform); await mockMobilePlatform(platform);
expect(shim.mobilePlatform()).toBe(platform); expect(shim.mobilePlatform()).toBe(platform);

View File

@@ -10,6 +10,7 @@ import { Store } from 'redux';
import mockRepositoryApiConstructor from './testUtils/mockRepositoryApiConstructor'; import mockRepositoryApiConstructor from './testUtils/mockRepositoryApiConstructor';
import { resetRepoApi } from './utils/useRepoApi'; import { resetRepoApi } from './utils/useRepoApi';
import mockPluginServiceSetup from '../../../../utils/testing/mockPluginServiceSetup'; import mockPluginServiceSetup from '../../../../utils/testing/mockPluginServiceSetup';
import { MobilePlatform } from '@joplin/lib/shim';
const expectSearchResultCountToBe = async (count: number) => { const expectSearchResultCountToBe = async (count: number) => {
await waitFor(() => { await waitFor(() => {
@@ -38,7 +39,7 @@ describe('PluginStates.search', () => {
await switchClient(0); await switchClient(0);
reduxStore = createMockReduxStore(); reduxStore = createMockReduxStore();
mockPluginServiceSetup(reduxStore); mockPluginServiceSetup(reduxStore);
mockMobilePlatform('android'); mockMobilePlatform(MobilePlatform.Android);
resetRepoApi(); resetRepoApi();
await mockRepositoryApiConstructor(); await mockRepositoryApiConstructor();
@@ -70,7 +71,7 @@ describe('PluginStates.search', () => {
it('should only show recommended plugin search results on iOS-like environments', async () => { it('should only show recommended plugin search results on iOS-like environments', async () => {
// iOS uses restricted install mode // iOS uses restricted install mode
mockMobilePlatform('ios'); mockMobilePlatform(MobilePlatform.Ios);
await mockRepositoryApiConstructor(); await mockRepositoryApiConstructor();
const wrapper = render(<WrappedPluginStates initialPluginSettings={{}} store={reduxStore}/>); const wrapper = render(<WrappedPluginStates initialPluginSettings={{}} store={reduxStore}/>);

View File

@@ -1,6 +1,6 @@
import shimInitShared from './shimInitShared'; import shimInitShared from './shimInitShared';
import shim from '@joplin/lib/shim'; import shim, { MobilePlatform } from '@joplin/lib/shim';
const { GeolocationReact } = require('../geolocation-react.js'); const { GeolocationReact } = require('../geolocation-react.js');
import RNFetchBlob from 'rn-fetch-blob'; import RNFetchBlob from 'rn-fetch-blob';
import { generateSecureRandom } from 'react-native-securerandom'; import { generateSecureRandom } from 'react-native-securerandom';
@@ -165,7 +165,7 @@ export default function shimInit() {
}; };
shim.mobilePlatform = () => { shim.mobilePlatform = () => {
return Platform.OS; return Platform.OS as MobilePlatform;
}; };
shim.isAppleSilicon = () => { shim.isAppleSilicon = () => {

View File

@@ -9,7 +9,7 @@ import * as mimeUtils from '@joplin/lib/mime-utils';
import Resource from '@joplin/lib/models/Resource'; import Resource from '@joplin/lib/models/Resource';
import { getLocales } from 'react-native-localize'; import { getLocales } from 'react-native-localize';
import type Setting from '@joplin/lib/models/Setting'; import type Setting from '@joplin/lib/models/Setting';
import shim from '@joplin/lib/shim'; import shim, { MobilePlatform } from '@joplin/lib/shim';
import { closestSupportedLocale, defaultLocale, setLocale } from '@joplin/lib/locale'; import { closestSupportedLocale, defaultLocale, setLocale } from '@joplin/lib/locale';
const shimInitShared = () => { const shimInitShared = () => {
@@ -76,7 +76,7 @@ const shimInitShared = () => {
}; };
shim.mobilePlatform = () => { shim.mobilePlatform = () => {
return Platform.OS; return Platform.OS as MobilePlatform;
}; };
shim.platformArch = () => { shim.platformArch = () => {

View File

@@ -54,6 +54,13 @@ export interface ShowMessageBoxOptions {
cancelId?: number; cancelId?: number;
} }
export enum MobilePlatform {
None = '',
Android = 'android',
Ios = 'ios',
Web = 'web',
}
let isTestingEnv_ = false; let isTestingEnv_ = false;
// We need to ensure that there's only one instance of React being used by all // We need to ensure that there's only one instance of React being used by all
@@ -190,8 +197,8 @@ const shim = {
}, },
// "ios" or "android", or "" if not on mobile // "ios" or "android", or "" if not on mobile
mobilePlatform: () => { mobilePlatform: (): MobilePlatform => {
return ''; // Default if we're not on mobile (React Native) return MobilePlatform.None; // Default if we're not on mobile (React Native)
}, },
// https://github.com/cheton/is-electron // https://github.com/cheton/is-electron

View File

@@ -6,7 +6,7 @@ import Setting, { AppType, Env } from '../models/Setting';
import BaseService from '../services/BaseService'; import BaseService from '../services/BaseService';
import FsDriverNode from '../fs-driver-node'; import FsDriverNode from '../fs-driver-node';
import time from '../time'; import time from '../time';
import shim from '../shim'; import shim, { MobilePlatform } from '../shim';
import uuid from '../uuid'; import uuid from '../uuid';
import ResourceService from '../services/ResourceService'; import ResourceService from '../services/ResourceService';
import KeymapService from '../services/KeymapService'; import KeymapService from '../services/KeymapService';
@@ -1119,7 +1119,7 @@ export const newOcrService = () => {
return new OcrService([driver]); return new OcrService([driver]);
}; };
export const mockMobilePlatform = (platform: string) => { export const mockMobilePlatform = (platform: MobilePlatform) => {
const originalMobilePlatform = shim.mobilePlatform; const originalMobilePlatform = shim.mobilePlatform;
const originalIsNode = shim.isNode; const originalIsNode = shim.isNode;