@eohjsc/react-native-smart-city 0.3.5 → 0.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@eohjsc/react-native-smart-city",
3
3
  "title": "React Native Smart Home",
4
- "version": "0.3.05",
4
+ "version": "0.3.06",
5
5
  "description": "TODO",
6
6
  "main": "index.js",
7
7
  "files": [
@@ -32,7 +32,7 @@ const NumberUpDownActionTemplate = ({ actionGroup, doAction, sensor }) => {
32
32
  if (
33
33
  !!config &&
34
34
  sensor?.is_managed_by_backend &&
35
- sensor.device_type !== 'GOOGLE_HOME'
35
+ sensor.device_type !== DEVICE_TYPE.GOOGLE_HOME
36
36
  ) {
37
37
  watchMultiConfigs([config]);
38
38
  }
@@ -53,12 +53,16 @@ const NumberUpDownActionTemplate = ({ actionGroup, doAction, sensor }) => {
53
53
 
54
54
  const doActionAndWatchConfig = useCallback(
55
55
  async (actionData, actionValue, actionName) => {
56
- const data = { temperature: actionValue };
57
- if (allow_config_store_value && config) {
58
- data.config_id = config;
59
- data.value = actionValue;
56
+ let data = actionValue;
57
+ if (sensor.device_type !== DEVICE_TYPE.GOOGLE_HOME) {
58
+ data = { temperature: actionValue };
59
+ if (allow_config_store_value && config) {
60
+ data.config_id = config;
61
+ data.value = actionValue;
62
+ }
63
+ data = JSON.stringify(data);
60
64
  }
61
- await doAction(actionData, JSON.stringify(data), actionName);
65
+ await doAction(actionData, data, actionName);
62
66
  if (!sensor?.is_managed_by_backend) {
63
67
  return;
64
68
  }
@@ -67,7 +71,7 @@ const NumberUpDownActionTemplate = ({ actionGroup, doAction, sensor }) => {
67
71
  }
68
72
 
69
73
  config &&
70
- sensor.device_type !== 'GOOGLE_HOME' &&
74
+ sensor.device_type !== DEVICE_TYPE.GOOGLE_HOME &&
71
75
  watchMultiConfigs([config]);
72
76
  },
73
77
  [
@@ -9,7 +9,7 @@ import { useDropdownAction } from './hooks/useDropdownAction';
9
9
  import { useConfigGlobalState } from '../../iot/states';
10
10
  import styles from './OptionsDropdownActionTemplateStyle';
11
11
  import { watchMultiConfigs } from '../../iot/Monitor';
12
- import { TESTID } from '../../configs/Constants';
12
+ import { DEVICE_TYPE, TESTID } from '../../configs/Constants';
13
13
  import IconComponent from '../../commons/IconComponent';
14
14
  import { useTranslations } from '../../hooks/Common/useTranslations';
15
15
  import SvgDoorState from '../../../assets/images/Device/door-state.svg';
@@ -61,14 +61,16 @@ const OptionsDropdownActionTemplate = ({ actionGroup, doAction, sensor }) => {
61
61
  const value = getOptionValue(newOption);
62
62
  let actionName = `${sensor?.name} ${title?.toLowerCase()} ${value}`;
63
63
  actionName = actionName.replace(/\s+/g, ' ').trim();
64
- doAction(
65
- action_data,
66
- JSON.stringify({ level: value, key_code: newOption?.value_int }),
67
- actionName
68
- );
64
+
65
+ let data = JSON.stringify({ level: value, key_code: newOption?.value_int });
66
+ if (sensor.device_type === DEVICE_TYPE.GOOGLE_HOME) {
67
+ data = value;
68
+ }
69
+
70
+ doAction(action_data, data, actionName);
69
71
  if (sensor?.is_managed_by_backend) {
70
72
  configuration.config &&
71
- sensor.device_type !== 'GOOGLE_HOME' &&
73
+ sensor.device_type !== DEVICE_TYPE.GOOGLE_HOME &&
72
74
  watchMultiConfigs([configuration.config]);
73
75
  }
74
76
  hideAlertAction();
@@ -328,4 +328,44 @@ describe('Test NumberUpDownActionTemplate', () => {
328
328
  const text = instance.findAllByType(ActivityIndicator);
329
329
  expect(text).toHaveLength(1);
330
330
  });
331
+ test('action up sensor device type is GOOGLE_HOME', async () => {
332
+ const mockDoAction = jest.fn();
333
+ await act(async () => {
334
+ wrapper = await create(
335
+ <NumberUpDownActionTemplate
336
+ actionGroup={actionGroup}
337
+ doAction={mockDoAction}
338
+ sensor={{
339
+ is_managed_by_backend: true,
340
+ device_type: DEVICE_TYPE.GOOGLE_HOME,
341
+ name: 'Device',
342
+ }}
343
+ />
344
+ );
345
+ });
346
+ const instance = wrapper.root;
347
+ const touchs = instance.find(
348
+ (el) =>
349
+ el.props.testID === TESTID.NUMBER_ACTION_UP &&
350
+ el.type === TouchableOpacity
351
+ );
352
+
353
+ await act(async () => {
354
+ await touchs.props.onPress();
355
+ });
356
+ expect(mockDoAction).toBeCalledWith(
357
+ {
358
+ color: '#00979D',
359
+ command_prefer_over_bluetooth: true,
360
+ command_prefer_over_googlehome: false,
361
+ command_prefer_over_internet: false,
362
+ googlehome_actions: [],
363
+ icon: 'caret-up',
364
+ id: 20,
365
+ key: '5ed1d4dc-a905-47cd-b0c9-f979644bd21a',
366
+ },
367
+ 26,
368
+ 'Device temp up'
369
+ );
370
+ });
331
371
  });
@@ -5,7 +5,7 @@ import { act, create } from 'react-test-renderer';
5
5
  import Text from '../../../commons/Text';
6
6
  import { watchMultiConfigs } from '../../../iot/Monitor';
7
7
  import { AlertAction, RadioCircle } from '../../../commons';
8
- import { TESTID } from '../../../configs/Constants';
8
+ import { DEVICE_TYPE, TESTID } from '../../../configs/Constants';
9
9
  import { getTranslate } from '../../../utils/I18n';
10
10
  import { SCProvider } from '../../../context';
11
11
  import { mockSCStore } from '../../../context/mockStore';
@@ -246,4 +246,60 @@ describe('Test OptionsDropdownActionTemplate', () => {
246
246
  getTranslate('en', 'not_available')
247
247
  );
248
248
  });
249
+ test('doAction sensor device type is GOOGLE_HOME', async () => {
250
+ actionGroup.configuration.options[0].value_text = 'level-1';
251
+ sensor.device_type = DEVICE_TYPE.GOOGLE_HOME;
252
+ const mockDoAction = jest.fn();
253
+ await act(async () => {
254
+ wrapper = await create(wrapComponent(actionGroup, mockDoAction, sensor));
255
+ });
256
+ const instance = wrapper.root;
257
+
258
+ const touchs = instance.findAllByType(TouchableOpacity);
259
+ expect(touchs).toHaveLength(5);
260
+ const showListOption = touchs[0];
261
+
262
+ const texts = instance.findAllByType(Text);
263
+ expect(texts[1].props.children).toEqual('Level2');
264
+
265
+ const radioCircles = instance.findAllByType(RadioCircle);
266
+ expect(radioCircles[0].props.active).toBeFalsy();
267
+ expect(radioCircles[1].props.active).toBeTruthy();
268
+
269
+ const listChoosingOption = instance.findAll(
270
+ (el) =>
271
+ el.props.testID === TESTID.OPTIONS_DROPDOWN_ACTION_CHOOSING_ITEM &&
272
+ el.type === TouchableOpacity
273
+ );
274
+ expect(listChoosingOption).toHaveLength(2);
275
+
276
+ const alertAction = instance.findByType(AlertAction);
277
+ expect(alertAction.props.visible).toBeFalsy();
278
+
279
+ // show option
280
+ await act(async () => {
281
+ await showListOption.props.onPress();
282
+ });
283
+ expect(alertAction.props.visible).toBeTruthy();
284
+
285
+ // Choosed Level1 option
286
+ await act(async () => {
287
+ await listChoosingOption[0].props.onPress();
288
+ });
289
+
290
+ expect(radioCircles[0].props.active).toBeTruthy();
291
+ expect(radioCircles[1].props.active).toBeFalsy();
292
+
293
+ // doAction then update selectedOption
294
+ await act(async () => {
295
+ await alertAction.props.rightButtonClick();
296
+ });
297
+ expect(alertAction.props.visible).toBeFalsy();
298
+
299
+ expect(mockDoAction).toHaveBeenCalledWith(
300
+ action_data,
301
+ 'level-1',
302
+ 'Sensor name fan speed level-1'
303
+ ); // doAction with text instead of int
304
+ });
249
305
  });
@@ -81,8 +81,8 @@ const ConnectingProcess = ({ route }) => {
81
81
  }
82
82
  );
83
83
  if (success) {
84
- setSensor({ name: gateway?.model });
85
- setNewName(gateway?.model);
84
+ setSensor(data);
85
+ setNewName(data?.name || gateway?.model);
86
86
  } else {
87
87
  ToastBottomHelper.error(JSON.stringify(data));
88
88
  goBack();
@@ -75,6 +75,7 @@ export const DEVICE_TYPE = {
75
75
  LG_THINQ: 'LG_THINQ',
76
76
  HANET: 'HANET',
77
77
  ZIGBEE: 'ZIGBEE',
78
+ GOOGLE_HOME: 'GOOGLE_HOME',
78
79
  };
79
80
 
80
81
  const marginItem = 12;
@@ -28,8 +28,6 @@ import WifiManager from 'react-native-wifi-reborn';
28
28
  import { useNavigation } from '@react-navigation/native';
29
29
  import { TESTID } from '../../../configs/Constants';
30
30
 
31
- const isIos = Platform.OS === 'ios';
32
-
33
31
  const GatewayWifiList = memo(({ route }) => {
34
32
  const {
35
33
  list_wifi,
@@ -72,41 +70,46 @@ const GatewayWifiList = memo(({ route }) => {
72
70
  if (Object.prototype.hasOwnProperty.call(data, 'gateway')) {
73
71
  const checkWifiInterval = setInterval(() => {
74
72
  // eslint-disable-next-line promise/prefer-await-to-then
75
- WifiManager.getCurrentWifiSSID().then((ssid) => {
76
- if (ssid !== wifi_ssid) {
77
- const IsRobot = devicePrefixName === 'ROBOT';
78
- if (IsRobot) {
79
- setAction(Action.IS_CONNECT_WIFI_GATEWAY, false);
80
- navigate(Routes.AddDeviceStack, {
81
- screen: Routes.AddCommonSelectSubUnit,
82
- params: {
83
- scan_sensor_data: scan_sensor_data,
84
- addType: 'AddDeviceNewFlow',
85
- gateway: data?.gateway,
86
- unit_id: unit_id,
87
- devicePrefixName: devicePrefixName,
88
- wifi_ssid: wifi_ssid,
89
- wifi_pass: wifi_pass,
90
- },
91
- });
92
- clearInterval(checkWifiInterval);
93
- } else {
94
- setAction(Action.IS_CONNECT_WIFI_GATEWAY, false);
95
- navigate(Routes.AddDeviceStack, {
96
- screen: Routes.ConnectingProcess,
97
- params: {
98
- unit_id: unit_id,
99
- unit_name: unit_name,
100
- scan_sensor_data: scan_sensor_data,
101
- gateway: data?.gateway,
102
- devicePrefixName: devicePrefixName,
103
- chip_id: chip_id,
104
- },
105
- });
106
- clearInterval(checkWifiInterval);
73
+ WifiManager.getCurrentWifiSSID().then(
74
+ (ssid) => {
75
+ if (ssid !== wifi_ssid) {
76
+ const IsRobot = devicePrefixName === 'ROBOT';
77
+ if (IsRobot) {
78
+ setAction(Action.IS_CONNECT_WIFI_GATEWAY, false);
79
+ navigate(Routes.AddDeviceStack, {
80
+ screen: Routes.AddCommonSelectSubUnit,
81
+ params: {
82
+ scan_sensor_data: scan_sensor_data,
83
+ addType: 'AddDeviceNewFlow',
84
+ gateway: data?.gateway,
85
+ unit_id: unit_id,
86
+ devicePrefixName: devicePrefixName,
87
+ wifi_ssid: wifi_ssid,
88
+ wifi_pass: wifi_pass,
89
+ },
90
+ });
91
+ clearInterval(checkWifiInterval);
92
+ } else {
93
+ setAction(Action.IS_CONNECT_WIFI_GATEWAY, false);
94
+ navigate(Routes.AddDeviceStack, {
95
+ screen: Routes.ConnectingProcess,
96
+ params: {
97
+ unit_id: unit_id,
98
+ unit_name: unit_name,
99
+ scan_sensor_data: scan_sensor_data,
100
+ gateway: data?.gateway,
101
+ devicePrefixName: devicePrefixName,
102
+ chip_id: chip_id,
103
+ },
104
+ });
105
+ clearInterval(checkWifiInterval);
106
+ }
107
107
  }
108
+ },
109
+ () => {
110
+ ToastBottomHelper.error('Cannot get current SSID!');
108
111
  }
109
- });
112
+ );
110
113
  }, 3000);
111
114
  }
112
115
  },
@@ -151,7 +154,7 @@ const GatewayWifiList = memo(({ route }) => {
151
154
 
152
155
  const connectWifi = useCallback(async () => {
153
156
  if (!isConnectWifiGateway) {
154
- if (isIos) {
157
+ if (Platform.OS === 'ios') {
155
158
  sendConnect(0);
156
159
  setAction(Action.IS_CONNECT_WIFI_GATEWAY, true);
157
160
  setIsSendWifi(true);
@@ -1,25 +1,32 @@
1
1
  import React from 'react';
2
2
  import { act, create } from 'react-test-renderer';
3
3
  import GatewayWifiList from '../GatewayWifiList';
4
- import { Text, ScrollView, TouchableOpacity } from 'react-native';
4
+ import { Text, ScrollView, TouchableOpacity, Platform } from 'react-native';
5
5
  import { getTranslate } from '../../../../utils/I18n';
6
6
  import { SCProvider } from '../../../../context';
7
7
  import { mockSCStore } from '../../../../context/mockStore';
8
8
  import TextInputPassword from '../../../../commons/Form/TextInputPassword';
9
9
  import { TESTID } from '../../../../configs/Constants';
10
+ import WifiManager from 'react-native-wifi-reborn';
11
+ import { useNavigation } from '@react-navigation/native';
12
+ import Routes from '../../../../utils/Route';
10
13
 
11
- const wrapComponent = (route) => (
12
- <SCProvider initState={mockSCStore({})}>
14
+ const wrapComponent = (route, state = {}) => (
15
+ <SCProvider initState={{ ...mockSCStore({}), ...state }}>
13
16
  <GatewayWifiList route={route} />
14
17
  </SCProvider>
15
18
  );
16
19
 
17
20
  describe('Test GatewayWifiList', () => {
18
21
  let tree;
22
+ const socket = {
23
+ on: jest.fn(),
24
+ send: jest.fn(),
25
+ };
19
26
  let route = {
20
27
  params: {
21
28
  list_wifi: [{ ssid: 'eoh@io' }, { ssid: 'eoh@ras.io' }],
22
- socket: { send: jest.fn() },
29
+ socket: socket,
23
30
  },
24
31
  };
25
32
 
@@ -33,6 +40,7 @@ describe('Test GatewayWifiList', () => {
33
40
  expect(texts[1].props.children).toEqual(getTranslate('en', 'set_network'));
34
41
  expect(texts[3].props.children).toEqual('eoh@io');
35
42
  });
43
+
36
44
  test('onPress wifi', async () => {
37
45
  await act(async () => {
38
46
  tree = await create(wrapComponent(route));
@@ -46,6 +54,7 @@ describe('Test GatewayWifiList', () => {
46
54
  await touchableOpacity[1].props.onPress();
47
55
  });
48
56
  });
57
+
49
58
  test('onChange TextInputPassword', async () => {
50
59
  await act(async () => {
51
60
  tree = await create(wrapComponent(route));
@@ -60,15 +69,60 @@ describe('Test GatewayWifiList', () => {
60
69
  const ButtonPopup = instance.find(
61
70
  (el) => el.props.testID === TESTID.GATEWAY_WIFI_LIST.BUTTON_POPUP
62
71
  );
63
- await act(() => {
64
- ButtonPopup.props.onPressMain();
72
+ await act(async () => {
73
+ await ButtonPopup.props.onPressMain();
65
74
  });
66
- await act(() => {
67
- ButtonPopup.props.onPressSecondary();
75
+ await act(async () => {
76
+ await ButtonPopup.props.onPressSecondary();
68
77
  });
69
- await act(() => {
70
- ButtonPopup.props.onClose();
78
+ await act(async () => {
79
+ await ButtonPopup.props.onClose();
71
80
  });
72
81
  expect(ButtonPopup).toBeDefined();
73
82
  });
83
+
84
+ test('on device is connected to wifi and mobile re-connect to internet', async () => {
85
+ jest.useFakeTimers();
86
+ Platform.OS = 'android';
87
+
88
+ await act(async () => {
89
+ tree = await create(wrapComponent(route, {}));
90
+ });
91
+
92
+ const instance = tree.root;
93
+ const ButtonPopup = instance.find(
94
+ (el) => el.props.testID === TESTID.GATEWAY_WIFI_LIST.BUTTON_POPUP
95
+ );
96
+ await act(async () => {
97
+ await ButtonPopup.props.onPressMain();
98
+ });
99
+ expect(socket.on).toBeCalled();
100
+ expect(socket.send).toBeCalled();
101
+
102
+ const mockWifiThen = jest.fn();
103
+ WifiManager.getCurrentWifiSSID.mockImplementation(() => ({
104
+ then: mockWifiThen,
105
+ }));
106
+
107
+ // receive message
108
+ await act(async () => {
109
+ await socket.on.mock.calls[0][1](
110
+ JSON.stringify({
111
+ gateway: true,
112
+ })
113
+ );
114
+ });
115
+ jest.runOnlyPendingTimers();
116
+
117
+ expect(WifiManager.getCurrentWifiSSID).toBeCalled();
118
+ expect(mockWifiThen).toBeCalled();
119
+
120
+ // get ssid success
121
+ act(() => {
122
+ mockWifiThen.mock.calls[0][0]('new-ssid');
123
+ });
124
+ const { navigate } = useNavigation();
125
+ expect(navigate).toBeCalled();
126
+ expect(navigate.mock.calls[0][0]).toEqual(Routes.AddDeviceStack);
127
+ });
74
128
  });