@eohjsc/react-native-smart-city 0.4.54 → 0.4.56
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 +1 -1
- package/src/commons/Action/ItemQuickAction.js +1 -5
- package/src/commons/Action/__test__/ItemQuickAction.test.js +6 -2
- package/src/screens/Device/components/SensorDisplayItem.js +13 -3
- package/src/screens/Device/hooks/__test__/useEvaluateValue.test.js +105 -34
- package/src/screens/Device/hooks/useEvaluateValue.js +15 -25
package/package.json
CHANGED
|
@@ -58,7 +58,7 @@ const ItemQuickAction = memo(
|
|
|
58
58
|
|
|
59
59
|
const userId = useSCContextSelector((state) => state?.auth.account.user.id);
|
|
60
60
|
const onActionPress = useCallback(async () => {
|
|
61
|
-
if (processing) {
|
|
61
|
+
if (processing || !action) {
|
|
62
62
|
/* istanbul ignore next */
|
|
63
63
|
return;
|
|
64
64
|
}
|
|
@@ -98,10 +98,6 @@ const ItemQuickAction = memo(
|
|
|
98
98
|
on_action?.id,
|
|
99
99
|
]);
|
|
100
100
|
|
|
101
|
-
if (!action) {
|
|
102
|
-
return <View />;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
101
|
return (
|
|
106
102
|
<TouchableOpacity
|
|
107
103
|
accessibilityLabel={`${AccessibilityLabel.ITEM_QUICK_ACTION_PRESS}-${sensor?.id}`}
|
|
@@ -7,6 +7,7 @@ import { AccessibilityLabel } from '../../../configs/Constants';
|
|
|
7
7
|
import { factory } from 'factory-girl';
|
|
8
8
|
import IconComponent from '../../IconComponent';
|
|
9
9
|
import { IconOutline } from '@ant-design/icons-react-native';
|
|
10
|
+
import Toast from 'react-native-toast-message';
|
|
10
11
|
|
|
11
12
|
class Sensor {}
|
|
12
13
|
|
|
@@ -67,13 +68,16 @@ describe('Test ItemQuickAction', () => {
|
|
|
67
68
|
tree = await create(<ItemQuickAction sensor={newSensor} />);
|
|
68
69
|
});
|
|
69
70
|
const instance = tree.root;
|
|
70
|
-
const buttonOnActionPress = instance.
|
|
71
|
+
const buttonOnActionPress = instance.find(
|
|
71
72
|
(el) =>
|
|
72
73
|
el.props.accessibilityLabel ===
|
|
73
74
|
`${AccessibilityLabel.ITEM_QUICK_ACTION_PRESS}-${sensor?.id}` &&
|
|
74
75
|
el.type === TouchableOpacity
|
|
75
76
|
);
|
|
76
|
-
|
|
77
|
+
await act(async () => {
|
|
78
|
+
await buttonOnActionPress.props.onPress();
|
|
79
|
+
});
|
|
80
|
+
expect(Toast.show).not.toBeCalled();
|
|
77
81
|
});
|
|
78
82
|
|
|
79
83
|
it('click quick action icon down , isSendingCommand = true', async () => {
|
|
@@ -36,7 +36,8 @@ export const SensorDisplayItem = ({
|
|
|
36
36
|
}) => {
|
|
37
37
|
const userId = useSCContextSelector((state) => state.auth.account.user.id);
|
|
38
38
|
const { configuration = {}, id: idTemplate } = item;
|
|
39
|
-
const { type, template, uri, id, name, title } =
|
|
39
|
+
const { type, template, uri, id, name, title, value_evaluation } =
|
|
40
|
+
configuration;
|
|
40
41
|
|
|
41
42
|
const sendRemoteCommand = useRemoteControl();
|
|
42
43
|
const [processing, setProcessing] = useState(false);
|
|
@@ -52,8 +53,10 @@ export const SensorDisplayItem = ({
|
|
|
52
53
|
|
|
53
54
|
const data = (item.configuration.configs || []).map((config) => {
|
|
54
55
|
const configValue = configValues[config.id]?.value;
|
|
56
|
+
|
|
55
57
|
const configEvaluate = evaluate[config.id] || {};
|
|
56
58
|
let value = {};
|
|
59
|
+
|
|
57
60
|
if (configValue === null || configValue === undefined) {
|
|
58
61
|
value = {
|
|
59
62
|
id: config.id,
|
|
@@ -64,14 +67,21 @@ export const SensorDisplayItem = ({
|
|
|
64
67
|
value = {
|
|
65
68
|
id: config.id,
|
|
66
69
|
value: configValue,
|
|
67
|
-
evaluate:
|
|
70
|
+
evaluate:
|
|
71
|
+
evaluateValue(configValue, value_evaluation) || configEvaluate,
|
|
68
72
|
};
|
|
69
73
|
}
|
|
70
74
|
return { ...config, ...value };
|
|
71
75
|
});
|
|
72
76
|
|
|
73
77
|
return data.filter(Boolean);
|
|
74
|
-
}, [
|
|
78
|
+
}, [
|
|
79
|
+
configValues,
|
|
80
|
+
evaluate,
|
|
81
|
+
evaluateValue,
|
|
82
|
+
item.configuration,
|
|
83
|
+
value_evaluation,
|
|
84
|
+
]);
|
|
75
85
|
|
|
76
86
|
const doAction = useCallback(
|
|
77
87
|
async (action, data) => {
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
1
|
import { renderHook } from '@testing-library/react-hooks';
|
|
3
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
useEvaluateValue,
|
|
4
|
+
useGetEvaluateValue,
|
|
5
|
+
valueEvaluationFuncs,
|
|
6
|
+
} from '../useEvaluateValue';
|
|
7
|
+
import React from 'react';
|
|
4
8
|
import { SCProvider } from '../../../../context';
|
|
5
9
|
import { mockSCStore } from '../../../../context/mockStore';
|
|
6
10
|
import { API } from '../../../../configs';
|
|
@@ -24,7 +28,7 @@ const mockUseContext = jest.fn().mockImplementation(() => ({
|
|
|
24
28
|
|
|
25
29
|
React.useContext = mockUseContext;
|
|
26
30
|
|
|
27
|
-
describe('
|
|
31
|
+
describe('useEvaluateValue', () => {
|
|
28
32
|
let valueEvaluations = {};
|
|
29
33
|
|
|
30
34
|
beforeEach(() => {
|
|
@@ -58,50 +62,117 @@ describe('Test useEvaluateValue', () => {
|
|
|
58
62
|
};
|
|
59
63
|
});
|
|
60
64
|
|
|
61
|
-
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
+
test('should evaluate range correctly', () => {
|
|
66
|
+
const value = 1.2;
|
|
67
|
+
const valueEvaluation = {
|
|
68
|
+
id: 8662,
|
|
69
|
+
template: 'range',
|
|
70
|
+
configuration: {
|
|
71
|
+
ranges: [
|
|
72
|
+
{
|
|
73
|
+
end: 1,
|
|
74
|
+
start: 1,
|
|
75
|
+
evaluate: {
|
|
76
|
+
icon: 'CheckCircleOutlined',
|
|
77
|
+
text: 'Active',
|
|
78
|
+
borderColor: '#00d084',
|
|
79
|
+
backgroundColor: 'rgba(0, 208, 132, 0.3)',
|
|
80
|
+
},
|
|
81
|
+
end_condition: '<',
|
|
82
|
+
start_condition: '>=',
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
end: 0,
|
|
86
|
+
start: 0,
|
|
87
|
+
evaluate: {
|
|
88
|
+
icon: 'CheckCircleOutlined',
|
|
89
|
+
text: 'InActive',
|
|
90
|
+
borderColor: '#abb8c3',
|
|
91
|
+
backgroundColor: 'rgba(171, 184, 195, 0.3)',
|
|
92
|
+
},
|
|
93
|
+
end_condition: '<',
|
|
94
|
+
start_condition: '>=',
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
},
|
|
98
|
+
configs: [28265],
|
|
99
|
+
};
|
|
65
100
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
});
|
|
101
|
+
const { result } = renderHook(() => useEvaluateValue());
|
|
102
|
+
const evaluateValue = result.current;
|
|
69
103
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
104
|
+
const evaluatedResult = evaluateValue(value, valueEvaluation);
|
|
105
|
+
expect(evaluatedResult).toEqual({
|
|
106
|
+
backgroundColor: 'rgba(171, 184, 195, 0.3)',
|
|
107
|
+
borderColor: '#abb8c3',
|
|
108
|
+
icon: 'CheckCircleOutlined',
|
|
109
|
+
text: 'InActive',
|
|
73
110
|
});
|
|
74
|
-
|
|
75
|
-
expect(evaluateValue.current(3, 1)).toBe(null);
|
|
76
111
|
});
|
|
77
112
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
113
|
+
test('should evaluate boolean correctly', () => {
|
|
114
|
+
const value = 0;
|
|
115
|
+
const valueEvaluation = {
|
|
116
|
+
id: 8662,
|
|
117
|
+
template: 'boolean',
|
|
118
|
+
configuration: {
|
|
119
|
+
on: {
|
|
120
|
+
value: 1,
|
|
121
|
+
evaluate: {
|
|
122
|
+
text: 'On',
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
off: {
|
|
126
|
+
value: 0,
|
|
127
|
+
evaluate: {
|
|
128
|
+
text: 'Off',
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
configs: [28265],
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const { result } = renderHook(() => useEvaluateValue());
|
|
136
|
+
const evaluateValue = result.current;
|
|
83
137
|
|
|
84
|
-
|
|
138
|
+
const evaluatedResult = evaluateValue(value, valueEvaluation);
|
|
139
|
+
expect(evaluatedResult).toEqual({ text: 'Off' });
|
|
85
140
|
});
|
|
86
141
|
|
|
87
|
-
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
142
|
+
test('should handle unknown valueEvaluation template', () => {
|
|
143
|
+
const value = 0;
|
|
144
|
+
const configuration = {};
|
|
145
|
+
|
|
146
|
+
const { result } = renderHook(() => useEvaluateValue());
|
|
147
|
+
const evaluateValue = result.current;
|
|
91
148
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
149
|
+
const expectedResult = { text: value };
|
|
150
|
+
const evaluatedResult = evaluateValue(
|
|
151
|
+
value,
|
|
152
|
+
'unknown-template',
|
|
153
|
+
configuration
|
|
154
|
+
);
|
|
155
|
+
expect(evaluatedResult).toEqual(expectedResult);
|
|
95
156
|
});
|
|
96
157
|
|
|
97
|
-
|
|
98
|
-
const
|
|
99
|
-
|
|
158
|
+
test('should handle error in evaluateFunc', () => {
|
|
159
|
+
const value = 0;
|
|
160
|
+
const configuration = {};
|
|
161
|
+
|
|
162
|
+
const { result } = renderHook(() => useEvaluateValue());
|
|
163
|
+
const evaluateValue = result.current;
|
|
164
|
+
|
|
165
|
+
// Mocking a faulty evaluateFunc that throws an error
|
|
166
|
+
valueEvaluationFuncs.range = jest.fn(() => {
|
|
167
|
+
throw new Error('Evaluation error');
|
|
100
168
|
});
|
|
101
169
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
170
|
+
const evaluatedResult = evaluateValue(
|
|
171
|
+
value,
|
|
172
|
+
valueEvaluationFuncs.range,
|
|
173
|
+
configuration
|
|
174
|
+
);
|
|
175
|
+
expect(evaluatedResult).toEqual({ text: 0 });
|
|
105
176
|
});
|
|
106
177
|
|
|
107
178
|
it('test evaluate fetch from server if not exists', async () => {
|
|
@@ -13,6 +13,7 @@ const evaluateRange = (value, configuration) => {
|
|
|
13
13
|
]
|
|
14
14
|
}
|
|
15
15
|
*/
|
|
16
|
+
|
|
16
17
|
if (!value) {
|
|
17
18
|
value = 0;
|
|
18
19
|
}
|
|
@@ -65,34 +66,23 @@ export const valueEvaluationFuncs = {
|
|
|
65
66
|
};
|
|
66
67
|
|
|
67
68
|
export const useEvaluateValue = () => {
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const evaluateValue = useCallback(
|
|
73
|
-
(configId, value) => {
|
|
74
|
-
if (value === null || value === undefined) {
|
|
75
|
-
return { text: '--', color: null };
|
|
76
|
-
}
|
|
69
|
+
const evaluateValue = useCallback((value, valueEvaluation) => {
|
|
70
|
+
if (valueEvaluation === null || valueEvaluation === undefined) {
|
|
71
|
+
return { text: '--', color: null };
|
|
72
|
+
}
|
|
77
73
|
|
|
78
|
-
|
|
79
|
-
if (!valueEvaluation) {
|
|
80
|
-
return null;
|
|
81
|
-
}
|
|
74
|
+
const evaluateFunc = valueEvaluationFuncs[valueEvaluation.template];
|
|
82
75
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
76
|
+
if (!evaluateFunc) {
|
|
77
|
+
return { text: value };
|
|
78
|
+
}
|
|
87
79
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
[valueEvaluations]
|
|
95
|
-
);
|
|
80
|
+
try {
|
|
81
|
+
return evaluateFunc(value, valueEvaluation.configuration);
|
|
82
|
+
} catch (e) {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
}, []);
|
|
96
86
|
|
|
97
87
|
return evaluateValue;
|
|
98
88
|
};
|