Skip to content

Commit

Permalink
chore(test): add android unit tests (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz1913 authored Oct 3, 2023
1 parent b2b7576 commit 4f04166
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 11 deletions.
14 changes: 12 additions & 2 deletions src/android-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export const createPluginPackageFile = (
spinner.succeed();
};

export const displayFinishStepsForAndroidApplicationPlugin = (pluginName: string) => {
const displayFinishStepsForAndroidApplicationPlugin = (pluginName: string) => {
console.log(kleur.gray(`${kleur.yellow(`Finish Android setup with registering ${pluginName + 'PluginPackage'}
in getPackages method, in your MainApplication.(java|kt)`)}
// MainApplication.(java|kt)
Expand All @@ -319,7 +319,7 @@ protected List<ReactPackage> getPackages() {
}`.trim()));
};

export const displayFinishStepsForAndroidLibraryPlugin = () => {
const displayFinishStepsForAndroidLibraryPlugin = () => {
console.log(kleur.gray(`${kleur.yellow(`Finish setup for your Android library
with adding "VisionCamera" dependency in your library's "build.gradle" file:`)}
// build.gradle
Expand All @@ -332,3 +332,13 @@ dependencies {
${kleur.green('api project(":react-native-vision-camera") // <--- add this')}
}`.trim()));
};

export const printFinishSetupForAndroid = (manifestPath: string, pluginName: string) => {
if (isManifestForApplication(manifestPath)) {
displayFinishStepsForAndroidApplicationPlugin(pluginName);
} else {
displayFinishStepsForAndroidLibraryPlugin();
}

console.log('\n');
};
11 changes: 2 additions & 9 deletions src/android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ import {
createPluginFile,
createPluginPackageFile,
displayExtractPackageNameErrorMessage,
displayFinishStepsForAndroidApplicationPlugin,
displayFinishStepsForAndroidLibraryPlugin,
extractPackageName,
getSourceSetDirectory,
isManifestForApplication,
printFinishSetupForAndroid,
suggestAndroidManifest,
} from './android-utils';
import { getPromptResponse, printFinishSetup, spinner } from './common';
Expand Down Expand Up @@ -138,12 +136,7 @@ export async function androidCommandHandler(argv: Arguments<unknown>) {
spinner.stop();

console.log('\n');
if (isManifestForApplication(manifestPath)) {
displayFinishStepsForAndroidApplicationPlugin(pluginName);
} else {
displayFinishStepsForAndroidLibraryPlugin();
}
printFinishSetupForAndroid(manifestPath, pluginName);

console.log('\n');
printFinishSetup(methodName);
}
100 changes: 100 additions & 0 deletions test/android-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { afterEach, describe, expect, test } from '@jest/globals';
import mockFS from 'mock-fs';

import { extractPackageName } from '../src/android-utils';

const PACKAGE_NAME = 'com.myawesomeapp';
const PROJECT_FILE_PATH = '/path/to/project';
const BUILD_GRADLE_FILE = `
android {
ndkVersion rootProject.ext.ndkVersion
compileSdk rootProject.ext.compileSdkVersion
namespace "${PACKAGE_NAME}"
defaultConfig {
applicationId "com.helloworld"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
}
}
`;
const BUILD_GRADLE_KTS_FILE = `
android {
val safeExtGet: (prop: String, fallback: Any) -> Any? by project.extra
compileSdk = safeExtGet("compileSdkVersion", 33) as Int?
namespace = "${PACKAGE_NAME}"
defaultConfig {
minSdk = safeExtGet("minSdkVersion", 21) as Int?
targetSdk = safeExtGet("targetSdkVersion", 33) as Int?
buildConfigField("boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString())
}
}
`;
const ANDROID_MANIFEST_FILE = `<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="${PACKAGE_NAME}">
</manifest>
`;

afterEach(() => {
mockFS.restore();
});

describe('extractPackageName', () => {
test('should retrieve package name from namespace value in build.gradle', () => {
mockFS({
[PROJECT_FILE_PATH]: {
android: {
app: {
'build.gradle': BUILD_GRADLE_FILE,
},
},
},
});
expect(
extractPackageName(
`${PROJECT_FILE_PATH}/android/app`,
`${PROJECT_FILE_PATH}/android/app/src/main/AndroidManifest.xml`
)
).toBe(PACKAGE_NAME);
});
test('should retrieve package name from namespace value in build.gradle.kts', () => {
mockFS({
[PROJECT_FILE_PATH]: {
android: {
app: {
'build.gradle.kts': BUILD_GRADLE_KTS_FILE,
},
},
},
});
expect(
extractPackageName(
`${PROJECT_FILE_PATH}/android/app`,
`${PROJECT_FILE_PATH}/android/app/src/main/AndroidManifest.xml`
)
).toBe(PACKAGE_NAME);
});
test('should retrieve package name from package attribute in AndroidManifest.xml', () => {
mockFS({
[PROJECT_FILE_PATH]: {
android: {
app: {
src: {
main: {
'AndroidManifest.xml': ANDROID_MANIFEST_FILE,
},
},
},
},
},
});
extractPackageName(`${PROJECT_FILE_PATH}/android/app`, `${PROJECT_FILE_PATH}/android/app/src/main/AndroidManifest.xml`);
});
});

0 comments on commit 4f04166

Please sign in to comment.