Skip to content

Commit

Permalink
test android native module
Browse files Browse the repository at this point in the history
  • Loading branch information
owencraston committed May 4, 2023
1 parent 0069af2 commit 0a95a9d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 14 deletions.
3 changes: 3 additions & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@ dependencies {
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"
implementation "com.android.installreferrer:installreferrer:2.2"
implementation 'org.apache.commons:commons-compress:1.22'
androidTestImplementation 'org.mockito:mockito-android:4.2.0'
androidTestImplementation 'androidx.test:core:1.5.0'
androidTestImplementation 'androidx.test:core-ktx:1.5.0'

debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") {
exclude group:'com.facebook.fbjni'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.metamask.nativeModules.RNTarTest;

import androidx.test.core.app.ApplicationProvider;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import java.nio.file.StandardCopyOption;

import io.metamask.nativeModules.RNTar.RNTar;

@RunWith(JUnit4.class)
public class RNTarTest {
private RNTar rNTar;
private ReactApplicationContext reactContext;
private Promise promise;

@Before
public void setUp() {
reactContext = new ReactApplicationContext(ApplicationProvider.getApplicationContext());
rNTar = new RNTar(reactContext);
promise = mock(Promise.class);
}

@Test
public void testUnTar_validTgzFile() throws IOException {
// Prepare a sample .tgz file
InputStream tgzResource = Thread.currentThread().getContextClassLoader().getResourceAsStream("validTgzFile.tgz");
File tgzFile = new File(reactContext.getCacheDir(), "validTgzFile.tgz");
Files.copy(tgzResource, tgzFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
tgzResource.close();
String outputPath = reactContext.getCacheDir().getAbsolutePath() + "/output";

// Call unTar method
rNTar.unTar(tgzFile.getAbsolutePath(), outputPath, promise);

// Verify the promise was resolved
Path expectedDecompressedPath = Paths.get(outputPath, "package");
verify(promise).resolve(expectedDecompressedPath.toString());
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
public class RNTar extends ReactContextBaseJavaModule {
private static String MODULE_NAME = "RNTar";

RNTar(ReactApplicationContext context) {
public RNTar(ReactApplicationContext context) {
super(context);
}

Expand Down
2 changes: 1 addition & 1 deletion app/components/Views/Snaps/SnapsDev.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState, useEffect } from 'react';
import { View, Alert, ScrollView, TextInput, Platform } from 'react-native';
import { View, Alert, ScrollView, TextInput } from 'react-native';
import { useSelector } from 'react-redux';
import { useNavigation } from '@react-navigation/native';

Expand Down
12 changes: 0 additions & 12 deletions app/core/Snaps/location/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ const fetchAndStoreNPMPackage = async (
const filePath = `${targetDir}/archive.tgz`;
const urlToFetch: string =
typeof inputRequest === 'string' ? inputRequest : inputRequest.url;
console.log(SNAPS_NPM_LOG_TAG, 'fetchAndStoreNPMPackage', urlToFetch);

try {
const response: FetchBlobResponse = await config({
Expand All @@ -106,18 +105,7 @@ const fetchAndStoreNPMPackage = async (
}).fetch('GET', urlToFetch);
const dataPath = response.data;
try {
console.log(
SNAPS_NPM_LOG_TAG,
'calling decompressFile with',
dataPath,
targetDir,
);
const decompressedPath = await decompressFile(dataPath, targetDir);
console.log(
SNAPS_NPM_LOG_TAG,
'fetchAndStoreNPMPackage decompressedPath',
decompressedPath,
);
return decompressedPath;
} catch (error) {
Logger.error(
Expand Down

0 comments on commit 0a95a9d

Please sign in to comment.