Skip to content

Commit

Permalink
throw Error if explicitly enabled but failed to find 'openURL' method
Browse files Browse the repository at this point in the history
  • Loading branch information
jey committed Dec 27, 2024
1 parent 75a6d12 commit 18bcd5a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 13 deletions.
53 changes: 48 additions & 5 deletions packages/auth/plugin/__tests__/iosPlugin_openUrlFix.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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",
);
});
});
Expand Down
24 changes: 16 additions & 8 deletions packages/auth/plugin/src/ios/openUrlFix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@ export const withIosCaptchaOpenUrlFix: ConfigPlugin<PluginConfigType> = (
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
Expand All @@ -38,15 +35,27 @@ export function shouldApplyIosOpenUrlFix({

export function withOpenUrlFixForCaptcha({
config,
props,
}: {
config: ExportedConfigWithProps<AppDelegateProjectFile>;
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,
Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 18bcd5a

Please sign in to comment.