Skip to content

Commit b1d8584

Browse files
authored
Basic test suite for Android tests (#32)
* Add basic tests for Android bridge * Use setup to clean up tests * Specify version in gradle.properties
1 parent 8456bde commit b1d8584

File tree

10 files changed

+152
-44
lines changed

10 files changed

+152
-44
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ jobs:
5050

5151
- run: yarn test
5252

53-
build-android:
54-
name: Build Android Sample App
53+
test-android:
54+
name: Run Android Tests
5555
runs-on: shopify-ubuntu-latest
5656
steps:
5757
- name: Checkout
@@ -85,15 +85,15 @@ jobs:
8585
~/.gradle/caches
8686
sample/android/.gradle/wrapper
8787
sample/android/.gradle/caches
88-
key:
88+
key: |
8989
${{ runner.os }}-gradle-${{ hashFiles('sample/android/gradle/wrapper/gradle-wrapper.properties') }}
9090
restore-keys: |
9191
${{ runner.os }}-gradle-
9292
93-
- name: Build sample for Android
93+
- name: Run Android tests
9494
# If turbo has already cached the build it will return instantly here
9595
run: |
96-
yarn turbo run build:android --cache-dir=".turbo"
96+
yarn turbo run test:android --cache-dir=".turbo"
9797
9898
build-ios:
9999
name: Build iOS Sample App

modules/react-native-shopify-checkout-kit/android/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
}
66

77
dependencies {
8-
classpath "com.android.tools.build:gradle:7.2.1"
8+
classpath "com.android.tools.build:gradle:7.4.2"
99
}
1010
}
1111

@@ -92,5 +92,6 @@ dependencies {
9292
implementation "com.facebook.react:react-native:+"
9393
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.7.0"
9494
implementation("com.shopify:checkout-kit:${SHOPIFY_CHECKOUT_SDK_VERSION}")
95+
debugImplementation("com.shopify:checkout-kit:${SHOPIFY_CHECKOUT_SDK_VERSION}")
9596
}
9697

modules/react-native-shopify-checkout-kit/android/src/main/java/com/shopifycheckoutkit/ShopifyCheckoutKitModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ of this software and associated documentation files (the "Software"), to deal
4444
public class ShopifyCheckoutKitModule extends ReactContextBaseJavaModule {
4545
private static final String MODULE_NAME = "ShopifyCheckoutKit";
4646

47-
private static Configuration checkoutConfig = new Configuration();
47+
public static Configuration checkoutConfig = new Configuration();
4848

4949
private ReactApplicationContext reactContext;
5050

sample/android/app/build.gradle

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -69,53 +69,61 @@ def enableProguardInReleaseBuilds = false
6969
def jscFlavor = 'org.webkit:android-jsc:+'
7070

7171
android {
72-
ndkVersion rootProject.ext.ndkVersion
72+
ndkVersion rootProject.ext.ndkVersion
7373

74-
compileSdkVersion rootProject.ext.compileSdkVersion
74+
compileSdkVersion rootProject.ext.compileSdkVersion
7575

76-
lintOptions {
77-
checkDependencies false
76+
lintOptions {
77+
checkDependencies false
78+
}
79+
tasks.whenTaskAdded { task ->
80+
if (task.name == 'lintAnalyzeDebug') {
81+
task.dependsOn 'copyReactNativeVectorIconFonts'
7882
}
79-
tasks.whenTaskAdded { task ->
80-
if (task.name == 'lintAnalyzeDebug') {
81-
task.dependsOn 'copyReactNativeVectorIconFonts'
82-
}
83+
}
84+
85+
namespace "com.reactnative"
86+
defaultConfig {
87+
applicationId "com.reactnative"
88+
minSdkVersion rootProject.ext.minSdkVersion
89+
targetSdkVersion rootProject.ext.targetSdkVersion
90+
versionCode 1
91+
versionName "1.0"
92+
}
93+
signingConfigs {
94+
debug {
95+
storeFile file('debug.keystore')
96+
storePassword 'android'
97+
keyAlias 'androiddebugkey'
98+
keyPassword 'android'
8399
}
84-
85-
namespace "com.reactnative"
86-
defaultConfig {
87-
applicationId "com.reactnative"
88-
minSdkVersion rootProject.ext.minSdkVersion
89-
targetSdkVersion rootProject.ext.targetSdkVersion
90-
versionCode 1
91-
versionName "1.0"
92-
}
93-
signingConfigs {
94-
debug {
95-
storeFile file('debug.keystore')
96-
storePassword 'android'
97-
keyAlias 'androiddebugkey'
98-
keyPassword 'android'
99-
}
100+
}
101+
buildTypes {
102+
debug {
103+
signingConfig signingConfigs.debug
100104
}
101-
buildTypes {
102-
debug {
103-
signingConfig signingConfigs.debug
104-
}
105-
release {
106-
// Caution! In production, you need to generate your own keystore file.
107-
// see https://reactnative.dev/docs/signed-apk-android.
108-
signingConfig signingConfigs.debug
109-
minifyEnabled enableProguardInReleaseBuilds
110-
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
111-
}
105+
release {
106+
// Caution! In production, you need to generate your own keystore file.
107+
// see https://reactnative.dev/docs/signed-apk-android.
108+
signingConfig signingConfigs.debug
109+
minifyEnabled enableProguardInReleaseBuilds
110+
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
112111
}
112+
}
113113
}
114114

115115
dependencies {
116116
// The version of react-native is set by the React Native Gradle Plugin
117117
implementation("com.facebook.react:react-android")
118118

119+
// Test dependencies
120+
testImplementation "junit:junit:4.13.2"
121+
testImplementation "org.mockito:mockito-core:4.11.0"
122+
androidTestImplementation "org.mockito:mockito-android:4.11.0"
123+
testImplementation "org.mockito:mockito-inline:4.6.1"
124+
testImplementation "org.assertj:assertj-core:3.24.2"
125+
testImplementation("com.shopify:checkout-kit:${SHOPIFY_CHECKOUT_SDK_VERSION}")
126+
119127
debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}")
120128
debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
121129
exclude group:'com.squareup.okhttp3', module:'okhttp'
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.reactnative;
2+
3+
import androidx.activity.ComponentActivity;
4+
5+
import com.facebook.react.bridge.JavaOnlyMap;
6+
import com.facebook.react.bridge.ReactApplicationContext;
7+
import com.shopify.checkoutkit.ShopifyCheckoutKit;
8+
import com.shopify.reactnative.checkoutkit.ShopifyCheckoutKitModule;
9+
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
import org.mockito.ArgumentCaptor;
14+
import org.mockito.Captor;
15+
import org.mockito.Mock;
16+
import org.mockito.MockedStatic;
17+
import org.mockito.Mockito;
18+
import org.mockito.junit.MockitoJUnitRunner;
19+
20+
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertFalse;
22+
import static org.junit.Assert.assertTrue;
23+
import static org.mockito.Mockito.*;
24+
25+
@RunWith(MockitoJUnitRunner.class)
26+
public class ShopifyCheckoutKitModuleTest {
27+
@Mock
28+
private ReactApplicationContext mockReactContext;
29+
30+
@Mock
31+
private ComponentActivity mockComponentActivity;
32+
33+
@Captor
34+
ArgumentCaptor<Runnable> runnableCaptor;
35+
36+
private ShopifyCheckoutKitModule shopifyCheckoutKitModule;
37+
38+
@Before
39+
public void setup() {
40+
when(mockReactContext.getCurrentActivity()).thenReturn(mockComponentActivity);
41+
shopifyCheckoutKitModule = new ShopifyCheckoutKitModule(mockReactContext);
42+
}
43+
44+
@Test
45+
public void callsPresentWithCheckoutURL() {
46+
try (MockedStatic<ShopifyCheckoutKit> mockedShopifyCheckoutKit = Mockito.mockStatic(ShopifyCheckoutKit.class)) {
47+
String checkoutUrl = "https://shopify.com";
48+
shopifyCheckoutKitModule.present(checkoutUrl);
49+
50+
verify(mockComponentActivity).runOnUiThread(runnableCaptor.capture());
51+
runnableCaptor.getValue().run();
52+
53+
mockedShopifyCheckoutKit.verify(() -> {
54+
ShopifyCheckoutKit.present(eq(checkoutUrl), any(), any());
55+
});
56+
}
57+
}
58+
59+
@Test
60+
public void callsPreloadWithCheckoutURL() {
61+
try (MockedStatic<ShopifyCheckoutKit> mockedShopifyCheckoutKit = Mockito.mockStatic(ShopifyCheckoutKit.class)) {
62+
String checkoutUrl = "https://shopify.com";
63+
shopifyCheckoutKitModule.preload(checkoutUrl);
64+
65+
mockedShopifyCheckoutKit.verify(() -> {
66+
ShopifyCheckoutKit.preload(eq(checkoutUrl), any());
67+
});
68+
}
69+
}
70+
71+
@Test
72+
public void configuresInternalConfig() {
73+
assertFalse(ShopifyCheckoutKitModule.checkoutConfig.getPreloading().getEnabled());
74+
75+
JavaOnlyMap updatedConfig = new JavaOnlyMap();
76+
updatedConfig.putBoolean("preloading", true);
77+
updatedConfig.putString("colorScheme", "dark");
78+
shopifyCheckoutKitModule.configure(updatedConfig);
79+
80+
boolean preloadingEnabled = ShopifyCheckoutKitModule.checkoutConfig.getPreloading().getEnabled();
81+
String colorScheme = ShopifyCheckoutKitModule.checkoutConfig.getColorScheme().getId();
82+
83+
assertTrue(preloadingEnabled);
84+
assertEquals(colorScheme, "dark");
85+
}
86+
}

sample/android/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
2-
32
buildscript {
43

54
ext {

sample/android/gradle.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,6 @@ hermesEnabled=true
4545

4646

4747
android.disableAutomaticComponentCreation=true
48+
49+
# Version of Shopify Checkout SDK to use with React Native
50+
SHOPIFY_CHECKOUT_SDK_VERSION=0.3.3

sample/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"lint": "yarn typecheck && eslint .",
1212
"ios": "react-native run-ios --simulator 'iPhone 14 Pro'",
1313
"start": "react-native start",
14-
"typecheck": "tsc --noEmit"
14+
"typecheck": "tsc --noEmit",
15+
"test:android": "sh ./scripts/test_android"
1516
},
1617
"dependencies": {
1718
"@apollo/client": "^3.8.6",

sample/scripts/test_android

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
cd android
6+
7+
./gradlew test --no-daemon --console=plain -PreactNativeArchitectures=arm64-v8a

turbo.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
"build:ios": {
1515
"dependsOn": ["build", "lint"],
1616
"outputs": ["sample/ios/build"]
17+
},
18+
"test:android": {
19+
"dependsOn": ["build", "lint"]
1720
}
1821
}
1922
}

0 commit comments

Comments
 (0)