diff --git a/__tests__/__snapshots__/android-java.js.snap b/__tests__/__snapshots__/android-java.js.snap index 90e7647..5ffd1bf 100644 --- a/__tests__/__snapshots__/android-java.js.snap +++ b/__tests__/__snapshots__/android-java.js.snap @@ -1,4 +1,4 @@ -exports[`Android/Java creates a TemplateManager.java 1`] = ` +exports[`Android/Java: Combined creates a TemplateManager.java 1`] = ` "// Created by react-native-create-bridge package com.testapp.testmodule; @@ -29,14 +29,14 @@ public class TestModuleManager extends SimpleViewManager { @ReactProp(name = \"exampleProp\") public void setExampleProp(View view, String prop) { - // Set properties from React onto your native component + // Set properties from React onto your native component via a setter method // https://facebook.github.io/react-native/docs/native-components-android.html#3-expose-view-property-setters-using-reactprop-or-reactpropgroup-annotation } } " `; -exports[`Android/Java creates a TemplateModule.java 1`] = ` +exports[`Android/Java: Combined creates a TemplateModule.java 1`] = ` "// Created by react-native-create-bridge package com.testapp.testmodule; @@ -96,7 +96,7 @@ public class TestModuleModule extends ReactContextBaseJavaModule { " `; -exports[`Android/Java creates a TemplatePackage.java 1`] = ` +exports[`Android/Java: Combined creates a TemplatePackage.java 1`] = ` "// Created by react-native-create-bridge package com.testapp.testmodule; @@ -136,3 +136,175 @@ public class TestModulePackage implements ReactPackage { } " `; + +exports[`Android/Java: Native Modules creates a TemplateModule.java 1`] = ` +"// Created by react-native-create-bridge + +package com.testapp.testmodule; + +import android.support.annotation.Nullable; + +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.bridge.ReactContextBaseJavaModule; +import com.facebook.react.bridge.ReactMethod; +import com.facebook.react.bridge.WritableMap; +import com.facebook.react.modules.core.DeviceEventManagerModule; + +import java.util.HashMap; +import java.util.Map; + +public class TestModuleModule extends ReactContextBaseJavaModule { + public static final String REACT_CLASS = \"TestModule\"; + private static ReactApplicationContext reactContext = null; + + public TestModuleModule(ReactApplicationContext context) { + // Pass in the context to the constructor and save it so you can emit events + // https://facebook.github.io/react-native/docs/native-modules-android.html#the-toast-module + super(context); + + reactContext = context; + } + + @Override + public String getName() { + // Tell React the name of the module + // https://facebook.github.io/react-native/docs/native-modules-android.html#the-toast-module + return REACT_CLASS; + } + + @Override + public Map getConstants() { + // Export any constants to be used in your native module + // https://facebook.github.io/react-native/docs/native-modules-android.html#the-toast-module + final Map constants = new HashMap<>(); + constants.put(\"EXAMPLE_CONSTANT\", \"example\"); + + return constants; + } + + @ReactMethod + public void exampleMethod () { + // An example native method that you will expose to React + // https://facebook.github.io/react-native/docs/native-modules-android.html#the-toast-module + } + + private static void emitDeviceEvent(String eventName, @Nullable WritableMap eventData) { + // A method for emitting from the native side to JS + // https://facebook.github.io/react-native/docs/native-modules-android.html#sending-events-to-javascript + reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, eventData); + } +} +" +`; + +exports[`Android/Java: Native Modules creates a TemplatePackage.java 1`] = ` +"// Created by react-native-create-bridge + +package com.testapp.testmodule; + +import com.facebook.react.ReactPackage; +import com.facebook.react.bridge.JavaScriptModule; +import com.facebook.react.bridge.NativeModule; +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.uimanager.ViewManager; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public class TestModulePackage implements ReactPackage { + @Override + public List createNativeModules(ReactApplicationContext reactContext) { + // Register your native module + // https://facebook.github.io/react-native/docs/native-modules-android.html#register-the-module + return Arrays.asList( + new TestModuleModule(reactContext) + ); + } + + @Override + public List> createJSModules() { + return Collections.emptyList(); + } + + public List createViewManagers(ReactApplicationContext reactContext) { + return Collections.emptyList(); + } +} +" +`; + +exports[`Android/Java: UI Components creates a TemplateManager.java 1`] = ` +"// Created by react-native-create-bridge + +package com.testapp.testmodule; + +import android.view.View; + +import com.facebook.react.uimanager.SimpleViewManager; +import com.facebook.react.uimanager.ThemedReactContext; + +import com.facebook.react.uimanager.annotations.ReactProp; + +public class TestModuleManager extends SimpleViewManager { + public static final String REACT_CLASS = \"TestModule\"; + + @Override + public String getName() { + // Tell React the name of the module + // https://facebook.github.io/react-native/docs/native-components-android.html#1-create-the-viewmanager-subclass + return REACT_CLASS; + } + + @Override + public View createViewInstance(ThemedReactContext context){ + // Create a view here + // https://facebook.github.io/react-native/docs/native-components-android.html#2-implement-method-createviewinstance + return new View(context); + } + + @ReactProp(name = \"exampleProp\") + public void setExampleProp(View view, String prop) { + // Set properties from React onto your native component via a setter method + // https://facebook.github.io/react-native/docs/native-components-android.html#3-expose-view-property-setters-using-reactprop-or-reactpropgroup-annotation + } +} +" +`; + +exports[`Android/Java: UI Components creates a TemplatePackage.java 1`] = ` +"// Created by react-native-create-bridge + +package com.testapp.testmodule; + +import com.facebook.react.ReactPackage; +import com.facebook.react.bridge.JavaScriptModule; +import com.facebook.react.bridge.NativeModule; +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.uimanager.ViewManager; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public class TestModulePackage implements ReactPackage { + @Override + public List createNativeModules(ReactApplicationContext reactContext) { + return Collections.emptyList(); + } + + @Override + public List> createJSModules() { + return Collections.emptyList(); + } + + public List createViewManagers(ReactApplicationContext reactContext) { + // Register your native component\'s view manager + // https://facebook.github.io/react-native/docs/native-components-android.html#4-register-the-viewmanager + return Arrays.asList( + new TestModuleManager() + ); + } +} +" +`; diff --git a/__tests__/__snapshots__/android-kotlin.js.snap b/__tests__/__snapshots__/android-kotlin.js.snap index 94fef54..4ea5326 100644 --- a/__tests__/__snapshots__/android-kotlin.js.snap +++ b/__tests__/__snapshots__/android-kotlin.js.snap @@ -1,4 +1,4 @@ -exports[`Android/Kotlin creates a TemplateManager.kt 1`] = ` +exports[`Android/Kotlin: Combined creates a TemplateManager.kt 1`] = ` "// Created by react-native-create-bridge package com.testapp.testmodule @@ -37,7 +37,7 @@ class TestModuleManager : SimpleViewManager() { " `; -exports[`Android/Kotlin creates a TemplateModule.kt 1`] = ` +exports[`Android/Kotlin: Combined creates a TemplateModule.kt 1`] = ` "// Created by react-native-create-bridge package com.testapp.testmodule @@ -93,7 +93,7 @@ class TestModuleModule(reactContext: ReactApplicationContext) : ReactContextBase " `; -exports[`Android/Kotlin creates a TemplatePackage.kt 1`] = ` +exports[`Android/Kotlin: Combined creates a TemplatePackage.kt 1`] = ` "// Created by react-native-create-bridge package com.testapp.testmodule @@ -109,8 +109,8 @@ import java.util.Arrays class TestModulePackage : ReactPackage { override fun createNativeModules(reactContext: ReactApplicationContext): List { - // Register your native module - // https://facebook.github.io/react-native/docs/native-modules-android.html#register-the-module + // Register your native module + // https://facebook.github.io/react-native/docs/native-modules-android.html#register-the-module return Arrays.asList( TestModuleModule(reactContext) ) @@ -131,3 +131,168 @@ class TestModulePackage : ReactPackage { } " `; + +exports[`Android/Kotlin: Native Modules creates a TemplateModule.kt 1`] = ` +"// Created by react-native-create-bridge + +package com.testapp.testmodule + +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.bridge.ReactContextBaseJavaModule +import com.facebook.react.bridge.ReactMethod +import com.facebook.react.bridge.WritableMap +import com.facebook.react.modules.core.DeviceEventManagerModule + +import java.util.Map + +class TestModuleModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) { + + init { + // Here we\'re saving the context we passed into the constructor to a variable so we can emit events + // https://facebook.github.io/react-native/docs/native-modules-android.html#the-toast-module + reactContext = context + } + + override fun getName(): String { + // Tell React the name of the module + // https://facebook.github.io/react-native/docs/native-components-android.html#1-create-the-viewmanager-subclass + return REACT_CLASS + } + + override fun getConstants(): Map? { + // Export any constants to be used in your native module + // https://facebook.github.io/react-native/docs/native-modules-android.html#the-toast-module + val reactConstants = Map() + constants.put(\"EXAMPLE_CONSTANT\", \"example\") + + return constants + } + + @ReactMethod + fun exampleMethod () { + // An example native method that you will expose to React + // https://facebook.github.io/react-native/docs/native-modules-android.html#the-toast-module + } + + companion object { + val REACT_CLASS = \"TestModule\" + private var reactContext: ReactApplicationContext = null + + private fun emitDeviceEvent(eventName: String, eventData: WritableMap?) { + // A method for emitting from the native side to JS + // https://facebook.github.io/react-native/docs/native-modules-android.html#sending-events-to-javascript + reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java).emit(eventName, eventData) + } + } +} +" +`; + +exports[`Android/Kotlin: Native Modules creates a TemplatePackage.kt 1`] = ` +"// Created by react-native-create-bridge + +package com.testapp.testmodule + +import com.facebook.react.ReactPackage +import com.facebook.react.bridge.JavaScriptModule +import com.facebook.react.bridge.NativeModule +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.uimanager.ViewManager + +import java.util.Arrays + +class TestModulePackage : ReactPackage { + + override fun createNativeModules(reactContext: ReactApplicationContext): List { + // Register your native module + // https://facebook.github.io/react-native/docs/native-modules-android.html#register-the-module + return Arrays.asList( + TestModuleModule(reactContext) + ) + } + + override fun createJSModules(): List> { + return emptyList() + } + + override fun createViewManagers(reactContext: ReactApplicationContext): List> { + return emptyList() + } + +} +" +`; + +exports[`Android/Kotlin: UI Components creates a TemplateManager.kt 1`] = ` +"// Created by react-native-create-bridge + +package com.testapp.testmodule + +import android.view.View + +import com.facebook.react.uimanager.SimpleViewManager +import com.facebook.react.uimanager.ThemedReactContext + +import com.facebook.react.uimanager.annotations.ReactProp + +class TestModuleManager : SimpleViewManager() { + + override fun getName(): String { + // Tell React the name of the module + // https://facebook.github.io/react-native/docs/native-components-android.html#1-create-the-viewmanager-subclass + return REACT_CLASS + } + + public override fun createViewInstance(context: ThemedReactContext): View { + // Create a view here + // https://facebook.github.io/react-native/docs/native-components-android.html#2-implement-method-createviewinstance + return View(context) + } + + @ReactProp(name = \"exampleProp\") + fun setExampleProp(View view, String prop) { + // Set properties from React onto your native component + // https://facebook.github.io/react-native/docs/native-components-android.html#3-expose-view-property-setters-using-reactprop-or-reactpropgroup-annotation + } + + companion object { + val REACT_CLASS = \"TestModule\" + } +} +" +`; + +exports[`Android/Kotlin: UI Components creates a TemplatePackage.kt 1`] = ` +"// Created by react-native-create-bridge + +package com.testapp.testmodule + +import com.facebook.react.ReactPackage +import com.facebook.react.bridge.JavaScriptModule +import com.facebook.react.bridge.NativeModule +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.uimanager.ViewManager + +import java.util.Arrays + +class TestModulePackage : ReactPackage { + + override fun createNativeModules(reactContext: ReactApplicationContext): List { + return emptyList() + } + + override fun createJSModules(): List> { + return emptyList() + } + + override fun createViewManagers(reactContext: ReactApplicationContext): List> { + // https://facebook.github.io/react-native/docs/native-components-android.html#4-register-the-viewmanager + // Register your native component\'s view manager + return Arrays.asList>( + TestModuleManager() + ) + } + +} +" +`; diff --git a/__tests__/__snapshots__/ios-objc.test.js.snap b/__tests__/__snapshots__/ios-objc.test.js.snap index bc61b5a..5ccc388 100644 --- a/__tests__/__snapshots__/ios-objc.test.js.snap +++ b/__tests__/__snapshots__/ios-objc.test.js.snap @@ -1,4 +1,4 @@ -exports[`iOS/Obj-C creates a Template.h 1`] = ` +exports[`iOS/Obj-C: Combined creates a Template.h 1`] = ` "// Created by react-native-create-bridge // import RCTBridgeModule @@ -16,7 +16,7 @@ exports[`iOS/Obj-C creates a Template.h 1`] = ` " `; -exports[`iOS/Obj-C creates a TemplateManager.m 1`] = ` +exports[`iOS/Obj-C: Combined creates a TemplateManager.m 1`] = ` "// Created by react-native-create-bridge #import \"TestModule.h\" @@ -46,6 +46,8 @@ exports[`iOS/Obj-C creates a TemplateManager.m 1`] = ` // https://facebook.github.io/react-native/docs/native-modules-ios.html RCT_EXPORT_MODULE(); +// Export constants +// https://facebook.github.io/react-native/releases/next/docs/native-modules-ios.html#exporting-constants - (NSDictionary *)constantsToExport { return @{ @@ -78,3 +80,213 @@ RCT_EXPORT_METHOD(exampleMethod) @end " `; + +exports[`iOS/Obj-C: Native Modules creates a Template.h 1`] = ` +"// Created by react-native-create-bridge + +// import RCTBridgeModule +#if __has_include() +#import +#elif __has_include(“RCTBridgeModule.h”) +#import “RCTBridgeModule.h” +#else +#import “React/RCTBridgeModule.h” // Required when used as a Pod in a Swift project +#endif + +// import RCTEventEmitter +#if __has_include() +#import +#elif __has_include(“RCTEventEmitter.h”) +#import “RCTEventEmitter.h” +#else +#import “React/RCTEventEmitter.h” // Required when used as a Pod in a Swift project +#endif + +@interface TestModule : RCTEventEmitter + // Define class properties here with @property +@end +" +`; + +exports[`iOS/Obj-C: Native Modules creates a Template.m 1`] = ` +"// Created by react-native-create-bridge + +#import \"TestModule.h\" + +// import RCTBridge +#if __has_include() +#import +#elif __has_include(“RCTBridge.h”) +#import “RCTBridge.h” +#else +#import “React/RCTBridge.h” // Required when used as a Pod in a Swift project +#endif + +// import RCTEventDispatcher +#if __has_include() +#import +#elif __has_include(“RCTEventDispatcher.h”) +#import “RCTEventDispatcher.h” +#else +#import “React/RCTEventDispatcher.h” // Required when used as a Pod in a Swift project +#endif + +@implementation TestModule +@synthesize bridge = _bridge; + +// Export a native module +// https://facebook.github.io/react-native/docs/native-modules-ios.html +RCT_EXPORT_MODULE(); + +// Export constants +// https://facebook.github.io/react-native/releases/next/docs/native-modules-ios.html#exporting-constants +- (NSDictionary *)constantsToExport +{ + return @{ + @\"EXAMPLE\": @\"example\" + }; +} + +// Export methods to a native module +// https://facebook.github.io/react-native/docs/native-modules-ios.html +RCT_EXPORT_METHOD(exampleMethod) +{ + [self.emitMessageToRN:@\"EXAMPLE_EVENT\" :nil] +} + +// List all your events here +// https://facebook.github.io/react-native/releases/next/docs/native-modules-ios.html#sending-events-to-javascript +- (NSArray *)supportedEvents +{ + return @[@\"SampleEvent\"]; +} + +#pragma mark - Private methods + +// Implement methods that you want to export to the native module +- (void) emitMessageToRN: (NSString *)eventName :(NSDictionary *)params { + // The bridge eventDispatcher is used to send events from native to JS env + // No documentation yet on DeviceEventEmitter: https://github.com/facebook/react-native/issues/2819 + [self sendEventWithName: eventName body: params]; +} + +@end +" +`; + +exports[`iOS/Obj-C: UI Components creates a Template.h 1`] = ` +"// Created by react-native-create-bridge + +// import UIKit so you can subclass off UIView +#import + +@class RCTEventDispatcher; + +@interface TestModule : UIView + // Define view properties here with @property + @property (nonatomic, assign) NSString *exampleProp; + + // Initializing with the event dispatcher allows us to communicate with JS + - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher NS_DESIGNATED_INITIALIZER; + +@end +" +`; + +exports[`iOS/Obj-C: UI Components creates a Template.m 1`] = ` +"// Created by react-native-create-bridge +#import +#import \"TestModule.h\" + +// import RCTEventDispatcher +#if __has_include() +#import +#elif __has_include(“RCTEventDispatcher.h”) +#import “RCTEventDispatcher.h” +#else +#import “React/RCTEventDispatcher.h” // Required when used as a Pod in a Swift project +#endif + + +@implementation TestModule : UIView { + + RCTEventDispatcher *_eventDispatcher; + +} + +- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher +{ + if ((self = [super init])) { + _eventDispatcher = eventDispatcher; + } + + return self; +} + +@end" +`; + +exports[`iOS/Obj-C: UI Components creates a TemplateManager.h 1`] = ` +"// Created by react-native-create-bridge + +// import RCTViewManager +#if __has_include() +#import +#elif __has_include(“RCTViewManager.h”) +#import “RCTViewManager.h” +#else +#import “React/RCTViewManager.h” // Required when used as a Pod in a Swift project +#endif + +// Subclass your view manager off the RCTViewManager +// http://facebook.github.io/react-native/docs/native-components-ios.html#ios-mapview-example +@interface TestModuleManager : RCTViewManager + +@end +" +`; + +exports[`iOS/Obj-C: UI Components creates a TemplateManager.m 1`] = ` +"// Created by react-native-create-bridge + +#import +#import \"TestModule.h\" +#import \"TestModuleManager.h\" + +// import RCTBridge +#if __has_include() +#import +#elif __has_include(“RCTBridge.h”) +#import “RCTBridge.h” +#else +#import “React/RCTBridge.h” // Required when used as a Pod in a Swift project +#endif + +@implementation TestModuleManager + +@synthesize bridge = _bridge; + +// Export a native module +// https://facebook.github.io/react-native/docs/native-modules-ios.html +RCT_EXPORT_MODULE(); + +// Return the native view that represents your React component +- (UIView *)view +{ + return [[TestModule alloc] initWithEventDispatcher:self.bridge.eventDispatcher]; +} + +RCT_EXPORT_VIEW_PROPERTY(exampleProp, NSString) + +// Export constants +// https://facebook.github.io/react-native/releases/next/docs/native-modules-ios.html#exporting-constants +- (NSDictionary *)constantsToExport +{ + return @{ + @\"EXAMPLE\": @\"example\" + }; +} + +@end +" +`; diff --git a/__tests__/__snapshots__/ios-swift.test.js.snap b/__tests__/__snapshots__/ios-swift.test.js.snap index f19d322..53c6df4 100644 --- a/__tests__/__snapshots__/ios-swift.test.js.snap +++ b/__tests__/__snapshots__/ios-swift.test.js.snap @@ -1,4 +1,4 @@ -exports[`iOS/Swift creates a Template.m 1`] = ` +exports[`iOS/Swift: Combined creates a Template.m 1`] = ` "// Created by react-native-create-bridge // import RCTViewManager @@ -35,7 +35,30 @@ RCT_EXTERN_METHOD(exampleMethod) " `; -exports[`iOS/Swift creates a TemplateManager.swift 1`] = ` +exports[`iOS/Swift: Combined creates a Template-Bridging-Header.h 1`] = ` +"// Created by react-native-create-bridge +// TestModule-Bridging-Header.h + +// import RCTBridgeModule +#if __has_include() +#import +#elif __has_include(“RCTBridgeModule.h”) +#import “RCTBridgeModule.h” +#else +#import “React/RCTBridgeModule.h” // Required when used as a Pod in a Swift project +#endif + +// import RCTEventEmitter +#if __has_include() +#import +#elif __has_include(“RCTViewManager.h”) +#import “RCTViewManager.h” +#else +#import “React/RCTViewManager.h” // Required when used as a Pod in a Swift project +#endif" +`; + +exports[`iOS/Swift: Combined creates a TemplateManager.swift 1`] = ` "// Created by react-native-create-bridge import Foundation @@ -61,3 +84,172 @@ class TestModuleManager : RCTViewManager { } " `; + +exports[`iOS/Swift: Native Modules creates a Template.m 1`] = ` +"// Created by react-native-create-bridge + +// import RCTBridgeModule +#if __has_include() +#import +#elif __has_include(“RCTBridgeModule.h”) +#import “RCTBridgeModule.h” +#else +#import “React/RCTBridgeModule.h” // Required when used as a Pod in a Swift project +#endif + +// import RCTEventEmitter +#if __has_include() +#import +#elif __has_include(“RCTEventEmitter.h”) +#import “RCTEventEmitter.h” +#else +#import “React/RCTEventEmitter.h” // Required when used as a Pod in a Swift project +#endif + +// Export a native module +// https://facebook.github.io/react-native/docs/native-modules-ios.html#exporting-swift +@interface RCT_EXTERN_MODULE(TestModule, NSObject) + +// Export methods to a native module +// https://facebook.github.io/react-native/docs/native-modules-ios.html#exporting-swift +RCT_EXTERN_METHOD(exampleMethod) + +@end +" +`; + +exports[`iOS/Swift: Native Modules creates a Template.swift 1`] = ` +"// Created by react-native-create-bridge + +import Foundation + +@objc(TestModule) +class TestModule : NSObject { + // Export constants to use in your native module + override func constantsToExport() -> [String : Any]! { + return [\"EXAMPLE_CONSTANT\": \"example\"] + } + + // Implement methods that you want to export to the native module + @objc func exampleMethod() { + // write method here + } +} +" +`; + +exports[`iOS/Swift: Native Modules creates a Template-Bridging-Header.h 1`] = ` +"// Created by react-native-create-bridge +// TestModule-Bridging-Header.h + +// import RCTBridgeModule +#if __has_include() +#import +#elif __has_include(“RCTBridgeModule.h”) +#import “RCTBridgeModule.h” +#else +#import “React/RCTBridgeModule.h” // Required when used as a Pod in a Swift project +#endif + +// import RCTEventEmitter +#if __has_include() +#import +#elif __has_include(“RCTEventEmitter.h”) +#import “RCTEventEmitter.h” +#else +#import “React/RCTEventEmitter.h” // Required when used as a Pod in a Swift project +#endif" +`; + +exports[`iOS/Swift: UI Components creates a Template.m 1`] = ` +"// Created by react-native-create-bridge + +// import RCTViewManager +#if __has_include() +#import +#elif __has_include(“RCTViewManager.h”) +#import “RCTViewManager.h” +#else +#import “React/RCTViewManager.h” // Required when used as a Pod in a Swift project +#endif + +// Export a native module +// https://facebook.github.io/react-native/docs/native-modules-ios.html#exporting-swift +@interface RCT_EXTERN_MODULE(TestModule, RCTViewManager) + +// Map native properties to React Component props +// https://facebook.github.io/react-native/docs/native-components-ios.html#properties +RCT_EXPORT_VIEW_PROPERTY(\"exampleProp\", NSString) + +@end +" +`; + +exports[`iOS/Swift: UI Components creates a Template-Bridging-Header.h 1`] = ` +"// Created by react-native-create-bridge +// TestModule-Bridging-Header.h + +// import RCTBridgeModule +#if __has_include() +#import +#elif __has_include(“RCTBridgeModule.h”) +#import “RCTBridgeModule.h” +#else +#import “React/RCTBridgeModule.h” // Required when used as a Pod in a Swift project +#endif + +// import RCTEventEmitter +#if __has_include() +#import +#elif __has_include(“RCTViewManager.h”) +#import “RCTViewManager.h” +#else +#import “React/RCTViewManager.h” // Required when used as a Pod in a Swift project +#endif" +`; + +exports[`iOS/Swift: UI Components creates a TemplateManager.swift 1`] = ` +"// Created by react-native-create-bridge + +import Foundation +import UIKit + +@objc(TestModule) +class TestModuleManager : RCTViewManager { + // Return the native view that represents your React component + override func view() -> UIView! { + return TestModuleView() + } + + // Export constants to use in your native module + override func constantsToExport() -> [String : Any]! { + return [\"EXAMPLE_CONSTANT\": \"example\"] + } + +} +" +`; + +exports[`iOS/Swift: UI Components creates a TemplateView.swift 1`] = ` +"// Created by react-native-create-bridge + +import Foundation + +@objc(TestModuleView) +class TestModuleView : UIView { + + override init(frame: CGRect) { + super.init(frame: frame); + self.frame = frame; + } + + required init?(coder aDecoder: NSCoder) { + fatalError(\"init(coder:) has not been implemented\") + } + + func setExampleProp(exampleProp: NSString) { + self.exampleProp = exampleProp + } + +}" +`; diff --git a/__tests__/__snapshots__/javascript.test.js.snap b/__tests__/__snapshots__/javascript.test.js.snap index 5418be6..23ec2b7 100644 --- a/__tests__/__snapshots__/javascript.test.js.snap +++ b/__tests__/__snapshots__/javascript.test.js.snap @@ -1,4 +1,4 @@ -exports[`JS creates a TemplateNativeModule.js 1`] = ` +exports[`JS: Combined creates a TemplateNativeModule.js 1`] = ` "// Created by react-native-create-bridge import { NativeModules } from \'react-native\' @@ -15,15 +15,15 @@ export default { " `; -exports[`JS creates a TemplateNativeView.js 1`] = ` +exports[`JS: Combined creates a TemplateNativeView.js 1`] = ` "// Created by react-native-create-bridge import React, { Component } from \'react\' import { requireNativeComponent } from \'react-native\' -const TestModule = requireNativeComponent(\'TestModule\', null) +const TestModule = requireNativeComponent(\'TestModule\', TestModuleView) -class TestModuleView extends Component { +export default class TestModuleView extends Component { render () { return } @@ -32,7 +32,42 @@ class TestModuleView extends Component { TestModuleView.propTypes = { exampleProp: React.PropTypes.any } +" +`; + +exports[`JS: Native Modules creates a TemplateNativeModule.js 1`] = ` +"// Created by react-native-create-bridge + +import { NativeModules } from \'react-native\' + +const { TestModule } = NativeModules -export default TestModuleView +export default { + exampleMethod () { + return TestModule.exampleMethod() + }, + + EXAMPLE_CONSTANT: TestModule.EXAMPLE_CONSTANT +} +" +`; + +exports[`JS: UI Components creates a TemplateNativeView.js 1`] = ` +"// Created by react-native-create-bridge + +import React, { Component } from \'react\' +import { requireNativeComponent } from \'react-native\' + +const TestModule = requireNativeComponent(\'TestModule\', TestModuleView) + +export default class TestModuleView extends Component { + render () { + return + } +} + +TestModuleView.propTypes = { + exampleProp: React.PropTypes.string +} " `; diff --git a/__tests__/android-java.js b/__tests__/android-java.js index 8301373..cc988d3 100644 --- a/__tests__/android-java.js +++ b/__tests__/android-java.js @@ -1,39 +1,111 @@ -import path from "path"; -import { readFile, parseFile } from "../src/file-operations"; +import path from 'path'; +import { readFile, parseFile } from '../src/file-operations'; -describe("Android/Java", () => { - const templateName = "TestModule"; - const readDirPath = path.join(__dirname, "..", "templates", "android-java"); +describe('Android/Java: UI Components', () => { + const templateName = 'TestModule'; + const readDirPath = path.join( + __dirname, + '..', + 'templates', + 'ui-components', + 'android-java', + ); - it("creates a TemplateManager.java", async () => { - const fileData = await readFile("TemplateManager.java", readDirPath); + it('creates a TemplateManager.java', async () => { + const fileData = await readFile('TemplateManager.java', readDirPath); const parsedFile = parseFile( fileData, templateName, templateName.toLowerCase(), - "testapp" + 'testapp', ); expect(parsedFile).toMatchSnapshot(); }); - it("creates a TemplateModule.java", async () => { - const fileData = await readFile("TemplateModule.java", readDirPath); + it('creates a TemplatePackage.java', async () => { + const fileData = await readFile('TemplatePackage.java', readDirPath); const parsedFile = parseFile( fileData, templateName, templateName.toLowerCase(), - "testapp" + 'testapp', + ); + expect(parsedFile).toMatchSnapshot(); + }); +}); + +describe('Android/Java: Native Modules', () => { + const templateName = 'TestModule'; + const readDirPath = path.join( + __dirname, + '..', + 'templates', + 'modules', + 'android-java', + ); + + it('creates a TemplateModule.java', async () => { + const fileData = await readFile('TemplateModule.java', readDirPath); + const parsedFile = parseFile( + fileData, + templateName, + templateName.toLowerCase(), + 'testapp', + ); + expect(parsedFile).toMatchSnapshot(); + }); + + it('creates a TemplatePackage.java', async () => { + const fileData = await readFile('TemplatePackage.java', readDirPath); + const parsedFile = parseFile( + fileData, + templateName, + templateName.toLowerCase(), + 'testapp', + ); + expect(parsedFile).toMatchSnapshot(); + }); +}); + +describe('Android/Java: Combined', () => { + const templateName = 'TestModule'; + const readDirPath = path.join( + __dirname, + '..', + 'templates', + 'combined', + 'android-java', + ); + + it('creates a TemplateManager.java', async () => { + const fileData = await readFile('TemplateManager.java', readDirPath); + const parsedFile = parseFile( + fileData, + templateName, + templateName.toLowerCase(), + 'testapp', + ); + expect(parsedFile).toMatchSnapshot(); + }); + + it('creates a TemplateModule.java', async () => { + const fileData = await readFile('TemplateModule.java', readDirPath); + const parsedFile = parseFile( + fileData, + templateName, + templateName.toLowerCase(), + 'testapp', ); expect(parsedFile).toMatchSnapshot(); }); - it("creates a TemplatePackage.java", async () => { - const fileData = await readFile("TemplatePackage.java", readDirPath); + it('creates a TemplatePackage.java', async () => { + const fileData = await readFile('TemplatePackage.java', readDirPath); const parsedFile = parseFile( fileData, templateName, templateName.toLowerCase(), - "testapp" + 'testapp', ); expect(parsedFile).toMatchSnapshot(); }); diff --git a/__tests__/android-kotlin.js b/__tests__/android-kotlin.js index f8bebc9..2ecbfa4 100644 --- a/__tests__/android-kotlin.js +++ b/__tests__/android-kotlin.js @@ -1,39 +1,111 @@ -import path from "path"; -import { readFile, parseFile } from "../src/file-operations"; +import path from 'path'; +import { readFile, parseFile } from '../src/file-operations'; -describe("Android/Kotlin", () => { - const templateName = "TestModule"; - const readDirPath = path.join(__dirname, "..", "templates", "android-kotlin"); +describe('Android/Kotlin: UI Components', () => { + const templateName = 'TestModule'; + const readDirPath = path.join( + __dirname, + '..', + 'templates', + 'ui-components', + 'android-kotlin', + ); - it("creates a TemplateManager.kt", async () => { - const fileData = await readFile("TemplateManager.kt", readDirPath); + it('creates a TemplateManager.kt', async () => { + const fileData = await readFile('TemplateManager.kt', readDirPath); const parsedFile = parseFile( fileData, templateName, templateName.toLowerCase(), - "testapp" + 'testapp', ); expect(parsedFile).toMatchSnapshot(); }); - it("creates a TemplateModule.kt", async () => { - const fileData = await readFile("TemplateModule.kt", readDirPath); + it('creates a TemplatePackage.kt', async () => { + const fileData = await readFile('TemplatePackage.kt', readDirPath); const parsedFile = parseFile( fileData, templateName, templateName.toLowerCase(), - "testapp" + 'testapp', + ); + expect(parsedFile).toMatchSnapshot(); + }); +}); + +describe('Android/Kotlin: Native Modules', () => { + const templateName = 'TestModule'; + const readDirPath = path.join( + __dirname, + '..', + 'templates', + 'modules', + 'android-kotlin', + ); + + it('creates a TemplateModule.kt', async () => { + const fileData = await readFile('TemplateModule.kt', readDirPath); + const parsedFile = parseFile( + fileData, + templateName, + templateName.toLowerCase(), + 'testapp', + ); + expect(parsedFile).toMatchSnapshot(); + }); + + it('creates a TemplatePackage.kt', async () => { + const fileData = await readFile('TemplatePackage.kt', readDirPath); + const parsedFile = parseFile( + fileData, + templateName, + templateName.toLowerCase(), + 'testapp', + ); + expect(parsedFile).toMatchSnapshot(); + }); +}); + +describe('Android/Kotlin: Combined', () => { + const templateName = 'TestModule'; + const readDirPath = path.join( + __dirname, + '..', + 'templates', + 'combined', + 'android-kotlin', + ); + + it('creates a TemplateManager.kt', async () => { + const fileData = await readFile('TemplateManager.kt', readDirPath); + const parsedFile = parseFile( + fileData, + templateName, + templateName.toLowerCase(), + 'testapp', + ); + expect(parsedFile).toMatchSnapshot(); + }); + + it('creates a TemplateModule.kt', async () => { + const fileData = await readFile('TemplateModule.kt', readDirPath); + const parsedFile = parseFile( + fileData, + templateName, + templateName.toLowerCase(), + 'testapp', ); expect(parsedFile).toMatchSnapshot(); }); - it("creates a TemplatePackage.kt", async () => { - const fileData = await readFile("TemplatePackage.kt", readDirPath); + it('creates a TemplatePackage.kt', async () => { + const fileData = await readFile('TemplatePackage.kt', readDirPath); const parsedFile = parseFile( fileData, templateName, templateName.toLowerCase(), - "testapp" + 'testapp', ); expect(parsedFile).toMatchSnapshot(); }); diff --git a/__tests__/ios-objc.test.js b/__tests__/ios-objc.test.js index 06e43a2..1114047 100644 --- a/__tests__/ios-objc.test.js +++ b/__tests__/ios-objc.test.js @@ -1,24 +1,83 @@ -import path from 'path' -import { readFile, parseFile } from '../src/file-operations' - -describe('iOS/Obj-C', () => { - const templateName = 'TestModule' - const readDirPath = path.join(__dirname, '..', 'templates', 'ios-objc') - const pkg = { - dependencies: { - 'react-native': '0.4' - } - } +import path from 'path'; +import { readFile, parseFile } from '../src/file-operations'; + +describe('iOS/Obj-C: UI Components', () => { + const templateName = 'TestModule'; + const readDirPath = path.join( + __dirname, + '..', + 'templates', + 'ui-components', + 'ios-objc', + ); + + it('creates a Template.h', async () => { + const fileData = await readFile('Template.h', readDirPath); + const parsedFile = parseFile(fileData, templateName); + expect(parsedFile).toMatchSnapshot(); + }); + + it('creates a Template.m', async () => { + const fileData = await readFile('Template.m', readDirPath); + const parsedFile = parseFile(fileData, templateName); + expect(parsedFile).toMatchSnapshot(); + }); + + it('creates a TemplateManager.h', async () => { + const fileData = await readFile('TemplateManager.h', readDirPath); + const parsedFile = parseFile(fileData, templateName); + expect(parsedFile).toMatchSnapshot(); + }); + + it('creates a TemplateManager.m', async () => { + const fileData = await readFile('TemplateManager.m', readDirPath); + const parsedFile = parseFile(fileData, templateName); + expect(parsedFile).toMatchSnapshot(); + }); +}); + +describe('iOS/Obj-C: Native Modules', () => { + const templateName = 'TestModule'; + const readDirPath = path.join( + __dirname, + '..', + 'templates', + 'modules', + 'ios-objc', + ); + + it('creates a Template.h', async () => { + const fileData = await readFile('Template.h', readDirPath); + const parsedFile = parseFile(fileData, templateName); + expect(parsedFile).toMatchSnapshot(); + }); + + it('creates a Template.m', async () => { + const fileData = await readFile('Template.m', readDirPath); + const parsedFile = parseFile(fileData, templateName); + expect(parsedFile).toMatchSnapshot(); + }); +}); + +describe('iOS/Obj-C: Combined', () => { + const templateName = 'TestModule'; + const readDirPath = path.join( + __dirname, + '..', + 'templates', + 'combined', + 'ios-objc', + ); it('creates a Template.h', async () => { - const fileData = await readFile('Template.h', readDirPath) - const parsedFile = parseFile(fileData, templateName) - expect(parsedFile).toMatchSnapshot() - }) + const fileData = await readFile('Template.h', readDirPath); + const parsedFile = parseFile(fileData, templateName); + expect(parsedFile).toMatchSnapshot(); + }); it('creates a TemplateManager.m', async () => { - const fileData = await readFile('TemplateManager.m', readDirPath) - const parsedFile = parseFile(fileData, templateName) - expect(parsedFile).toMatchSnapshot() - }) -}) + const fileData = await readFile('TemplateManager.m', readDirPath); + const parsedFile = parseFile(fileData, templateName); + expect(parsedFile).toMatchSnapshot(); + }); +}); diff --git a/__tests__/ios-swift.test.js b/__tests__/ios-swift.test.js index 35fa616..05b258d 100644 --- a/__tests__/ios-swift.test.js +++ b/__tests__/ios-swift.test.js @@ -1,24 +1,95 @@ -import path from 'path' -import { readFile, parseFile } from '../src/file-operations' - -describe('iOS/Swift', () => { - const templateName = 'TestModule' - const readDirPath = path.join(__dirname, '..', 'templates', 'ios-swift') - const pkg = { - dependencies: { - 'react-native': '0.4' - } - } +import path from 'path'; +import { readFile, parseFile } from '../src/file-operations'; + +describe('iOS/Swift: UI Components', () => { + const templateName = 'TestModule'; + const readDirPath = path.join( + __dirname, + '..', + 'templates', + 'ui-components', + 'ios-swift', + ); + + it('creates a Template-Bridging-Header.h', async () => { + const fileData = await readFile('Template-Bridging-Header.h', readDirPath); + const parsedFile = parseFile(fileData, templateName); + expect(parsedFile).toMatchSnapshot(); + }); + + it('creates a Template.m', async () => { + const fileData = await readFile('Template.m', readDirPath); + const parsedFile = parseFile(fileData, templateName); + expect(parsedFile).toMatchSnapshot(); + }); + + it('creates a TemplateManager.swift', async () => { + const fileData = await readFile('TemplateManager.swift', readDirPath); + const parsedFile = parseFile(fileData, templateName); + expect(parsedFile).toMatchSnapshot(); + }); + + it('creates a TemplateView.swift', async () => { + const fileData = await readFile('TemplateView.swift', readDirPath); + const parsedFile = parseFile(fileData, templateName); + expect(parsedFile).toMatchSnapshot(); + }); +}); + +describe('iOS/Swift: Native Modules', () => { + const templateName = 'TestModule'; + const readDirPath = path.join( + __dirname, + '..', + 'templates', + 'modules', + 'ios-swift', + ); + + it('creates a Template-Bridging-Header.h', async () => { + const fileData = await readFile('Template-Bridging-Header.h', readDirPath); + const parsedFile = parseFile(fileData, templateName); + expect(parsedFile).toMatchSnapshot(); + }); + + it('creates a Template.m', async () => { + const fileData = await readFile('Template.m', readDirPath); + const parsedFile = parseFile(fileData, templateName); + expect(parsedFile).toMatchSnapshot(); + }); + + it('creates a Template.swift', async () => { + const fileData = await readFile('Template.swift', readDirPath); + const parsedFile = parseFile(fileData, templateName); + expect(parsedFile).toMatchSnapshot(); + }); +}); + +describe('iOS/Swift: Combined', () => { + const templateName = 'TestModule'; + const readDirPath = path.join( + __dirname, + '..', + 'templates', + 'combined', + 'ios-swift', + ); + + it('creates a Template-Bridging-Header.h', async () => { + const fileData = await readFile('Template-Bridging-Header.h', readDirPath); + const parsedFile = parseFile(fileData, templateName); + expect(parsedFile).toMatchSnapshot(); + }); it('creates a Template.m', async () => { - const fileData = await readFile('Template.m', readDirPath) - const parsedFile = parseFile(fileData, templateName) - expect(parsedFile).toMatchSnapshot() - }) + const fileData = await readFile('Template.m', readDirPath); + const parsedFile = parseFile(fileData, templateName); + expect(parsedFile).toMatchSnapshot(); + }); it('creates a TemplateManager.swift', async () => { - const fileData = await readFile('TemplateManager.swift', readDirPath) - const parsedFile = parseFile(fileData, templateName) - expect(parsedFile).toMatchSnapshot() - }) -}) + const fileData = await readFile('TemplateManager.swift', readDirPath); + const parsedFile = parseFile(fileData, templateName); + expect(parsedFile).toMatchSnapshot(); + }); +}); diff --git a/__tests__/javascript.test.js b/__tests__/javascript.test.js index 25de0cd..f2cef5f 100644 --- a/__tests__/javascript.test.js +++ b/__tests__/javascript.test.js @@ -1,19 +1,47 @@ -import path from 'path' -import { readFile, parseFile } from '../src/file-operations' +import path from 'path'; +import { readFile, parseFile } from '../src/file-operations'; -describe('JS', () => { - const templateName = 'TestModule' - const readDirPath = path.join(__dirname, '..', 'templates', 'js') +describe('JS: Combined', () => { + const templateName = 'TestModule'; + const readDirPath = path.join(__dirname, '..', 'templates', 'combined', 'js'); it('creates a TemplateNativeModule.js', async () => { - const fileData = await readFile('TemplateNativeModule.js', readDirPath) - const parsedFile = parseFile(fileData, templateName) - expect(parsedFile).toMatchSnapshot() - }) + const fileData = await readFile('TemplateNativeModule.js', readDirPath); + const parsedFile = parseFile(fileData, templateName); + expect(parsedFile).toMatchSnapshot(); + }); it('creates a TemplateNativeView.js', async () => { - const fileData = await readFile('TemplateNativeView.js', readDirPath) - const parsedFile = parseFile(fileData, templateName) - expect(parsedFile).toMatchSnapshot() - }) -}) + const fileData = await readFile('TemplateNativeView.js', readDirPath); + const parsedFile = parseFile(fileData, templateName); + expect(parsedFile).toMatchSnapshot(); + }); +}); + +describe('JS: Native Modules', () => { + const templateName = 'TestModule'; + const readDirPath = path.join(__dirname, '..', 'templates', 'modules', 'js'); + + it('creates a TemplateNativeModule.js', async () => { + const fileData = await readFile('TemplateNativeModule.js', readDirPath); + const parsedFile = parseFile(fileData, templateName); + expect(parsedFile).toMatchSnapshot(); + }); +}); + +describe('JS: UI Components', () => { + const templateName = 'TestModule'; + const readDirPath = path.join( + __dirname, + '..', + 'templates', + 'ui-components', + 'js', + ); + + it('creates a TemplateNativeView.js', async () => { + const fileData = await readFile('TemplateNativeView.js', readDirPath); + const parsedFile = parseFile(fileData, templateName); + expect(parsedFile).toMatchSnapshot(); + }); +}); diff --git a/templates/combined/ios-swift/Template-Bridging-Header.h b/templates/combined/ios-swift/Template-Bridging-Header.h new file mode 100644 index 0000000..46a26b7 --- /dev/null +++ b/templates/combined/ios-swift/Template-Bridging-Header.h @@ -0,0 +1,20 @@ +// Created by react-native-create-bridge +// {{template}}-Bridging-Header.h + +// import RCTBridgeModule +#if __has_include() +#import +#elif __has_include(“RCTBridgeModule.h”) +#import “RCTBridgeModule.h” +#else +#import “React/RCTBridgeModule.h” // Required when used as a Pod in a Swift project +#endif + +// import RCTEventEmitter +#if __has_include() +#import +#elif __has_include(“RCTViewManager.h”) +#import “RCTViewManager.h” +#else +#import “React/RCTViewManager.h” // Required when used as a Pod in a Swift project +#endif \ No newline at end of file diff --git a/templates/modules/ios-objc/TemplateManager.m b/templates/modules/ios-objc/Template.m similarity index 100% rename from templates/modules/ios-objc/TemplateManager.m rename to templates/modules/ios-objc/Template.m