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

mendersoftware / gui / 901187442

pending completion
901187442

Pull #3795

gitlab-ci

mzedel
feat: increased chances of adopting our intended navigation patterns instead of unsupported browser navigation

Ticket: None
Changelog: None
Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #3795: feat: increased chances of adopting our intended navigation patterns instead of unsupported browser navigation

4389 of 6365 branches covered (68.96%)

5 of 5 new or added lines in 1 file covered. (100.0%)

1729 existing lines in 165 files now uncovered.

8274 of 10019 relevant lines covered (82.58%)

144.86 hits per line

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

58.97
/src/js/components/devices/group-management/group-definition.js
1
// Copyright 2020 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, { useState } from 'react';
15

16
import { Autocomplete, FormHelperText, TextField } from '@mui/material';
17
import { createFilterOptions } from '@mui/material/useAutocomplete';
18

19
import validator from 'validator';
20

21
import { UNGROUPED_GROUP } from '../../../constants/deviceConstants';
22
import { fullyDecodeURI } from '../../../helpers';
23
import InfoText from '../../common/infotext';
24

25
const filter = createFilterOptions();
6✔
26

27
export const validateGroupName = (encodedName, groups = [], selectedDevices = [], isCreationDynamic) => {
6!
28
  const name = fullyDecodeURI(encodedName);
5✔
29
  let invalid = false;
5✔
30
  let errortext = null;
5✔
31
  const isModification = name.length && groups.some(group => decodeURIComponent(group) === name);
5✔
32
  if (!name && !isModification) {
5✔
33
    invalid = true;
2✔
34
  } else if (!validator.isWhitelisted(name, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-')) {
3✔
35
    invalid = true;
1✔
36
    errortext = 'Valid characters are a-z, A-Z, 0-9, _ and -';
1✔
37
  } else if (selectedDevices.length && selectedDevices.every(({ group }) => group === name)) {
2✔
38
    invalid = true;
1✔
39
    errortext = `${name} is the same group the selected devices are already in`;
1✔
40
  } else if (isModification && isCreationDynamic) {
1!
41
    invalid = true;
1✔
42
    errortext = 'A group with the same name already exists';
1✔
UNCOV
43
  } else if (name === UNGROUPED_GROUP.name) {
×
UNCOV
44
    invalid = true;
×
UNCOV
45
    errortext = `A group with the name ${name} is created automatically`;
×
46
  }
47
  return { errortext, invalid, isModification, name };
5✔
48
};
49

50
const GroupOption = (props, option) => <li {...props}>{option.title}</li>;
6✔
51

52
export const GroupDefinition = ({ docsVersion = '', isCreationDynamic, groups, newGroup, onInputChange, selectedDevices, selectedGroup }) => {
6✔
53
  const [errortext, setErrorText] = useState('');
2✔
54

55
  const validateName = encodedName => {
2✔
UNCOV
56
    const { errortext: error, invalid, isModification, name } = validateGroupName(encodedName, groups, selectedDevices, isCreationDynamic);
×
UNCOV
57
    setErrorText(error);
×
UNCOV
58
    onInputChange(invalid, name, isModification);
×
59
  };
60

61
  const filteredGroups = groups
2✔
62
    .filter(group => group !== selectedGroup)
1✔
63
    .map(group => ({
1✔
64
      value: group,
65
      title: group
66
    }));
67
  return (
2✔
68
    <>
69
      <Autocomplete
70
        id="group-creation-selection"
71
        autoSelect
72
        freeSolo
73
        filterSelectedOptions
74
        filterOptions={(options, params) => {
UNCOV
75
          const filtered = filter(options, params);
×
UNCOV
76
          if (
×
77
            params.inputValue !== '' &&
×
UNCOV
78
            !groups.some(group => decodeURIComponent(group) === params.inputValue) &&
×
79
            (filtered.length !== 1 || (filtered.length === 1 && filtered[0].title !== params.inputValue))
80
          ) {
UNCOV
81
            filtered.push({
×
82
              inputValue: params.inputValue,
83
              title: `Create "${params.inputValue}" group`
84
            });
85
          }
UNCOV
86
          return filtered;
×
87
        }}
88
        getOptionLabel={option => {
UNCOV
89
          if (typeof option === 'string') {
×
UNCOV
90
            return option;
×
91
          }
UNCOV
92
          if (option.inputValue) {
×
UNCOV
93
            return option.inputValue;
×
94
          }
UNCOV
95
          return option.title;
×
96
        }}
97
        handleHomeEndKeys
98
        inputValue={newGroup}
99
        options={filteredGroups}
UNCOV
100
        onInputChange={(e, newValue) => validateName(newValue)}
×
101
        renderInput={params => <TextField {...params} label="Select a group, or type to create new" InputProps={{ ...params.InputProps }} />}
4✔
102
        renderOption={GroupOption}
103
      />
104
      <FormHelperText>{errortext}</FormHelperText>
105
      {isCreationDynamic && (
3✔
106
        <InfoText>
107
          Note: individual devices can&apos;t be added to dynamic groups.
108
          <br />
109
          <a href={`https://docs.mender.io/${docsVersion}overview/device-group`} target="_blank" rel="noopener noreferrer">
110
            Learn more about static vs. dynamic groups
111
          </a>
112
        </InfoText>
113
      )}
114
    </>
115
  );
116
};
117

118
export default GroupDefinition;
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