• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

akvo / nmis-mobile / 5811397198

09 Aug 2023 04:03PM UTC coverage: 81.299% (+0.2%) from 81.104%
5811397198

Pull #129

github

ifirmawan
[#112] Update use current location test in MapView
Pull Request #129: Feature/112 fix gps accuracy behavior

670 of 885 branches covered (75.71%)

Branch coverage included in aggregate %.

40 of 40 new or added lines in 3 files covered. (100.0%)

1421 of 1687 relevant lines covered (84.23%)

18.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.86
/app/src/form/fields/TypeGeo.js
1
import React, { useState, useEffect, useMemo, useCallback } from 'react';
2
import { View } from 'react-native';
3
import { Text, Button } from '@rneui/themed';
4
import { useNavigation, useRoute } from '@react-navigation/native';
5

6
import { UIState, FormState, BuildParamsState } from '../../store';
7
import { FieldLabel } from '../support';
8
import { styles } from '../styles';
9
import { loc, i18n } from '../../lib';
10

11
const TypeGeo = ({ onChange, values, keyform, id, name, tooltip, required, requiredSign }) => {
4✔
12
  const [errorMsg, setErrorMsg] = useState(null);
36✔
13
  const [gpsAccuracy, setGpsAccuracy] = useState(null);
36✔
14
  const [currLocation, setCurrLocation] = useState({ lat: null, lng: null });
36✔
15
  const [loading, setLoading] = useState({ current: false, map: false });
36✔
16
  const currentValues = FormState.useState((s) => s.currentValues);
36✔
17
  const [latitude, longitude] = currentValues?.[id] || [];
36✔
18

19
  const gpsThreshold = BuildParamsState.useState((s) => s.gpsThreshold);
36✔
20
  const isOnline = UIState.useState((s) => s.online);
36✔
21
  const activeLang = FormState.useState((s) => s.lang);
36✔
22

23
  const trans = i18n.text(activeLang);
36✔
24

25
  const navigation = useNavigation();
36✔
26
  const route = useRoute();
36✔
27
  const requiredValue = required ? requiredSign : null;
36✔
28

29
  const handleOpenMap = () => {
36✔
30
    if (latitude && longitude) {
2!
31
      const params = { latitude, longitude, id, current_location: currLocation };
×
32
      navigation.navigate('MapView', { ...route?.params, ...params });
×
33
    } else {
34
      handleGetCurrLocation(true);
2✔
35
    }
36
  };
37

38
  const handleGetCurrLocation = useCallback(
36✔
39
    async (openMap = false) => {
×
40
      const loadingKey = openMap ? 'map' : 'current';
7✔
41
      setLoading({
7✔
42
        ...loading,
43
        [loadingKey]: true,
44
      });
45
      await loc.getCurrentLocation(
7✔
46
        ({ coords }) => {
47
          const { latitude: lat, longitude: lng, accuracy } = coords;
6✔
48
          /**
49
           * accuracy number in meters, doc: https://docs.expo.dev/versions/latest/sdk/location/#locationgeocodedlocation
50
           */
51
          setGpsAccuracy(Math.floor(accuracy));
6✔
52
          // console.info('GPS accuracy:', accuracy, 'GPS Threshold:', gpsThreshold);
53
          if ((accuracy <= gpsThreshold && !openMap) || openMap) {
6✔
54
            setCurrLocation({
4✔
55
              lat,
56
              lng,
57
            });
58
            onChange(id, [lat, lng]);
4✔
59
            setLoading({
4✔
60
              ...loading,
61
              [loadingKey]: false,
62
            });
63
          }
64
          if (openMap) {
6✔
65
            const params = { latitude: lat, longitude: lng, id, current_location: { lat, lng } };
2✔
66
            navigation.navigate('MapView', { ...route?.params, ...params });
2✔
67
          }
68
        },
69
        ({ message }) => {
70
          setLoading({
1✔
71
            ...loading,
72
            [loadingKey]: false,
73
          });
74
          setErrorMsg(message);
1✔
75
        },
76
      );
77
    },
78
    [gpsThreshold],
79
  );
80

81
  useEffect(() => {
36✔
82
    if (gpsAccuracy && gpsAccuracy >= gpsThreshold && loading.current) {
20✔
83
      handleGetCurrLocation(false);
2✔
84
    }
85
  }, [handleGetCurrLocation, gpsAccuracy, gpsThreshold, loading.current]);
86

87
  return (
36✔
88
    <View>
89
      <FieldLabel keyform={keyform} name={name} tooltip={tooltip} requiredSign={requiredValue} />
90
      <View style={styles.inputGeoContainer}>
91
        <View>
92
          <Text testID="text-lat">
93
            {trans.latitude}: {latitude}
94
          </Text>
95
          <Text testID="text-lng">
96
            {trans.longitude}: {longitude}
97
          </Text>
98
        </View>
99
        {errorMsg && (
37✔
100
          <Text testID="text-error" style={styles.errorText}>
101
            {errorMsg}
102
          </Text>
103
        )}
104
        <View style={styles.geoButtonGroup}>
105
          <Button onPress={() => handleGetCurrLocation(false)} testID="button-curr-location">
3✔
106
            {loading.current ? trans.fetchingLocation : trans.buttonCurrLocation}
36✔
107
          </Button>
108
          {isOnline && (
71✔
109
            <Button type="outline" onPress={handleOpenMap} testID="button-open-map">
110
              {loading.map ? trans.loadingText : trans.buttonOpenMap}
35✔
111
            </Button>
112
          )}
113
        </View>
114
      </View>
115
    </View>
116
  );
117
};
118

119
export default TypeGeo;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc