Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
expect.assertions(5);
nodejs.start = jest.fn();
// This mocks the initial heartbeat from the server
nodejs.channel.once = jest.fn((_, handler) => handler());
// This mocks the server to immediately be in "Listening" state
nodejs.channel.addListener = jest.fn((_, handler) =>
handler(Constants.LISTENING)
);
nodejs.channel.post = jest.fn();
const api = new Api({ baseUrl: "__URL__" });
await expect(api.startServer()).resolves.toBeUndefined();
expect(nodejs.start.mock.calls.length).toBe(1);
expect(nodejs.start.mock.calls[0][0]).toBe("loader.js");
expect(nodejs.channel.post.mock.calls.length).toBe(2);
expect(nodejs.channel.post.mock.calls[1][1]).toBe(
RNFS.ExternalDirectoryPath
);
});
androidUpdate = async () => {
let _this = this
const {url, filename, version} = this.fetchRes
// 按照目录/包名/文件名 存放,生成md5文件标识
this.filePath = `${RNFS.ExternalDirectoryPath}/${filename}${version}.apk`
// 检查包是否已经下载过,如果有,则直接安装
let exist = await RNFS.exists(this.filePath)
if (exist) {
RNUpdateApp.install(this.filePath)
this.hideModal()
return
}
// 下载apk并安装
RNFS.downloadFile({
fromUrl: url,
toFile: this.filePath,
progressDivider: 2, // 节流
begin(res) {
_this.jobId = res.jobId // 设置jobId,用于暂停和恢复下载任务
export const generateLocalPath = async (url, format) => {
try {
const ABSOLUTEPATH = VALUE.ios
? REACTNATIVEFS.LibraryDirectoryPath
: REACTNATIVEFS.ExternalDirectoryPath;
const STORAGEPATH = `${ABSOLUTEPATH}/${Date.now()}.${format}`;
const { statusCode } = await REACTNATIVEFS.downloadFile({
fromUrl: url,
toFile: STORAGEPATH
}).promise;
if (statusCode === 200) return `file://${STORAGEPATH}`;
return false;
} catch (error) {
console.error('生成本地路径异常', error);
}
};
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View,
Button,
NativeModules,
} from 'react-native';
import ViewShot from "react-native-view-shot";
import RNFS from "react-native-fs";
const PrinterManager = NativeModules.PrinterManager;
const imageType = "png";
const imagePath = `${RNFS.ExternalDirectoryPath}/image.${imageType}`;
export default class App extends React.Component {
constructor(props) {
super(props);
}
captureView = async () => {
this.refs.viewShot.capture().then(uri => {
RNFS.moveFile(uri, imagePath)
.then((success) => {
console.log('FILE MOVED!');
//this.printViewShot(imagePath)
})
.catch((err) => {
console.log(err.message);
_download(){
const {timeLine,} = this.props
var localPath = RNFS.LibraryDirectoryPath + '/' + this.state.pageInfo.id + '.mp3'
if(Platform.OS === 'android'){
localPath = RNFS.ExternalDirectoryPath + '/' + this.state.pageInfo.id + '.mp3'
}
let downloadManager = new DownloadManager()
if(downloadManager.isDownloading ){
Alert.alert('提示','已有任务在队列中',[{text:'确定',onPress:()=>{console.log('sure')} }])
return
}
console.log(localPath)
let starage = new MyStorage()
starage.getAudioInfo(this.state.pageInfo.id,(res)=>{
if(res){
Alert.alert('提示','资源已经下载',[{text:'确定',onPress:()=>{console.log('sure')} }])
}else{
Alert.alert('提示','开始下载',[{text:'确定',onPress:()=>{console.log('sure')} }])
downloadManager.downloadFile(this.state.pageInfo.media.mp3[0],localPath,timeLine,this.state.pageInfo)
Text,
View,
Alert,
Picker,
Button,
} from "react-native";
import RNFS from "react-native-fs";
import js2xmlparser from "js2xmlparser";
import Stats from "./Stats";
import Record from "./Record";
import tracking, { trackingConfigs } from "./tracking";
import { toKnots, stateFromLocation } from "./utils";
const jsToXml = js2xmlparser.parse;
const filePath = RNFS.ExternalDirectoryPath + "/route.gpx";
const getDefaultState = () => ({
latitude: "--",
longitude: "--",
distance: "--",
accuracy: "--",
cog: "--",
speed: "--",
});
export default class App extends React.Component {
state = {
trackingRunning: false,
mode: "walk",
...getDefaultState(),
};
getPath: function(filename? : string) {
let targetPath = Platform.OS === 'android' ? RNFS.ExternalDirectoryPath : RNFS.DocumentDirectoryPath;
if (filename) {
if (targetPath[targetPath.length-1] !== '/') {
targetPath += '/';
}
targetPath += filename;
}
return targetPath;
},