|
| 1 | +/** |
| 2 | + * @format |
| 3 | + */ |
| 4 | + |
| 5 | +import ObjcPatcher from '../ObjcPatcher'; |
| 6 | +import fs from 'fs'; |
| 7 | + |
| 8 | +jest.mock('fs'); |
| 9 | + |
| 10 | +describe('ObjcPatcher', () => { |
| 11 | + test('addImport() will append imports', () => { |
| 12 | + const beforePatch = `\ |
| 13 | +#import <UIKit/UIKit.h> |
| 14 | +#import <React/RCTRootView.h> |
| 15 | +
|
| 16 | +@implementation AppDelegate |
| 17 | +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
| 18 | +{ |
| 19 | + return NO; |
| 20 | +} |
| 21 | +`; |
| 22 | + |
| 23 | + const afterPatch = `\ |
| 24 | +/* Patched by ObjcPatcher: foo */ |
| 25 | +#import <UIKit/UIKit.h> |
| 26 | +#import <React/RCTRootView.h> |
| 27 | +#import <React/Foo.h> |
| 28 | +
|
| 29 | +@implementation AppDelegate |
| 30 | +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
| 31 | +{ |
| 32 | + return NO; |
| 33 | +} |
| 34 | +`; |
| 35 | + fs.readFileSync = jest.fn().mockReturnValueOnce(beforePatch); |
| 36 | + |
| 37 | + fs.writeFileSync = jest.fn(); |
| 38 | + |
| 39 | + const patcher = new ObjcPatcher('/foo', 'foo'); |
| 40 | + patcher.addImport('<React/Foo.h>').write('/bar'); |
| 41 | + expect(fs.writeFileSync.mock.calls[0][0]).toBe('/bar'); |
| 42 | + expect(fs.writeFileSync.mock.calls[0][1]).toBe(afterPatch); |
| 43 | + }); |
| 44 | + |
| 45 | + test('isPatched() returns false if the file has not been patched', () => { |
| 46 | + const beforePatch = `\ |
| 47 | +#import <UIKit/UIKit.h> |
| 48 | +#import <React/RCTRootView.h> |
| 49 | +
|
| 50 | +@implementation AppDelegate |
| 51 | +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
| 52 | +{ |
| 53 | + return NO; |
| 54 | +} |
| 55 | +`; |
| 56 | + |
| 57 | + fs.readFileSync = jest.fn().mockReturnValueOnce(beforePatch); |
| 58 | + const patcher = new ObjcPatcher('/foo', 'foo'); |
| 59 | + expect(patcher.isPatched()).toBe(false); |
| 60 | + }); |
| 61 | + |
| 62 | + test('isPatched() returns true if the file has been patched', () => { |
| 63 | + const afterPatch = `\ |
| 64 | +/* Patched by ObjcPatcher: foo */ |
| 65 | +#import <UIKit/UIKit.h> |
| 66 | +#import <React/RCTRootView.h> |
| 67 | +
|
| 68 | +@implementation AppDelegate |
| 69 | +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
| 70 | +{ |
| 71 | + return NO; |
| 72 | +} |
| 73 | +`; |
| 74 | + |
| 75 | + fs.readFileSync = jest.fn().mockReturnValueOnce(afterPatch); |
| 76 | + const patcher = new ObjcPatcher('/foo', 'foo'); |
| 77 | + expect(patcher.isPatched()).toBe(true); |
| 78 | + }); |
| 79 | + |
| 80 | + test('addFunction() will add code block before last @end', () => { |
| 81 | + const beforePatch = `\ |
| 82 | +#import <UIKit/UIKit.h> |
| 83 | +#import <React/RCTRootView.h> |
| 84 | +
|
| 85 | +@implementation AppDelegate |
| 86 | +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
| 87 | +{ |
| 88 | + self.window.rootViewController = rootViewController; |
| 89 | + return NO; |
| 90 | +} |
| 91 | +@end |
| 92 | +`; |
| 93 | + |
| 94 | + const afterPatch = `\ |
| 95 | +/* Patched by ObjcPatcher: foo */ |
| 96 | +#import <UIKit/UIKit.h> |
| 97 | +#import <React/RCTRootView.h> |
| 98 | +
|
| 99 | +@implementation AppDelegate |
| 100 | +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
| 101 | +{ |
| 102 | + self.window.rootViewController = rootViewController; |
| 103 | + return NO; |
| 104 | +} |
| 105 | +
|
| 106 | +- (void)foo |
| 107 | +{ |
| 108 | +} |
| 109 | +@end |
| 110 | +`; |
| 111 | + |
| 112 | + fs.readFileSync = jest.fn().mockReturnValueOnce(beforePatch); |
| 113 | + |
| 114 | + fs.writeFileSync = jest.fn(); |
| 115 | + |
| 116 | + const patcher = new ObjcPatcher('/foo', 'foo'); |
| 117 | + const addCode = ` |
| 118 | +- (void)foo |
| 119 | +{ |
| 120 | +}`; |
| 121 | + patcher.addFunction(addCode).write('/bar'); |
| 122 | + expect(fs.writeFileSync.mock.calls[0][1]).toBe(afterPatch); |
| 123 | + }); |
| 124 | + |
| 125 | + test('replace() is simply passthrough String.replace', () => { |
| 126 | + const beforePatch = `\ |
| 127 | +#import <UIKit/UIKit.h> |
| 128 | +#import <React/RCTRootView.h> |
| 129 | +
|
| 130 | +@implementation AppDelegate |
| 131 | +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
| 132 | +{ |
| 133 | + self.window.rootViewController = rootViewController; |
| 134 | + return NO; |
| 135 | +} |
| 136 | +`; |
| 137 | + |
| 138 | + const afterPatch = `\ |
| 139 | +/* Patched by ObjcPatcher: foo */ |
| 140 | +#import <UIKit/UIKit.h> |
| 141 | +#import <React/RCTRootView.h> |
| 142 | +
|
| 143 | +@implementation AppDelegate |
| 144 | +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
| 145 | +{ |
| 146 | + self.window.rootViewController = rootViewController; |
| 147 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 148 | + NSLog(@"AsyncTask"); |
| 149 | + }); |
| 150 | + return NO; |
| 151 | +} |
| 152 | +`; |
| 153 | + |
| 154 | + fs.readFileSync = jest.fn().mockReturnValueOnce(beforePatch); |
| 155 | + |
| 156 | + fs.writeFileSync = jest.fn(); |
| 157 | + |
| 158 | + const patcher = new ObjcPatcher('/foo', 'foo'); |
| 159 | + const patchCode = `\ |
| 160 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 161 | + NSLog(@"AsyncTask"); |
| 162 | + });`; |
| 163 | + patcher |
| 164 | + .replace( |
| 165 | + /(^\s*self.window.rootViewController = rootViewController;\s*$)/m, |
| 166 | + `$1\n${patchCode}`, |
| 167 | + ) |
| 168 | + .write('/bar'); |
| 169 | + expect(fs.writeFileSync.mock.calls[0][1]).toBe(afterPatch); |
| 170 | + }); |
| 171 | +}); |
0 commit comments