mirror of
https://github.com/zhigang1992/react-native-code-push.git
synced 2026-06-12 08:59:05 +08:00
add tests
This commit is contained in:
@@ -1,101 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
var React = require("react-native");
|
||||
var { Platform, DeviceEventEmitter } = require("react-native");
|
||||
var CodePushSdk = require("react-native-code-push");
|
||||
var NativeCodePush = require("react-native").NativeModules.CodePush;
|
||||
var RCTTestModule = require('NativeModules').TestModule || {};
|
||||
|
||||
var {
|
||||
Text,
|
||||
View,
|
||||
} = React;
|
||||
|
||||
var DownloadProgressTest = React.createClass({
|
||||
propTypes: {
|
||||
shouldThrow: React.PropTypes.bool,
|
||||
waitOneFrame: React.PropTypes.bool,
|
||||
},
|
||||
|
||||
getInitialState() {
|
||||
return {
|
||||
done: false,
|
||||
};
|
||||
},
|
||||
|
||||
componentDidMount() {
|
||||
if (this.props.waitOneFrame) {
|
||||
requestAnimationFrame(this.runTest);
|
||||
} else {
|
||||
this.runTest();
|
||||
}
|
||||
},
|
||||
|
||||
checkReceivedAndExpectedBytesEqual() {
|
||||
if (this.state.progress.receivedBytes !== this.state.progress.totalBytes) {
|
||||
throw new Error("Bytes do not tally: Received bytes=" + this.state.progress.receivedBytes + " Total bytes=" + this.state.progress.totalBytes);
|
||||
}
|
||||
},
|
||||
|
||||
runTest() {
|
||||
var downloadProgressSubscription = DeviceEventEmitter.addListener(
|
||||
"CodePushDownloadProgress",
|
||||
(progress) => {
|
||||
this.setState({
|
||||
progress:progress,
|
||||
done: false,
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
var updates = require("./TestPackages");
|
||||
NativeCodePush.downloadUpdate(updates.smallPackage)
|
||||
.then((smallPackage) => {
|
||||
if (smallPackage) {
|
||||
this.checkReceivedAndExpectedBytesEqual();
|
||||
return NativeCodePush.downloadUpdate(updates.mediumPackage);
|
||||
} else {
|
||||
throw new Error("Small package download failed.");
|
||||
}
|
||||
})
|
||||
.then((mediumPackage) => {
|
||||
if (mediumPackage) {
|
||||
this.checkReceivedAndExpectedBytesEqual();
|
||||
return NativeCodePush.downloadUpdate(updates.largePackage);
|
||||
} else {
|
||||
throw new Error("Medium package download failed.");
|
||||
}
|
||||
})
|
||||
.done((largePackage) => {
|
||||
if (largePackage) {
|
||||
this.checkReceivedAndExpectedBytesEqual();
|
||||
this.setState({done: true}, RCTTestModule.markTestCompleted);
|
||||
} else {
|
||||
throw new Error("Large package download failed.");
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
render() {
|
||||
var progressView;
|
||||
if (this.state.progress) {
|
||||
progressView = (
|
||||
<Text>{this.state.progress.receivedBytes} of {this.state.progress.totalBytes} bytes received</Text>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<View style={{backgroundColor: "white", padding: 40}}>
|
||||
<Text>
|
||||
{this.constructor.displayName + ": "}
|
||||
{this.state.done ? "Done" : "Testing..."}
|
||||
</Text>
|
||||
{progressView}
|
||||
</View>
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
DownloadProgressTest.displayName = "DownloadProgressTest";
|
||||
|
||||
module.exports = DownloadProgressTest;
|
||||
@@ -12,7 +12,7 @@ var {
|
||||
} = React;
|
||||
|
||||
var TESTS = [
|
||||
require("./DownloadProgressTest")
|
||||
require("./testcases/DownloadProgressTest")
|
||||
];
|
||||
|
||||
TESTS.forEach(
|
||||
@@ -20,12 +20,12 @@ TESTS.forEach(
|
||||
);
|
||||
|
||||
var DownloadProgressTestApp = React.createClass({
|
||||
getInitialState: function() {
|
||||
getInitialState() {
|
||||
return {
|
||||
test: null,
|
||||
};
|
||||
},
|
||||
render: function() {
|
||||
render() {
|
||||
if (this.state.test) {
|
||||
return (
|
||||
<ScrollView>
|
||||
@@ -36,9 +36,7 @@ var DownloadProgressTestApp = React.createClass({
|
||||
return (
|
||||
<View style={styles.container}>
|
||||
<Text style={styles.row}>
|
||||
Click on a test to run it in this shell for easier debugging and
|
||||
development. Run all tests in the testing environment with cmd+U in
|
||||
Xcode.
|
||||
DownloadProgress Tests
|
||||
</Text>
|
||||
<View style={styles.separator} />
|
||||
<ScrollView>
|
||||
@@ -49,6 +47,9 @@ var DownloadProgressTestApp = React.createClass({
|
||||
<Text style={styles.testName}>
|
||||
{test.displayName}
|
||||
</Text>
|
||||
<Text style={styles.testDescription}>
|
||||
{test.description}
|
||||
</Text>
|
||||
</TouchableOpacity>,
|
||||
<View style={styles.separator} />
|
||||
])}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
var { Platform } = require("react-native");
|
||||
|
||||
var packages = {
|
||||
smallPackage: {
|
||||
var packages = [
|
||||
{
|
||||
downloadUrl: "smallFile",
|
||||
description: "Angry flappy birds",
|
||||
appVersion: "1.5.0",
|
||||
@@ -12,7 +12,7 @@ var packages = {
|
||||
packageHash: "hash240",
|
||||
packageSize: 1024
|
||||
},
|
||||
mediumPackage: {
|
||||
{
|
||||
downloadUrl: "mediumFile",
|
||||
description: "Angry flappy birds",
|
||||
appVersion: "1.5.0",
|
||||
@@ -23,7 +23,7 @@ var packages = {
|
||||
packageHash: "hash240",
|
||||
packageSize: 1024
|
||||
},
|
||||
largePackage: {
|
||||
{
|
||||
downloadUrl: "largeFile",
|
||||
description: "Angry flappy birds",
|
||||
appVersion: "1.5.0",
|
||||
@@ -34,15 +34,14 @@ var packages = {
|
||||
packageHash: "hash240",
|
||||
packageSize: 1024
|
||||
}
|
||||
};
|
||||
];
|
||||
|
||||
for (var aPackage in packages) {
|
||||
packages.forEach((aPackage) => {
|
||||
if (Platform.OS === "android") {
|
||||
// Genymotion forwards 10.0.3.2 to host machine's localhost
|
||||
packages[aPackage].downloadUrl = "http://10.0.3.2:8081/CodePushDemoAppTests/DownloadProgressTests/" + packages[aPackage].downloadUrl;
|
||||
aPackage.downloadUrl = "http://10.0.3.2:8081/CodePushDemoAppTests/DownloadProgressTests/resources/" + aPackage.downloadUrl;
|
||||
} else if (Platform.OS === "ios") {
|
||||
packages[aPackage].downloadUrl = "http://localhost:8081/CodePushDemoAppTests/DownloadProgressTests/" + packages[aPackage].downloadUrl;
|
||||
aPackage.downloadUrl = "http://localhost:8081/CodePushDemoAppTests/DownloadProgressTests/resources/" + aPackage.downloadUrl;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = packages;
|
||||
@@ -0,0 +1,52 @@
|
||||
"use strict";
|
||||
|
||||
var React = require("react-native");
|
||||
var CodePush = require("react-native-code-push");
|
||||
var NativeCodePush = React.NativeModules.CodePush;
|
||||
var createTestCaseComponent = require("../../utils/createTestCaseComponent");
|
||||
var PackageMixins = require("react-native-code-push/package-mixins.js")(NativeCodePush);
|
||||
var assert = require("assert");
|
||||
|
||||
var testPackages = require("../resources/TestPackages");
|
||||
var localPackage = {};
|
||||
var saveProgress;
|
||||
|
||||
function checkReceivedAndExpectedBytesEqual() {
|
||||
assert(saveProgress, "Download progress was not reported.");
|
||||
assert.equal(
|
||||
saveProgress.receivedBytes,
|
||||
saveProgress.totalBytes,
|
||||
`Bytes do not tally: Received bytes=${saveProgress.receivedBytes} Total bytes=${saveProgress.totalBytes}`
|
||||
);
|
||||
console.log("Downloaded one package.");
|
||||
saveProgress = null;
|
||||
}
|
||||
|
||||
var DownloadProgressTest = createTestCaseComponent(
|
||||
"DownloadProgressTest",
|
||||
"should successfully download all the bytes contained in the test packages",
|
||||
() => {
|
||||
testPackages.forEach((aPackage, index) => {
|
||||
testPackages[index] = Object.assign(aPackage, PackageMixins.remote);
|
||||
});
|
||||
return Promise.resolve();
|
||||
},
|
||||
() => {
|
||||
var downloadProgressCallback = (downloadProgress) => {
|
||||
console.log(`Expecting ${downloadProgress.totalBytes} bytes, received ${downloadProgress.receivedBytes} bytes.`);
|
||||
saveProgress = downloadProgress;
|
||||
};
|
||||
|
||||
// Chains promises together.
|
||||
return testPackages.reduce((aPackageDownloaded, nextPackage, index) => {
|
||||
return aPackageDownloaded
|
||||
.then(() => {
|
||||
// Skip the first time.
|
||||
index && checkReceivedAndExpectedBytesEqual();
|
||||
return nextPackage.download(downloadProgressCallback);
|
||||
})
|
||||
}, Promise.resolve());
|
||||
}
|
||||
);
|
||||
|
||||
module.exports = DownloadProgressTest;
|
||||
Reference in New Issue
Block a user