Skip to content
This repository was archived by the owner on Sep 4, 2021. It is now read-only.

Commit a383e62

Browse files
committed
unit testing
1 parent 205beeb commit a383e62

File tree

12 files changed

+1046
-27
lines changed

12 files changed

+1046
-27
lines changed

js/geocoder.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default {
1616
}
1717

1818
return RNGeocoder.geocodePosition(position).catch(err => {
19-
if (!this.apiKey) { throw err; }
19+
if (!this.apiKey || err.code !== 'NOT_AVAILABLE') { throw err; }
2020
return GoogleApi.geocodePosition(this.apiKey, position);
2121
});
2222
},
@@ -27,7 +27,7 @@ export default {
2727
}
2828

2929
return RNGeocoder.geocodeAddress(address).catch(err => {
30-
if (!this.apiKey) { throw err; }
30+
if (!this.apiKey || err.code !== 'NOT_AVAILABLE') { throw err; }
3131
return GoogleApi.geocodeAddress(this.apiKey, address);
3232
});
3333
},

js/googleApi.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,18 @@ export default {
6363
return Promise.reject(new Error("invalid apiKey / position"));
6464
}
6565

66-
return this.googleRequest(`${googleUrl}?key=${apiKey}&latlng=${position.lat},${position.lng}`);
66+
return this.geocodeRequest(`${googleUrl}?key=${apiKey}&latlng=${position.lat},${position.lng}`);
6767
},
6868

6969
geocodeAddress(apiKey, address) {
7070
if (!apiKey || !address) {
7171
return Promise.reject(new Error("invalid apiKey / address"));
7272
}
7373

74-
return this.googleRequest(`${googleUrl}?key=${apiKey}&address=${encodeURI(address)}`);
74+
return this.geocodeRequest(`${googleUrl}?key=${apiKey}&address=${encodeURI(address)}`);
7575
},
7676

77-
async googleRequest(url) {
77+
async geocodeRequest(url) {
7878
const res = await fetch(url);
7979
const json = await res.json();
8080

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "react native geocoding and reverse geocoding",
55
"main": "index.js",
66
"scripts": {
7-
"test": "e2e/run.sh"
7+
"test": "mocha --recursive --require test/setup.js --reporter dot test/unit "
88
},
99
"keywords": [
1010
"react-component",
@@ -35,6 +35,9 @@
3535
"chai": "^3.5.0",
3636
"colors": "^1.1.2",
3737
"mocha": "^2.4.5",
38+
"proxyquire": "^1.7.9",
39+
"sinon": "^1.17.4",
40+
"sinon-chai": "^2.8.0",
3841
"wd": "^0.4.0"
3942
}
4043
}
File renamed without changes.
File renamed without changes.

test/index.spec.js renamed to test/e2e/index.spec.js

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,54 @@ const Paris = {
1616
lng: 2.3488,
1717
};
1818

19-
describe ('react-native-geocoder', function() {
20-
this.timeout(600000);
19+
const sleepBeforeStart = process.env['SAUCE_RUN'] ? 3 * 60 * 1000 : 10 * 1000;
20+
21+
const driverConfig = process.env['SAUCE_RUN'] ?
22+
{ host: 'ondemand.saucelabs.com', port: 80 } :
23+
{ host: 'localhost', port: 4723 };
24+
25+
let driverCaps = {
26+
platformName: 'Android',
27+
deviceName: 'Google Nexus 7C GoogleAPI Emulator',
28+
platformVersion: '4.4',
29+
newCommandTimeout: 600,
30+
commandTimeout: 600,
31+
idleTimeout: 1000,
32+
app: 'http://localhost:8080/e2e/GeocoderE2EApp/android/app/build/outputs/apk/app-debug.apk',
33+
appiumVersion: '1.5.2',
34+
}
35+
36+
driverCaps = process.env['SAUCE_RUN'] ? driverCaps :
37+
{
38+
platformName: 'Android',
39+
deviceName: 'Android emulator',
40+
newCommandTimeout: 600,
41+
app: 'http://localhost:8080/e2e/GeocoderE2EApp/android/app/build/outputs/apk/app-debug.apk',
42+
};
2143

22-
const driver = wd.promiseChainRemote({
23-
host: 'localhost',
24-
port: 4723
25-
});
44+
describe ('react-native-geocoder', function() {
45+
this.timeout(60000000);
2646

47+
const driver = wd.promiseChainRemote(driverConfig);
2748
require("./helpers/logging").configure(driver);
2849

29-
before(() => {
30-
return driver.init({
31-
platformName: 'Android',
32-
deviceName: 'Android Emulator',
33-
newCommandTimeout: 60000,
34-
app: path.resolve('e2e/GeocoderE2EApp/android/app/build/outputs/apk/app-debug.apk')
35-
})
36-
.setImplicitWaitTimeout(3000);
50+
before(async function() {
51+
console.log('Init driver');
52+
await driver.init(driverCaps);
53+
54+
console.log('set implicit timeout');
55+
await driver.setImplicitWaitTimeout(10000);
56+
57+
console.log('sleeping ', sleepBeforeStart);
58+
await driver.sleep(sleepBeforeStart);
3759
});
3860

3961
after(async () => {
4062
await driver.quit();
4163
});
4264

4365
it ('displays default view', failWithShot(driver, shotDir, async function() {
44-
await driver.waitForElementByXPath('//android.widget.EditText[1]', 120000); // wait for view to be initialized
66+
await driver.waitForElementByXPath('//android.widget.EditText[1]', 5 * 60 * 1000); // wait for view to be initialized
4567
await driver.waitForElementByXPath('//android.widget.EditText[2]');
4668
await driver.waitForElementByXPath('//android.widget.TextView[starts-with(@text, "Geocode")]');
4769
await driver.waitForElementByXPath('//android.widget.TextView[starts-with(@text, "Reverse")]');

test/globals.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

test/setup.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require('babel-core/register')({
2+
presets: ["react-native"]
3+
});
4+
require("babel-polyfill");
5+
6+
require('colors');
7+
8+
var chai = require("chai");
9+
var sinon = require("sinon");
10+
var sinonChai = require("sinon-chai");
11+
chai.use(sinonChai);
12+
13+
global.expect = chai.expect;

0 commit comments

Comments
 (0)