|
| 1 | +import type { ExpoConfig } from '@expo/config-types'; |
| 2 | + |
| 3 | +import { warnOnce } from '../../plugin/src/utils'; |
| 4 | +import { modifyAppDelegate } from '../../plugin/src/withSentryIOS'; |
| 5 | + |
| 6 | +// Mock dependencies |
| 7 | +jest.mock('@expo/config-plugins', () => ({ |
| 8 | + ...jest.requireActual('@expo/config-plugins'), |
| 9 | + withAppDelegate: jest.fn((config, callback) => callback(config)), |
| 10 | +})); |
| 11 | + |
| 12 | +jest.mock('../../plugin/src/utils', () => ({ |
| 13 | + warnOnce: jest.fn(), |
| 14 | +})); |
| 15 | + |
| 16 | +interface MockedExpoConfig extends ExpoConfig { |
| 17 | + modResults: { |
| 18 | + path: string; |
| 19 | + contents: string; |
| 20 | + language: 'swift' | 'objc'; |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +const objcContents = `#import "AppDelegate.h" |
| 25 | +
|
| 26 | +@implementation AppDelegate |
| 27 | +
|
| 28 | +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
| 29 | +{ |
| 30 | + self.moduleName = @"main"; |
| 31 | +
|
| 32 | + // You can add your custom initial props in the dictionary below. |
| 33 | + // They will be passed down to the ViewController used by React Native. |
| 34 | + self.initialProps = @{}; |
| 35 | +
|
| 36 | + return [super application:application didFinishLaunchingWithOptions:launchOptions]; |
| 37 | +} |
| 38 | +
|
| 39 | +@end |
| 40 | +`; |
| 41 | + |
| 42 | +const objcExpected = `#import "AppDelegate.h" |
| 43 | +#import <RNSentry/RNSentry.h> |
| 44 | +
|
| 45 | +@implementation AppDelegate |
| 46 | +
|
| 47 | +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
| 48 | +{ |
| 49 | + [RNSentrySDK start]; |
| 50 | + self.moduleName = @"main"; |
| 51 | +
|
| 52 | + // You can add your custom initial props in the dictionary below. |
| 53 | + // They will be passed down to the ViewController used by React Native. |
| 54 | + self.initialProps = @{}; |
| 55 | +
|
| 56 | + return [super application:application didFinishLaunchingWithOptions:launchOptions]; |
| 57 | +} |
| 58 | +
|
| 59 | +@end |
| 60 | +`; |
| 61 | + |
| 62 | +const swiftContents = `import React |
| 63 | +import React_RCTAppDelegate |
| 64 | +import ReactAppDependencyProvider |
| 65 | +import UIKit |
| 66 | +
|
| 67 | +@main |
| 68 | +class AppDelegate: RCTAppDelegate { |
| 69 | + override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool { |
| 70 | + self.moduleName = "sentry-react-native-sample" |
| 71 | + self.dependencyProvider = RCTAppDependencyProvider() |
| 72 | + return super.application(application, didFinishLaunchingWithOptions: launchOptions) |
| 73 | + } |
| 74 | +}`; |
| 75 | + |
| 76 | +const swiftExpected = `import React |
| 77 | +import React_RCTAppDelegate |
| 78 | +import ReactAppDependencyProvider |
| 79 | +import UIKit |
| 80 | +import RNSentry |
| 81 | +
|
| 82 | +@main |
| 83 | +class AppDelegate: RCTAppDelegate { |
| 84 | + override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool { |
| 85 | + RNSentrySDK.start() |
| 86 | + self.moduleName = "sentry-react-native-sample" |
| 87 | + self.dependencyProvider = RCTAppDependencyProvider() |
| 88 | + return super.application(application, didFinishLaunchingWithOptions: launchOptions) |
| 89 | + } |
| 90 | +}`; |
| 91 | + |
| 92 | +describe('modifyAppDelegate', () => { |
| 93 | + let config: MockedExpoConfig; |
| 94 | + |
| 95 | + beforeEach(() => { |
| 96 | + jest.clearAllMocks(); |
| 97 | + // Reset to a mocked Swift config after each test |
| 98 | + config = { |
| 99 | + name: 'test', |
| 100 | + slug: 'test', |
| 101 | + modResults: { |
| 102 | + path: 'samples/react-native/ios/AppDelegate.swift', |
| 103 | + contents: swiftContents, |
| 104 | + language: 'swift', |
| 105 | + }, |
| 106 | + }; |
| 107 | + }); |
| 108 | + |
| 109 | + it('should skip modification if modResults or path is missing', async () => { |
| 110 | + config.modResults.path = undefined; |
| 111 | + |
| 112 | + const result = await modifyAppDelegate(config); |
| 113 | + |
| 114 | + expect(warnOnce).toHaveBeenCalledWith('Skipping AppDelegate modification because the file does not exist.'); |
| 115 | + expect(result).toBe(config); // No modification |
| 116 | + }); |
| 117 | + |
| 118 | + it('should warn if RNSentrySDK.start() is already present in a Swift project', async () => { |
| 119 | + config.modResults.contents = 'RNSentrySDK.start();'; |
| 120 | + |
| 121 | + const result = await modifyAppDelegate(config); |
| 122 | + |
| 123 | + expect(warnOnce).toHaveBeenCalledWith(`Your 'AppDelegate.swift' already contains 'RNSentrySDK.start()'.`); |
| 124 | + expect(result).toBe(config); // No modification |
| 125 | + }); |
| 126 | + |
| 127 | + it('should warn if [RNSentrySDK start] is already present in an Objective-C project', async () => { |
| 128 | + config.modResults.language = 'objc'; |
| 129 | + config.modResults.path = 'samples/react-native/ios/AppDelegate.mm'; |
| 130 | + config.modResults.contents = '[RNSentrySDK start];'; |
| 131 | + |
| 132 | + const result = await modifyAppDelegate(config); |
| 133 | + |
| 134 | + expect(warnOnce).toHaveBeenCalledWith(`Your 'AppDelegate.mm' already contains '[RNSentrySDK start]'.`); |
| 135 | + expect(result).toBe(config); // No modification |
| 136 | + }); |
| 137 | + |
| 138 | + it('should modify a Swift file by adding the RNSentrySDK import and start', async () => { |
| 139 | + const result = (await modifyAppDelegate(config)) as MockedExpoConfig; |
| 140 | + |
| 141 | + expect(result.modResults.contents).toContain('import RNSentry'); |
| 142 | + expect(result.modResults.contents).toContain('RNSentrySDK.start()'); |
| 143 | + expect(result.modResults.contents).toBe(swiftExpected); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should modify an Objective-C file by adding the RNSentrySDK import and start', async () => { |
| 147 | + config.modResults.language = 'objc'; |
| 148 | + config.modResults.contents = objcContents; |
| 149 | + |
| 150 | + const result = (await modifyAppDelegate(config)) as MockedExpoConfig; |
| 151 | + |
| 152 | + expect(result.modResults.contents).toContain('#import <RNSentry/RNSentry.h>'); |
| 153 | + expect(result.modResults.contents).toContain('[RNSentrySDK start];'); |
| 154 | + expect(result.modResults.contents).toBe(objcExpected); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should insert import statements only once in an Swift project', async () => { |
| 158 | + config.modResults.contents = |
| 159 | + 'import UIKit\nimport RNSentrySDK\n\noverride func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {'; |
| 160 | + |
| 161 | + const result = (await modifyAppDelegate(config)) as MockedExpoConfig; |
| 162 | + |
| 163 | + const importCount = (result.modResults.contents.match(/import RNSentrySDK/g) || []).length; |
| 164 | + expect(importCount).toBe(1); |
| 165 | + }); |
| 166 | + |
| 167 | + it('should insert import statements only once in an Objective-C project', async () => { |
| 168 | + config.modResults.language = 'objc'; |
| 169 | + config.modResults.contents = |
| 170 | + '#import "AppDelegate.h"\n#import <RNSentry/RNSentry.h>\n\n- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {'; |
| 171 | + |
| 172 | + const result = (await modifyAppDelegate(config)) as MockedExpoConfig; |
| 173 | + |
| 174 | + const importCount = (result.modResults.contents.match(/#import <RNSentry\/RNSentry.h>/g) || []).length; |
| 175 | + expect(importCount).toBe(1); |
| 176 | + }); |
| 177 | +}); |
0 commit comments