Skip to content

Commit

Permalink
navigator.geolocation.getCurrentPosition is an sync function, added a…
Browse files Browse the repository at this point in the history
…wait
  • Loading branch information
silyevsk committed Aug 6, 2017
1 parent 2119105 commit 32e2f09
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
2 changes: 2 additions & 0 deletions detox/test/e2e/o-location.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ describe('location', () => {
it('Location should be unavabilable', async () => {
await device.relaunchApp({permissions: {location: 'never'}});
await element(by.label('Location')).tap();
await element(by.id('getLocationButton')).tap();
await expect(element(by.id('error'))).toBeVisible();
});

it('Should receive location (20,20)', async () => {
await device.relaunchApp({permissions: {location: 'always'}});
await device.setLocation(20, 20);
await element(by.label('Location')).tap();
await element(by.id('getLocationButton')).tap();
await expect(element(by.text('Latitude: 20'))).toBeVisible();
await expect(element(by.text('Longitude: 20'))).toBeVisible();
});
Expand Down
23 changes: 20 additions & 3 deletions detox/test/src/Screens/LocationScreen.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import React, { Component } from 'react';
import {
Text,
View
View,
Button
} from 'react-native';

export default class LocationScreen extends Component {

constructor(props) {
super(props);
this.state = {
locationRequested: false,
coordinates: null
}

this.getLocation();
}

getLocation() {
async getLocation() {
function success(pos) {
this.setState({
coordinates: pos.coords
Expand All @@ -28,10 +30,25 @@ export default class LocationScreen extends Component {
});
};

navigator.geolocation.getCurrentPosition(success.bind(this), error.bind(this));
await navigator.geolocation.getCurrentPosition(success.bind(this), error.bind(this));
}

render() {
if(!this.state.locationRequested) {
return (
<View style={{ flex: 1, paddingTop: 20, justifyContent: 'center', alignItems: 'center' }}>
<Button
testID="getLocationButton"
title="get location"
onPress={async () => {
await this.getLocation();
this.setState({locationRequested: true});
}}
/>
</View>
);
}

if (this.state.coordinates) {
return (
<View style={{ flex: 1, paddingTop: 20, justifyContent: 'center', alignItems: 'center' }}>
Expand Down

0 comments on commit 32e2f09

Please sign in to comment.