@eohjsc/react-native-smart-city 0.7.22 → 0.7.24

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.
Files changed (61) hide show
  1. package/package.json +1 -1
  2. package/src/commons/ActionGroup/TerminalBoxTemplate.js +3 -0
  3. package/src/commons/ActionTemplate/OnOffButtonAction.js +38 -4
  4. package/src/commons/ActionTemplate/OnOffSimpleAction.js +55 -15
  5. package/src/commons/ActionTemplate/OnOffSmartLockAction.js +46 -8
  6. package/src/commons/ActionTemplate/SwitchButtonAction.js +35 -4
  7. package/src/commons/ActionTemplate/ThreeButtonAction.js +13 -3
  8. package/src/commons/ActionTemplate/__test__/OnOffButtonAction.test.js +46 -7
  9. package/src/commons/ActionTemplate/__test__/OnOffSimpleAction.test.js +66 -6
  10. package/src/commons/ActionTemplate/__test__/OnOffSmartLockAction.test.js +53 -13
  11. package/src/commons/ActionTemplate/__test__/SwitchButtonAction.test.js +46 -7
  12. package/src/commons/ActionTemplate/__test__/index.test.js +6 -2
  13. package/src/commons/ActionTemplate/index.js +65 -10
  14. package/src/commons/MediaPlayerDetail/MediaPlayerFull.js +26 -32
  15. package/src/commons/OneTapTemplate/StatesGridActionTemplate.js +8 -6
  16. package/src/commons/SubUnit/OneTap/__test__/SubUnitAutomate.test.js +6 -0
  17. package/src/commons/SubUnit/OneTap/index.js +5 -0
  18. package/src/commons/UnitSummary/ConfigHistoryChart/index.js +16 -12
  19. package/src/commons/Widgets/IFrameWithConfig/IFrameWithConfig.js +3 -1
  20. package/src/commons/Widgets/IFrameWithConfig/__tests__/IFrameWithConfig.test.js +1 -1
  21. package/src/configs/API.js +6 -0
  22. package/src/configs/AccessibilityLabel.js +2 -0
  23. package/src/screens/ActivityLog/__test__/index.test.js +10 -0
  24. package/src/screens/ActivityLog/hooks/index.js +1 -1
  25. package/src/screens/Automate/AddNewAction/ChooseAction.js +15 -51
  26. package/src/screens/Automate/AddNewAction/SelectControlDevices.js +13 -3
  27. package/src/screens/Automate/AddNewAction/SetupConfigCondition.js +74 -54
  28. package/src/screens/Automate/AddNewAction/__test__/ChooseAction.test.js +114 -4
  29. package/src/screens/Automate/AddNewAction/__test__/SetupConfigCondition.test.js +37 -8
  30. package/src/screens/Automate/AddNewAutoSmart/AddTypeSmart.js +5 -0
  31. package/src/screens/Automate/AddNewAutoSmart/__test__/AddNewAutoSmart.test.js +18 -2
  32. package/src/screens/Automate/Components/InputName.js +7 -6
  33. package/src/screens/Automate/Constants.js +12 -0
  34. package/src/screens/Automate/EditActionsList/UpdateActionScript.js +24 -55
  35. package/src/screens/Automate/EditActionsList/__tests__/UpdateActionScript.test.js +298 -41
  36. package/src/screens/Automate/EditActionsList/__tests__/index.test.js +2 -2
  37. package/src/screens/Automate/EditActionsList/index.js +26 -14
  38. package/src/screens/Automate/MultiUnits.js +9 -1
  39. package/src/screens/Automate/OneTap/__test__/AddNewOneTap.test.js +3 -3
  40. package/src/screens/Automate/ScriptDetail/Components/AddActionScript.js +4 -10
  41. package/src/screens/Automate/ScriptDetail/Components/DeleteScript.js +2 -4
  42. package/src/screens/Automate/ScriptDetail/__test__/index.test.js +78 -0
  43. package/src/screens/Automate/ScriptDetail/index.js +16 -10
  44. package/src/screens/Automate/ScriptDetail/utils.js +39 -15
  45. package/src/screens/Automate/SetSchedule/AddEditConditionSchedule.js +27 -160
  46. package/src/screens/Automate/SetSchedule/EditSchedule.js +269 -0
  47. package/src/screens/Automate/SetSchedule/__test__/AddEditConditionSchedule.test.js +327 -22
  48. package/src/screens/Automate/SetSchedule/__test__/index.test.js +35 -22
  49. package/src/screens/Automate/SetSchedule/components/RepeatOptionsPopup.js +2 -8
  50. package/src/screens/Automate/SetSchedule/index.js +15 -129
  51. package/src/screens/Automate/SetSchedule/styles/indexStyles.js +9 -0
  52. package/src/screens/Automate/__test__/MultiUnits.test.js +6 -1
  53. package/src/screens/Automate/hooks/useAction.js +222 -0
  54. package/src/screens/Device/__test__/detail.test.js +48 -1
  55. package/src/screens/Device/detail.js +46 -3
  56. package/src/screens/PlayBackCamera/__test__/index.test.js +48 -13
  57. package/src/screens/PlayBackCamera/index.js +1 -1
  58. package/src/utils/Apis/axios.js +6 -0
  59. package/src/utils/I18n/translations/en.js +8 -0
  60. package/src/utils/I18n/translations/vi.js +12 -4
  61. package/src/screens/Automate/constants.js +0 -0
@@ -44,7 +44,15 @@ describe('test AddNewAutoSmart', () => {
44
44
  expect(global.mockedNavigate).toHaveBeenCalledWith(
45
45
  Routes.SelectMonitorDevices,
46
46
  {
47
- automate: { unit: 1, type: 'value_change' },
47
+ automate: {
48
+ type: 'value_change',
49
+ conditions: [
50
+ {
51
+ type: 'value_change',
52
+ },
53
+ ],
54
+ unit: 1,
55
+ },
48
56
  closeScreen: undefined,
49
57
  unitId: 1,
50
58
  }
@@ -64,7 +72,15 @@ describe('test AddNewAutoSmart', () => {
64
72
  expect(items[2].props.isSelected).toBeTruthy();
65
73
 
66
74
  expect(global.mockedNavigate).toHaveBeenCalledWith(Routes.SetSchedule, {
67
- automate: { type: 'schedule', unit: 1 },
75
+ automate: {
76
+ type: 'schedule',
77
+ conditions: [
78
+ {
79
+ type: 'schedule',
80
+ },
81
+ ],
82
+ unit: 1,
83
+ },
68
84
  closeScreen: undefined,
69
85
  unitId: 1,
70
86
  });
@@ -7,13 +7,13 @@ import { AccessibilityLabel } from '../../../configs/Constants';
7
7
  import _TextInput from '../../../commons/Form/TextInput';
8
8
  import styles from './InputNameStyles';
9
9
  import { useTranslations } from '../../../hooks/Common/useTranslations';
10
- import { axiosPost, axiosPut } from '../../../utils/Apis/axios';
10
+ import { axiosPost } from '../../../utils/Apis/axios';
11
11
  import NewActionWrapper from '../../Automate/AddNewAction/NewActionWrapper';
12
12
  import Routes from '../../../utils/Route';
13
13
 
14
14
  const InputName = ({ title, placeholder }) => {
15
15
  const { automate = {}, closeScreen } = useRoute().params;
16
- const { id: automateNameId, name: automateName } = automate;
16
+ const { name: automateName } = automate;
17
17
  const t = useTranslations();
18
18
  const { navigate } = useNavigation();
19
19
  const [name, setName] = useState(automateName);
@@ -31,9 +31,10 @@ const InputName = ({ title, placeholder }) => {
31
31
  name: name,
32
32
  };
33
33
 
34
- const { success, data } = automateNameId
35
- ? await axiosPut(API.AUTOMATE.UPDATE_AUTOMATE(automateNameId), params)
36
- : await axiosPost(API.AUTOMATE.CREATE_AUTOMATE(), params);
34
+ const { success, data } = await axiosPost(
35
+ API.AUTOMATE.CREATE_AUTOMATE_V2(),
36
+ params
37
+ );
37
38
 
38
39
  if (success) {
39
40
  navigate({
@@ -46,7 +47,7 @@ const InputName = ({ title, placeholder }) => {
46
47
  });
47
48
  }
48
49
  setProcessing(false);
49
- }, [processing, automate, name, automateNameId, navigate, closeScreen]);
50
+ }, [processing, automate, name, navigate, closeScreen]);
50
51
 
51
52
  return (
52
53
  <NewActionWrapper
@@ -0,0 +1,12 @@
1
+ export const REPEAT_OPTIONS = {
2
+ ONCE: 'once',
3
+ EVERYDAY: 'every_day',
4
+ EVERYWEEK: 'every_week',
5
+ TIMERANGE: 'time_range',
6
+ INTERVAL: 'interval',
7
+ };
8
+
9
+ export const TIME_RANGE_REPEAT_OPTIONS = {
10
+ EVERYDAY: 'every_day',
11
+ EVERYWEEK: 'every_week',
12
+ };
@@ -1,13 +1,14 @@
1
- import React, { useCallback, useEffect, useMemo, useState } from 'react';
1
+ import React, { useCallback, useMemo, useState } from 'react';
2
2
  import { View } from 'react-native';
3
3
  import { useNavigation } from '@react-navigation/native';
4
4
 
5
5
  import NewActionWrapper from '../AddNewAction/NewActionWrapper';
6
6
  import { useTranslations } from '../../../hooks/Common/useTranslations';
7
- import { axiosGet, axiosPut } from '../../../utils/Apis/axios';
7
+ import { axiosGet } from '../../../utils/Apis/axios';
8
8
  import { API } from '../../../configs';
9
9
  import { ToastBottomHelper } from '../../../utils/Utils';
10
10
  import Routes from '../../../utils/Route';
11
+ import useAction from '../hooks/useAction';
11
12
  import RenderActionItem from '../AddNewAction/RenderActionItem';
12
13
  import { useBackendPermission } from '../../../utils/Permission/backend';
13
14
  import AccessibilityLabel from '../../../configs/AccessibilityLabel';
@@ -19,30 +20,23 @@ const UpdateActionScript = ({ route }) => {
19
20
  unitId,
20
21
  device,
21
22
  automateId,
22
- numberActionAdded,
23
- scriptItemId,
23
+ activatedScript,
24
+ scriptItems,
24
25
  closeScreen,
25
26
  } = route.params;
26
27
  const permissions = useBackendPermission();
27
28
  const { max_actions_per_automation } = permissions || {};
28
- const [data, setData] = useState([]);
29
- const [actions, setActions] = useState([]);
30
29
  const [processing, setProcessing] = useState(false);
31
30
 
32
- const numberActionCanAdd = useMemo(() => {
33
- return max_actions_per_automation - numberActionAdded;
34
- }, [max_actions_per_automation, numberActionAdded]);
35
-
36
- const fetchData = useCallback(async () => {
37
- const { success, data: automateData } = await axiosGet(
38
- API.DEVICE.DISPLAY_ACTIONS(device.id)
39
- );
31
+ const { displayActions, saveActions, onSelectAction, onUpdateSubmit } =
32
+ useAction(device, scriptItems, activatedScript);
40
33
 
41
- success && setData(automateData);
42
- }, [device.id]);
34
+ const numberActionCanAdd = useMemo(() => {
35
+ return max_actions_per_automation - scriptItems.length;
36
+ }, [max_actions_per_automation, scriptItems.length]);
43
37
 
44
38
  const onSave = useCallback(async () => {
45
- if (actions.length > numberActionCanAdd) {
39
+ if (saveActions.length > numberActionCanAdd + 1) {
46
40
  ToastBottomHelper.error(
47
41
  `${t('you_can_only_add_more', { number: numberActionCanAdd })} ${t(
48
42
  numberActionCanAdd > 1 ? 'actions' : 'action'
@@ -53,18 +47,11 @@ const UpdateActionScript = ({ route }) => {
53
47
  return;
54
48
  }
55
49
  setProcessing(true);
56
- let list_action = [...actions];
57
- list_action = list_action.map((item) => ({
58
- action: item.action,
59
- data: item.data,
60
- }));
61
- const { success } = await axiosPut(
62
- API.AUTOMATE.UPDATE_SCRIPT_ACTION(automateId),
63
- {
64
- id: scriptItemId,
65
- list_action,
66
- unit: unitId,
67
- }
50
+
51
+ const success = await onUpdateSubmit(
52
+ automateId,
53
+ activatedScript.id,
54
+ unitId
68
55
  );
69
56
 
70
57
  if (success) {
@@ -84,38 +71,20 @@ const UpdateActionScript = ({ route }) => {
84
71
  setProcessing(false);
85
72
  }
86
73
  }, [
87
- actions,
88
74
  numberActionCanAdd,
89
75
  automateId,
90
- scriptItemId,
76
+ activatedScript.id,
77
+ saveActions.length,
91
78
  unitId,
92
- t,
79
+ onUpdateSubmit,
93
80
  navigate,
81
+ t,
94
82
  ]);
95
83
 
96
- const handleOnSelectAction = (action) => {
97
- setActions((prevActions) => {
98
- const index = prevActions.findIndex(
99
- (item) => item.index === action.index
100
- );
101
- if (index !== -1) {
102
- const newActions = [...prevActions];
103
- newActions[index] = action;
104
- return newActions;
105
- } else {
106
- return [...prevActions, action];
107
- }
108
- });
109
- };
110
-
111
- useEffect(() => {
112
- fetchData();
113
- }, [fetchData]);
114
-
115
84
  return (
116
85
  <NewActionWrapper
117
86
  closeScreen={closeScreen}
118
- canNext={actions.length}
87
+ canNext={saveActions.length}
119
88
  onNext={onSave}
120
89
  nextTitle={t('update_now')}
121
90
  name={t('choose_action')}
@@ -123,14 +92,14 @@ const UpdateActionScript = ({ route }) => {
123
92
  showLoading={processing}
124
93
  >
125
94
  <View>
126
- {!!data.length &&
127
- data.map((item, index) => (
95
+ {!!displayActions.length &&
96
+ displayActions.map((item, index) => (
128
97
  <RenderActionItem
129
98
  key={`action_item_${index}`}
130
99
  device={device}
131
100
  item={item}
132
101
  index={index}
133
- handleOnSelectAction={handleOnSelectAction}
102
+ handleOnSelectAction={onSelectAction}
134
103
  t={t}
135
104
  />
136
105
  ))}
@@ -20,7 +20,7 @@ const mockedPermission = jest.fn();
20
20
 
21
21
  jest.mock('../../../../utils/Permission/backend', () => ({
22
22
  useBackendPermission: () => ({
23
- max_actions_per_automation: 1,
23
+ max_actions_per_automation: 2,
24
24
  }),
25
25
  }));
26
26
 
@@ -33,12 +33,27 @@ let response = [
33
33
  text_off: 'text_off',
34
34
  action_on: 'action_on',
35
35
  action_off: 'action_off',
36
+ config: 9,
37
+ is_on_value: [1],
36
38
  },
37
39
  },
38
40
  {
41
+ title: 'Trigger',
39
42
  template: 'one_button_action_template',
40
43
  configuration: { text: 'text', action: 'action' },
41
44
  },
45
+ {
46
+ title: 'Garage',
47
+ template: 'three_button_action_template',
48
+ configuration: {
49
+ action1: 'garage open',
50
+ action2: 'garage close',
51
+ action3: 'garage stop',
52
+ text1: 'Open',
53
+ text2: 'Close',
54
+ text3: 'Stop',
55
+ },
56
+ },
42
57
  ];
43
58
  const wrapComponent = (route) => (
44
59
  <SCProvider initState={mockSCStore({})}>
@@ -48,35 +63,38 @@ const wrapComponent = (route) => (
48
63
 
49
64
  describe('Test UpdateActionScript', () => {
50
65
  let tree;
51
- let route = {
52
- params: {
53
- unitId: 1,
54
- automateId: 1,
55
- scriptItemId: 1,
56
- numberActionAdded: 0,
57
- device: { id: 1 },
58
- },
59
- };
66
+ let activatedScript;
67
+ let route;
68
+
60
69
  beforeEach(() => {
70
+ activatedScript = {
71
+ id: 1,
72
+ action_script: {
73
+ id: 1,
74
+ action_name: 'ON',
75
+ data: null,
76
+ end_device_id: 2,
77
+ sensor_name: 'LED',
78
+ station_name: 'Room',
79
+ unit_name: 'EoH Office',
80
+ },
81
+ toggle_script: null,
82
+ order: 1,
83
+ };
84
+ route = {
85
+ params: {
86
+ unitId: 1,
87
+ automateId: 1,
88
+ activatedScript,
89
+ scriptItems: [activatedScript],
90
+ device: { id: 1 },
91
+ },
92
+ };
61
93
  useRoute.mockReturnValue({
62
94
  params: {},
63
95
  });
64
96
  mockedPermission.mockClear();
65
- });
66
-
67
- it('test update action script', async () => {
68
- mock.onGet(API.DEVICE.DISPLAY_ACTIONS(1)).reply(200, response);
69
- mock.onPut(API.AUTOMATE.UPDATE_SCRIPT_ACTION(1)).reply(200);
70
- mock.onGet(API.AUTOMATE.SCRIPT_ITEMS(1)).reply(200, { script_items: [] });
71
- await act(async () => {
72
- tree = await renderer.create(wrapComponent(route));
73
- });
74
- const instance = tree.root;
75
- await showAllCards(instance);
76
- await selectActionOn(instance);
77
- await pressSaveButton(instance);
78
-
79
- expect(global.mockedNavigate).toHaveBeenCalled();
97
+ mock.reset();
80
98
  });
81
99
 
82
100
  const pressSaveButton = async (instance) => {
@@ -90,20 +108,6 @@ describe('Test UpdateActionScript', () => {
90
108
  });
91
109
  };
92
110
 
93
- it('test update action script fail', async () => {
94
- mock.onGet(API.DEVICE.DISPLAY_ACTIONS(1)).reply(200, response);
95
- mock.onPut(API.AUTOMATE.UPDATE_SCRIPT_ACTION(1)).reply(400);
96
- await act(async () => {
97
- tree = await renderer.create(wrapComponent(route));
98
- });
99
- const instance = tree.root;
100
- await showAllCards(instance);
101
- await selectActionOn(instance);
102
- await pressSaveButton(instance);
103
-
104
- expect(global.mockedNavigate).not.toHaveBeenCalled();
105
- });
106
-
107
111
  const selectActionOn = async (instance) => {
108
112
  const buttonOn = instance.findAll(
109
113
  (el) =>
@@ -126,6 +130,17 @@ describe('Test UpdateActionScript', () => {
126
130
  });
127
131
  };
128
132
 
133
+ const selectActionToggle = async (instance) => {
134
+ const buttonToggle = instance.findAll(
135
+ (el) =>
136
+ el.props.accessibilityLabel === AccessibilityLabel.TOGGLE_BUTTON &&
137
+ el.type === TouchableOpacity
138
+ );
139
+ await act(async () => {
140
+ buttonToggle[0].props.onPress();
141
+ });
142
+ };
143
+
129
144
  const selectActionOne = async (instance) => {
130
145
  const buttonOne = instance.findAll(
131
146
  (el) =>
@@ -137,6 +152,243 @@ describe('Test UpdateActionScript', () => {
137
152
  });
138
153
  };
139
154
 
155
+ const selectActionFirstThreeTemplate = async (instance) => {
156
+ const buttonOne = instance.findAll(
157
+ (el) =>
158
+ el.props.accessibilityLabel === AccessibilityLabel.BUTTON_TEMPLATE_1 &&
159
+ el.type === TouchableOpacity
160
+ );
161
+ await act(async () => {
162
+ buttonOne[0].props.onPress();
163
+ });
164
+ };
165
+
166
+ it('test update action script', async () => {
167
+ mock.onGet(API.DEVICE.DISPLAY_ACTIONS(1)).reply(200, response);
168
+ mock.onPut(API.AUTOMATE.UPDATE_SCRIPT_ACTION(1)).reply(200);
169
+ mock.onGet(API.AUTOMATE.SCRIPT_ITEMS(1)).reply(200, { script_items: [] });
170
+ await act(async () => {
171
+ tree = await renderer.create(wrapComponent(route));
172
+ });
173
+ const instance = tree.root;
174
+ await showAllCards(instance);
175
+ await selectActionOn(instance);
176
+ await pressSaveButton(instance);
177
+
178
+ expect(mock.history.put).toHaveLength(1);
179
+ expect(mock.history.put[0].url).toEqual(
180
+ API.AUTOMATE.UPDATE_SCRIPT_ACTION(1)
181
+ );
182
+ expect(mock.history.put[0].data).toEqual(
183
+ JSON.stringify({
184
+ id: 1,
185
+ list_action: [
186
+ {
187
+ action: 'action_on',
188
+ data: null,
189
+ order: 1,
190
+ },
191
+ ],
192
+ number_action: 1,
193
+ unit: 1,
194
+ })
195
+ );
196
+ expect(global.mockedNavigate).toHaveBeenCalled();
197
+ });
198
+
199
+ it('test update action script to toggle script and add new action', async () => {
200
+ mock.onGet(API.DEVICE.DISPLAY_ACTIONS(1)).reply(200, response);
201
+ mock.onDelete(API.AUTOMATE.DELETE_SCRIPT_ITEM(1, 1)).reply(200);
202
+ mock.onPost(API.AUTOMATE.ADD_SCRIPT_ACTION(1)).reply(200);
203
+ mock.onPost(API.AUTOMATE.ADD_SCRIPT_TOGGLE(1)).reply(200);
204
+ mock.onGet(API.AUTOMATE.SCRIPT_ITEMS(1)).reply(200, { script_items: [] });
205
+ await act(async () => {
206
+ tree = await renderer.create(wrapComponent(route));
207
+ });
208
+ const instance = tree.root;
209
+ await showAllCards(instance);
210
+ await selectActionToggle(instance);
211
+ await selectActionOne(instance);
212
+ await pressSaveButton(instance);
213
+
214
+ expect(mock.history.delete).toHaveLength(1);
215
+ expect(mock.history.delete[0].url).toEqual(
216
+ API.AUTOMATE.DELETE_SCRIPT_ITEM(1, 1)
217
+ );
218
+ expect(mock.history.post).toHaveLength(2);
219
+ expect(mock.history.post[0].url).toEqual(API.AUTOMATE.ADD_SCRIPT_TOGGLE(1));
220
+ expect(mock.history.post[0].data).toEqual(
221
+ JSON.stringify({
222
+ list_action: [
223
+ {
224
+ action_on: 'action_on',
225
+ action_off: 'action_off',
226
+ config: 9,
227
+ action_on_data: null,
228
+ action_off_data: null,
229
+ is_on_values: [1],
230
+ order: 1,
231
+ },
232
+ ],
233
+ start_order: 1,
234
+ number_action: 2,
235
+ unit: 1,
236
+ })
237
+ );
238
+ expect(mock.history.post[1].url).toEqual(API.AUTOMATE.ADD_SCRIPT_ACTION(1));
239
+ expect(mock.history.post[1].data).toEqual(
240
+ JSON.stringify({
241
+ list_action: [
242
+ {
243
+ action: 'action',
244
+ data: null,
245
+ order: 2,
246
+ },
247
+ ],
248
+ unit: 1,
249
+ })
250
+ );
251
+ expect(global.mockedNavigate).toHaveBeenCalled();
252
+ });
253
+
254
+ it('test update toggle script', async () => {
255
+ activatedScript = {
256
+ ...activatedScript,
257
+ action_script: null,
258
+ toggle_script: {
259
+ id: 1,
260
+ end_device_id: 2,
261
+ sensor_name: 'LED',
262
+ station_name: 'Room',
263
+ unit_name: 'EoH Office',
264
+ },
265
+ };
266
+ route.params = {
267
+ ...route.params,
268
+ activatedScript,
269
+ scriptItems: [activatedScript],
270
+ };
271
+ mock.onGet(API.DEVICE.DISPLAY_ACTIONS(1)).reply(200, response);
272
+ mock.onPut(API.AUTOMATE.UPDATE_SCRIPT_TOGGLE(1)).reply(200);
273
+ mock.onGet(API.AUTOMATE.SCRIPT_ITEMS(1)).reply(200, { script_items: [] });
274
+ await act(async () => {
275
+ tree = await renderer.create(wrapComponent(route));
276
+ });
277
+ const instance = tree.root;
278
+ await showAllCards(instance);
279
+ await selectActionToggle(instance);
280
+ await pressSaveButton(instance);
281
+
282
+ expect(mock.history.put).toHaveLength(1);
283
+ expect(mock.history.put[0].url).toEqual(
284
+ API.AUTOMATE.UPDATE_SCRIPT_TOGGLE(1)
285
+ );
286
+ expect(mock.history.put[0].data).toEqual(
287
+ JSON.stringify({
288
+ id: 1,
289
+ list_action: [
290
+ {
291
+ action_on: 'action_on',
292
+ action_off: 'action_off',
293
+ config: 9,
294
+ action_on_data: null,
295
+ action_off_data: null,
296
+ is_on_values: [1],
297
+ order: 1,
298
+ },
299
+ ],
300
+ number_action: 1,
301
+ unit: 1,
302
+ })
303
+ );
304
+ expect(global.mockedNavigate).toHaveBeenCalled();
305
+ });
306
+
307
+ it('test update toggle script to action script and add new toggle', async () => {
308
+ activatedScript = {
309
+ ...activatedScript,
310
+ action_script: null,
311
+ toggle_script: {
312
+ id: 1,
313
+ end_device_id: 2,
314
+ sensor_name: 'LED',
315
+ station_name: 'Room',
316
+ unit_name: 'EoH Office',
317
+ },
318
+ };
319
+ route.params = {
320
+ ...route.params,
321
+ activatedScript,
322
+ scriptItems: [activatedScript],
323
+ };
324
+ mock.onGet(API.DEVICE.DISPLAY_ACTIONS(1)).reply(200, response);
325
+ mock.onDelete(API.AUTOMATE.DELETE_SCRIPT_ITEM(1, 1)).reply(200);
326
+ mock.onPost(API.AUTOMATE.ADD_SCRIPT_ACTION(1)).reply(200);
327
+ mock.onPost(API.AUTOMATE.ADD_SCRIPT_TOGGLE(1)).reply(200);
328
+ mock.onGet(API.AUTOMATE.SCRIPT_ITEMS(1)).reply(200, { script_items: [] });
329
+ await act(async () => {
330
+ tree = await renderer.create(wrapComponent(route));
331
+ });
332
+ const instance = tree.root;
333
+ await showAllCards(instance);
334
+ await selectActionOne(instance);
335
+ await selectActionToggle(instance);
336
+ await pressSaveButton(instance);
337
+
338
+ expect(mock.history.delete).toHaveLength(1);
339
+ expect(mock.history.delete[0].url).toEqual(
340
+ API.AUTOMATE.DELETE_SCRIPT_ITEM(1, 1)
341
+ );
342
+ expect(mock.history.post).toHaveLength(2);
343
+ expect(mock.history.post[0].url).toEqual(API.AUTOMATE.ADD_SCRIPT_ACTION(1));
344
+ expect(mock.history.post[0].data).toEqual(
345
+ JSON.stringify({
346
+ list_action: [
347
+ {
348
+ action: 'action',
349
+ data: null,
350
+ order: 2,
351
+ },
352
+ ],
353
+ start_order: 2,
354
+ number_action: 2,
355
+ unit: 1,
356
+ })
357
+ );
358
+ expect(mock.history.post[1].url).toEqual(API.AUTOMATE.ADD_SCRIPT_TOGGLE(1));
359
+ expect(mock.history.post[1].data).toEqual(
360
+ JSON.stringify({
361
+ list_action: [
362
+ {
363
+ action_on: 'action_on',
364
+ action_off: 'action_off',
365
+ config: 9,
366
+ action_on_data: null,
367
+ action_off_data: null,
368
+ is_on_values: [1],
369
+ order: 1,
370
+ },
371
+ ],
372
+ unit: 1,
373
+ })
374
+ );
375
+ expect(global.mockedNavigate).toHaveBeenCalled();
376
+ });
377
+
378
+ it('test update action script fail', async () => {
379
+ mock.onGet(API.DEVICE.DISPLAY_ACTIONS(1)).reply(200, response);
380
+ mock.onPut(API.AUTOMATE.UPDATE_SCRIPT_ACTION(1)).reply(400);
381
+ await act(async () => {
382
+ tree = await renderer.create(wrapComponent(route));
383
+ });
384
+ const instance = tree.root;
385
+ await showAllCards(instance);
386
+ await selectActionOn(instance);
387
+ await pressSaveButton(instance);
388
+
389
+ expect(global.mockedNavigate).not.toHaveBeenCalled();
390
+ });
391
+
140
392
  it('test can not update because adding too many times allowed', async () => {
141
393
  mock.onGet(API.DEVICE.DISPLAY_ACTIONS(1)).reply(200, response);
142
394
  const spyToast = jest.spyOn(ToastBottomHelper, 'error');
@@ -145,13 +397,18 @@ describe('Test UpdateActionScript', () => {
145
397
  });
146
398
 
147
399
  const instance = tree.root;
148
- expect(instance.findAllByType(RenderActionItem).length).toBe(2);
400
+ expect(instance.findAllByType(RenderActionItem).length).toBe(3);
149
401
  await showAllCards(instance);
150
402
  await selectActionOn(instance);
151
403
  await selectActionOff(instance);
152
404
  await selectActionOne(instance);
405
+ await selectActionFirstThreeTemplate(instance);
153
406
  await pressSaveButton(instance);
154
407
 
155
- expect(spyToast).toHaveBeenCalled();
408
+ expect(spyToast).toHaveBeenCalledWith(
409
+ 'You can only add more 1 action',
410
+ null,
411
+ 3000
412
+ );
156
413
  });
157
414
  });
@@ -170,12 +170,12 @@ describe('EditActionsList', () => {
170
170
  Routes.UpdateActionScript,
171
171
  {
172
172
  automateId: 1,
173
+ activatedScript: actionsList[0],
173
174
  closeScreen: Routes.EditActionsList,
174
175
  device: {
175
176
  id: 1,
176
177
  },
177
- numberActionAdded: 4,
178
- scriptItemId: 1,
178
+ scriptItems: actionsList,
179
179
  unitId: undefined,
180
180
  }
181
181
  );