From 18bcd5aa71a174a9f5acbdc11d269b6f34e6fd24 Mon Sep 17 00:00:00 2001 From: Jey Kottalam Date: Thu, 26 Dec 2024 22:20:27 -0800 Subject: [PATCH] throw Error if explicitly enabled but failed to find 'openURL' method --- .../__tests__/iosPlugin_openUrlFix.test.ts | 53 +++++++++++++++++-- packages/auth/plugin/src/ios/openUrlFix.ts | 24 ++++++--- 2 files changed, 64 insertions(+), 13 deletions(-) diff --git a/packages/auth/plugin/__tests__/iosPlugin_openUrlFix.test.ts b/packages/auth/plugin/__tests__/iosPlugin_openUrlFix.test.ts index 823a5f5413..8c93723f44 100644 --- a/packages/auth/plugin/__tests__/iosPlugin_openUrlFix.test.ts +++ b/packages/auth/plugin/__tests__/iosPlugin_openUrlFix.test.ts @@ -1,7 +1,12 @@ import fs from 'fs/promises'; import path from 'path'; import { beforeEach, describe, expect, it, jest } from '@jest/globals'; -import { shouldApplyIosOpenUrlFix, modifyObjcAppDelegate } from '../src/ios/openUrlFix'; +import type { AppDelegateProjectFile } from '@expo/config-plugins/build/ios/Paths'; +import { + shouldApplyIosOpenUrlFix, + modifyObjcAppDelegate, + withOpenUrlFixForCaptcha, +} from '../src/ios/openUrlFix'; import type { ExpoConfigPluginEntry } from '../src/ios/openUrlFix'; describe('Config Plugin iOS Tests - openUrlFix', () => { @@ -194,12 +199,50 @@ describe('Config Plugin iOS Tests - openUrlFix', () => { const appDelegateFixturesNoop = ['AppDelegate_sdk42.m', 'AppDelegate_fallback.m']; appDelegateFixturesNoop.forEach(fixtureName => { it(`skips AppDelegate without openURL - ${fixtureName}`, async () => { - const appDelegate = await readFixtureAsText(fixtureName); + const fixturePath = path.join(__dirname, 'fixtures', fixtureName); + const appDelegate = await fs.readFile(fixturePath, { encoding: 'utf-8' }); + const config = { + name: 'TestName', + slug: 'TestSlug', + plugins: ['expo-router'], + modRequest: { projectRoot: path.join(__dirname, 'fixtures') } as any, + modResults: { + path: fixturePath, + language: 'objc', + contents: appDelegate, + } as AppDelegateProjectFile, + modRawConfig: { name: 'TestName', slug: 'TestSlug' }, + }; + const props = undefined; const spy = jest.spyOn(console, 'warn').mockImplementation(() => undefined); - const result = modifyObjcAppDelegate(appDelegate); - expect(result).toBe(null); + const result = withOpenUrlFixForCaptcha({ config, props }); + expect(result.modResults.contents).toBe(appDelegate); expect(spy).toHaveBeenCalledWith( - "Skipping iOS openURL fix for captcha because no 'openURL' method was found", + "Skipping iOS openURL fix because no 'openURL' method was found", + ); + }); + + it(`errors when enabled but openURL not found - ${fixtureName}`, async () => { + const fixturePath = path.join(__dirname, 'fixtures', fixtureName); + const appDelegate = await fs.readFile(fixturePath, { encoding: 'utf-8' }); + const config = { + name: 'TestName', + slug: 'TestSlug', + modRequest: { projectRoot: path.join(__dirname, 'fixtures') } as any, + modResults: { + path: fixturePath, + language: 'objc', + contents: appDelegate, + } as AppDelegateProjectFile, + modRawConfig: { name: 'TestName', slug: 'TestSlug' }, + }; + const props = { + ios: { + captchaOpenUrlFix: true, + }, + }; + expect(() => withOpenUrlFixForCaptcha({ config, props })).toThrow( + "Failed to apply iOS openURL fix because no 'openURL' method was found", ); }); }); diff --git a/packages/auth/plugin/src/ios/openUrlFix.ts b/packages/auth/plugin/src/ios/openUrlFix.ts index a9fb57ec6e..db768b7322 100644 --- a/packages/auth/plugin/src/ios/openUrlFix.ts +++ b/packages/auth/plugin/src/ios/openUrlFix.ts @@ -8,12 +8,9 @@ export const withIosCaptchaOpenUrlFix: ConfigPlugin = ( config: ExpoConfig, props?: PluginConfigType, ) => { - if (shouldApplyIosOpenUrlFix({ config, props })) { - config = withAppDelegate(config, config => { - return withOpenUrlFixForCaptcha({ config }); - }); - } - return config; + return withAppDelegate(config, config => { + return withOpenUrlFixForCaptcha({ config, props }); + }); }; // Interpret the plugin config to determine whether this fix should be applied @@ -38,15 +35,27 @@ export function shouldApplyIosOpenUrlFix({ export function withOpenUrlFixForCaptcha({ config, + props, }: { config: ExportedConfigWithProps; + props?: PluginConfigType; }) { const { language, contents } = config.modResults; + if (!shouldApplyIosOpenUrlFix({ config, props })) { + return config; + } + if (['objc', 'objcpp'].includes(language)) { const newContents = modifyObjcAppDelegate(contents); if (newContents === null) { - return config; + if (props?.ios?.captchaOpenUrlFix === true) { + throw new Error("Failed to apply iOS openURL fix because no 'openURL' method was found"); + } else { + // eslint-disable-next-line no-console + console.warn("Skipping iOS openURL fix because no 'openURL' method was found"); + return config; + } } else { return { ...config, @@ -86,7 +95,6 @@ export function modifyObjcAppDelegate(contents: string): string | null { } else if (contents.match(/openURL\s*:/)) { throw new Error("Failed to find insertion point but detected 'openURL' method"); } else { - console.warn("Skipping iOS captcha openURL fix because no 'openURL' method was found"); return null; } }