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

mendersoftware / gui / 966933164

pending completion
966933164

Pull #3943

gitlab-ci

mzedel
feat(e2e-tests): added test for search functionality

Ticket: None
Changelog: None
Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #3943: feat(e2e-tests): added test for search functionality

4365 of 6355 branches covered (68.69%)

8244 of 10042 relevant lines covered (82.1%)

193.8 hits per line

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

52.75
/src/js/components/devices/device-groups.js
1
// Copyright 2018 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14
import React, { useCallback, useEffect, useRef, useState } from 'react';
15
import { useDispatch, useSelector } from 'react-redux';
16
import { useParams } from 'react-router-dom';
17

18
import { AddCircle as AddIcon } from '@mui/icons-material';
19
import { Dialog, DialogContent, DialogTitle } from '@mui/material';
20

21
import pluralize from 'pluralize';
22

23
import { setOfflineThreshold } from '../../actions/appActions';
24
import {
25
  addDynamicGroup,
26
  addStaticGroup,
27
  removeDevicesFromGroup,
28
  removeDynamicGroup,
29
  removeStaticGroup,
30
  selectGroup,
31
  setDeviceFilters,
32
  setDeviceListState,
33
  updateDynamicGroup
34
} from '../../actions/deviceActions';
35
import { setShowConnectingDialog } from '../../actions/userActions';
36
import { SORTING_OPTIONS } from '../../constants/appConstants';
37
import { DEVICE_FILTERING_OPTIONS, DEVICE_ISSUE_OPTIONS, DEVICE_STATES, emptyFilter } from '../../constants/deviceConstants';
38
import { onboardingSteps } from '../../constants/onboardingConstants';
39
import { toggle } from '../../helpers';
40
import {
41
  getAcceptedDevices,
42
  getDeviceCountsByStatus,
43
  getDeviceFilters,
44
  getDeviceLimit,
45
  getFeatures,
46
  getGroups as getGroupsSelector,
47
  getIsEnterprise,
48
  getIsPreview,
49
  getLimitMaxed,
50
  getOnboardingState,
51
  getSelectedGroupInfo,
52
  getSortedFilteringAttributes,
53
  getTenantCapabilities,
54
  getUserCapabilities
55
} from '../../selectors';
56
import { useLocationParams } from '../../utils/liststatehook';
57
import { getOnboardingComponentFor } from '../../utils/onboardingmanager';
58
import Global from '../settings/global';
59
import AuthorizedDevices from './authorized-devices';
60
import DeviceStatusNotification from './devicestatusnotification';
61
import MakeGatewayDialog from './dialogs/make-gateway-dialog';
62
import PreauthDialog, { DeviceLimitWarning } from './dialogs/preauth-dialog';
63
import CreateGroup from './group-management/create-group';
64
import CreateGroupExplainer from './group-management/create-group-explainer';
65
import RemoveGroup from './group-management/remove-group';
66
import Groups from './groups';
67
import DeviceAdditionWidget from './widgets/deviceadditionwidget';
68

69
export const DeviceGroups = () => {
4✔
70
  const [createGroupExplanation, setCreateGroupExplanation] = useState(false);
2✔
71
  const [fromFilters, setFromFilters] = useState(false);
2✔
72
  const [modifyGroupDialog, setModifyGroupDialog] = useState(false);
2✔
73
  const [openIdDialog, setOpenIdDialog] = useState(false);
2✔
74
  const [openPreauth, setOpenPreauth] = useState(false);
2✔
75
  const [showMakeGateway, setShowMakeGateway] = useState(false);
2✔
76
  const [removeGroup, setRemoveGroup] = useState(false);
2✔
77
  const [tmpDevices, setTmpDevices] = useState([]);
2✔
78
  const deviceConnectionRef = useRef();
2✔
79
  const { status: statusParam } = useParams();
2✔
80

81
  const { groupCount, selectedGroup, groupFilters = [] } = useSelector(getSelectedGroupInfo);
2!
82
  const filteringAttributes = useSelector(getSortedFilteringAttributes);
2✔
83
  const { canManageDevices } = useSelector(getUserCapabilities);
2✔
84
  const tenantCapabilities = useSelector(getTenantCapabilities);
2✔
85
  const { groupNames, ...groupsByType } = useSelector(getGroupsSelector);
2✔
86
  const groups = groupNames;
2✔
87
  const { total: acceptedCount = 0 } = useSelector(getAcceptedDevices);
2!
88
  const authRequestCount = useSelector(state => state.monitor.issueCounts.byType[DEVICE_ISSUE_OPTIONS.authRequests.key].total);
5✔
89
  const canPreview = useSelector(getIsPreview);
2✔
90
  const deviceLimit = useSelector(getDeviceLimit);
2✔
91
  const deviceListState = useSelector(state => state.devices.deviceList);
5✔
92
  const features = useSelector(getFeatures);
2✔
93
  const { hasReporting } = features;
2✔
94
  const filters = useSelector(getDeviceFilters);
2✔
95
  const limitMaxed = useSelector(getLimitMaxed);
2✔
96
  const { pending: pendingCount } = useSelector(getDeviceCountsByStatus);
2✔
97
  const showDeviceConnectionDialog = useSelector(state => state.users.showConnectDeviceDialog);
5✔
98
  const onboardingState = useSelector(getOnboardingState);
2✔
99
  const isEnterprise = useSelector(getIsEnterprise);
2✔
100
  const dispatch = useDispatch();
2✔
101
  const isInitialized = useRef(false);
2✔
102

103
  const [locationParams, setLocationParams] = useLocationParams('devices', {
2✔
104
    filteringAttributes,
105
    filters,
106
    defaults: { sort: { direction: SORTING_OPTIONS.desc } }
107
  });
108

109
  const { refreshTrigger, selectedId, state: selectedState } = deviceListState;
2✔
110

111
  const refreshListState = useCallback(() => dispatch(setDeviceListState({ refreshTrigger: !refreshTrigger })), [dispatch, refreshTrigger]);
2✔
112

113
  useEffect(() => {
2✔
114
    if (!isInitialized.current) {
2!
115
      return;
2✔
116
    }
117
    setLocationParams({ pageState: deviceListState, filters, selectedGroup });
×
118
    // eslint-disable-next-line react-hooks/exhaustive-deps
119
  }, [
120
    deviceListState.detailsTab,
121
    deviceListState.page,
122
    deviceListState.perPage,
123
    deviceListState.selectedIssues,
124
    // eslint-disable-next-line react-hooks/exhaustive-deps
125
    JSON.stringify(deviceListState.sort),
126
    refreshTrigger,
127
    selectedId,
128
    filters,
129
    selectedGroup,
130
    selectedState,
131
    setLocationParams
132
  ]);
133

134
  useEffect(() => {
2✔
135
    const { groupName, filters = [], id = [], ...remainder } = locationParams;
1!
136
    const { hasFullFiltering } = tenantCapabilities;
1✔
137
    if (groupName) {
1!
138
      dispatch(selectGroup(groupName, filters));
×
139
    } else if (filters.length) {
1!
140
      dispatch(setDeviceFilters(filters));
×
141
    }
142
    let listState = { ...remainder };
1✔
143
    if (statusParam && Object.values(DEVICE_STATES).some(state => state === statusParam)) {
1!
144
      listState.state = statusParam;
×
145
    }
146
    if (id.length === 1 && Boolean(locationParams.open)) {
1!
147
      listState.selectedId = id[0];
×
148
    } else if (id.length && hasFullFiltering) {
1!
149
      dispatch(setDeviceFilters([...filters, { ...emptyFilter, key: 'id', operator: DEVICE_FILTERING_OPTIONS.$in.key, value: id }]));
×
150
    }
151
    dispatch(setDeviceListState(listState)).then(() => {
1✔
152
      if (isInitialized.current) {
×
153
        return;
×
154
      }
155
      isInitialized.current = true;
×
156
      refreshListState();
×
157
      dispatch(setOfflineThreshold());
×
158
    });
159
    // eslint-disable-next-line react-hooks/exhaustive-deps
160
  }, [dispatch, JSON.stringify(tenantCapabilities), JSON.stringify(locationParams), statusParam]);
161

162
  /*
163
   * Groups
164
   */
165
  const removeCurrentGroup = () => {
2✔
166
    const request = groupFilters.length ? dispatch(removeDynamicGroup(selectedGroup)) : dispatch(removeStaticGroup(selectedGroup));
×
167
    return request.then(toggleGroupRemoval).catch(console.log);
×
168
  };
169

170
  // Edit groups from device selection
171
  const addDevicesToGroup = tmpDevices => {
2✔
172
    // (save selected devices in state, open dialog)
173
    setTmpDevices(tmpDevices);
×
174
    setModifyGroupDialog(toggle);
×
175
  };
176

177
  const createGroupFromDialog = (devices, group) => {
2✔
178
    let request = fromFilters ? dispatch(addDynamicGroup(group, filters)) : dispatch(addStaticGroup(group, devices));
×
179
    return request.then(() => {
×
180
      // reached end of list
181
      setCreateGroupExplanation(false);
×
182
      setModifyGroupDialog(false);
×
183
      setFromFilters(false);
×
184
    });
185
  };
186

187
  const onGroupClick = () => {
2✔
188
    if (selectedGroup && groupFilters.length) {
×
189
      return dispatch(updateDynamicGroup(selectedGroup, filters));
×
190
    }
191
    setModifyGroupDialog(true);
×
192
    setFromFilters(true);
×
193
  };
194

195
  const onRemoveDevicesFromGroup = devices => {
2✔
196
    const isGroupRemoval = devices.length >= groupCount;
×
197
    let request;
198
    if (isGroupRemoval) {
×
199
      request = dispatch(removeStaticGroup(selectedGroup));
×
200
    } else {
201
      request = dispatch(removeDevicesFromGroup(selectedGroup, devices));
×
202
    }
203
    return request.catch(console.log);
×
204
  };
205

206
  const openSettingsDialog = e => {
2✔
207
    e.preventDefault();
×
208
    setOpenIdDialog(toggle);
×
209
  };
210

211
  const onCreateGroupClose = () => {
2✔
212
    setModifyGroupDialog(false);
×
213
    setFromFilters(false);
×
214
    setTmpDevices([]);
×
215
  };
216

217
  const onPreauthSaved = addMore => {
2✔
218
    setOpenPreauth(!addMore);
×
219
    dispatch(setDeviceListState({ page: 1, refreshTrigger: !refreshTrigger }));
×
220
  };
221

222
  const onShowDeviceStateClick = state => {
2✔
223
    dispatch(selectGroup());
×
224
    dispatch(setDeviceListState({ state }));
×
225
  };
226

227
  const onGroupSelect = groupName => {
2✔
228
    dispatch(selectGroup(groupName));
×
229
    dispatch(setDeviceListState({ page: 1, refreshTrigger: !refreshTrigger, selection: [] }));
×
230
  };
231

232
  const onShowAuthRequestDevicesClick = () => {
2✔
233
    dispatch(setDeviceFilters([]));
×
234
    dispatch(setDeviceListState({ selectedIssues: [DEVICE_ISSUE_OPTIONS.authRequests.key], page: 1 }));
×
235
  };
236

237
  const toggleGroupRemoval = () => setRemoveGroup(toggle);
2✔
238

239
  const toggleMakeGatewayClick = () => setShowMakeGateway(toggle);
2✔
240

241
  let onboardingComponent;
242
  if (deviceConnectionRef.current && !(pendingCount || acceptedCount)) {
2!
243
    const anchor = { top: deviceConnectionRef.current.offsetTop + deviceConnectionRef.current.offsetHeight / 2, left: deviceConnectionRef.current.offsetLeft };
×
244
    onboardingComponent = getOnboardingComponentFor(
×
245
      onboardingSteps.DEVICES_DELAYED_ONBOARDING,
246
      onboardingState,
247
      { anchor, place: 'left' },
248
      onboardingComponent
249
    );
250
  }
251
  return (
2✔
252
    <>
253
      <div className="tab-container with-sub-panels" style={{ paddingTop: 0, paddingBottom: 45, minHeight: 'max-content', alignContent: 'center' }}>
254
        <h3 className="flexbox center-aligned" style={{ marginBottom: 0, marginTop: 0, flexWrap: 'wrap' }}>
255
          Devices
256
        </h3>
257
        <span className="flexbox space-between margin-left-large margin-right center-aligned padding-top-small">
258
          {hasReporting && !!authRequestCount && (
2!
259
            <a className="flexbox center-aligned margin-right-large" onClick={onShowAuthRequestDevicesClick}>
260
              <AddIcon fontSize="small" style={{ marginRight: 6 }} />
261
              {authRequestCount} new device authentication {pluralize('request', authRequestCount)}
262
            </a>
263
          )}
264
          {!!pendingCount && !selectedGroup && selectedState !== DEVICE_STATES.pending ? (
6!
265
            <DeviceStatusNotification deviceCount={pendingCount} state={DEVICE_STATES.pending} onClick={onShowDeviceStateClick} />
266
          ) : (
267
            <div />
268
          )}
269
          {canManageDevices && (
4✔
270
            <DeviceAdditionWidget
271
              features={features}
272
              onConnectClick={() => dispatch(setShowConnectingDialog(true))}
×
273
              onMakeGatewayClick={toggleMakeGatewayClick}
274
              onPreauthClick={setOpenPreauth}
275
              tenantCapabilities={tenantCapabilities}
276
              innerRef={deviceConnectionRef}
277
            />
278
          )}
279
          {onboardingComponent}
280
        </span>
281
      </div>
282
      <div className="tab-container with-sub-panels" style={{ padding: 0, height: '100%' }}>
283
        <Groups
284
          className="leftFixed"
285
          acceptedCount={acceptedCount}
286
          changeGroup={onGroupSelect}
287
          groups={groupsByType}
288
          openGroupDialog={setCreateGroupExplanation}
289
          selectedGroup={selectedGroup}
290
        />
291
        <div className="rightFluid relative" style={{ paddingTop: 0 }}>
292
          {limitMaxed && <DeviceLimitWarning acceptedDevices={acceptedCount} deviceLimit={deviceLimit} />}
2!
293
          <AuthorizedDevices
294
            addDevicesToGroup={addDevicesToGroup}
295
            onGroupClick={onGroupClick}
296
            onGroupRemoval={toggleGroupRemoval}
297
            onMakeGatewayClick={toggleMakeGatewayClick}
298
            onPreauthClick={setOpenPreauth}
299
            openSettingsDialog={openSettingsDialog}
300
            removeDevicesFromGroup={onRemoveDevicesFromGroup}
301
            showsDialog={showDeviceConnectionDialog || removeGroup || modifyGroupDialog || createGroupExplanation || openIdDialog || openPreauth}
12✔
302
          />
303
        </div>
304
        {removeGroup && <RemoveGroup onClose={toggleGroupRemoval} onRemove={removeCurrentGroup} />}
2!
305
        {modifyGroupDialog && (
2!
306
          <CreateGroup
307
            addListOfDevices={createGroupFromDialog}
308
            fromFilters={fromFilters}
309
            isCreation={fromFilters || !groups.length}
×
310
            selectedDevices={tmpDevices}
311
            onClose={onCreateGroupClose}
312
          />
313
        )}
314
        {createGroupExplanation && <CreateGroupExplainer isEnterprise={isEnterprise} onClose={() => setCreateGroupExplanation(false)} />}
×
315
        {openIdDialog && (
2!
316
          <Dialog open>
317
            <DialogTitle>Default device identity attribute</DialogTitle>
318
            <DialogContent style={{ overflow: 'hidden' }}>
319
              <Global dialog closeDialog={openSettingsDialog} />
320
            </DialogContent>
321
          </Dialog>
322
        )}
323
        {openPreauth && (
2!
324
          <PreauthDialog
325
            acceptedDevices={acceptedCount}
326
            deviceLimit={deviceLimit}
327
            limitMaxed={limitMaxed}
328
            onSubmit={onPreauthSaved}
329
            onCancel={() => setOpenPreauth(false)}
×
330
          />
331
        )}
332
        {showMakeGateway && <MakeGatewayDialog isPreRelease={canPreview} onCancel={toggleMakeGatewayClick} />}
2!
333
      </div>
334
    </>
335
  );
336
};
337

338
export default DeviceGroups;
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

© 2025 Coveralls, Inc