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

mendersoftware / gui / 1350829378

27 Jun 2024 01:46PM UTC coverage: 83.494% (-16.5%) from 99.965%
1350829378

Pull #4465

gitlab-ci

mzedel
chore: test fixes

Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #4465: MEN-7169 - feat: added multi sorting capabilities to devices view

4506 of 6430 branches covered (70.08%)

81 of 100 new or added lines in 14 files covered. (81.0%)

1661 existing lines in 163 files now uncovered.

8574 of 10269 relevant lines covered (83.49%)

160.6 hits per line

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

75.0
/src/js/components/common/forms/keyvalueeditor.js
1
// Copyright 2021 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, { createRef, useEffect, useState } from 'react';
15

16
import { Clear as ClearIcon, Add as ContentAddIcon } from '@mui/icons-material';
17
import { Fab, FormControl, FormHelperText, IconButton, Input } from '@mui/material';
18
import { useTheme } from '@mui/material/styles';
19

20
const emptyInput = { helptip: null, key: '', value: '' };
17✔
21

22
export const KeyValueEditor = ({ disabled, errortext, initialInput = {}, inputHelpTipsMap = {}, onInputChange, reset }) => {
17✔
23
  const theme = useTheme();
178✔
24
  const [inputs, setInputs] = useState([{ ...emptyInput }]);
178✔
25
  const [error, setError] = useState('');
178✔
26

27
  useEffect(() => {
178✔
28
    const newInputs = Object.keys(initialInput).length
13!
UNCOV
29
      ? Object.entries(initialInput).map(([key, value]) => ({ helptip: inputHelpTipsMap[key.toLowerCase()], key, ref: createRef(), value }))
×
30
      : [{ ...emptyInput, ref: createRef() }];
31
    setInputs(newInputs);
13✔
32
    // eslint-disable-next-line react-hooks/exhaustive-deps
33
  }, [JSON.stringify(initialInput), JSON.stringify(inputHelpTipsMap), reset]);
34

35
  const onClearClick = () => {
178✔
UNCOV
36
    const changedInputs = [{ ...emptyInput }];
×
UNCOV
37
    setInputs(changedInputs);
×
UNCOV
38
    const inputObject = reducePairs(changedInputs);
×
UNCOV
39
    onInputChange(inputObject);
×
40
  };
41

42
  const updateInputs = (key, index, event) => {
178✔
43
    let changedInputs = [...inputs];
142✔
44
    const {
45
      target: { value }
46
    } = event;
142✔
47
    changedInputs[index][key] = value;
142✔
48
    changedInputs[index].helptip = null;
142✔
49
    const normalizedKey = changedInputs[index].key.toLowerCase();
142✔
50
    if (inputHelpTipsMap[normalizedKey]) {
142✔
51
      changedInputs[index].helptip = inputHelpTipsMap[normalizedKey];
1✔
52
    }
53
    setInputs(changedInputs);
142✔
54
    const inputObject = reducePairs(changedInputs);
142✔
55
    if (changedInputs.every(item => item.key && item.value) && changedInputs.length !== Object.keys(inputObject).length) {
170✔
56
      setError('Duplicate keys exist, only the last set value will be submitted');
10✔
57
    } else {
58
      setError('');
132✔
59
    }
60
    onInputChange(inputObject);
142✔
61
  };
62

63
  const reducePairs = listOfPairs => listOfPairs.reduce((accu, item) => ({ ...accu, ...(item.value ? { [item.key]: item.value } : {}) }), {});
178✔
64

65
  const addKeyValue = () => {
178✔
66
    const changedInputs = [...inputs, { ...emptyInput, ref: createRef() }];
3✔
67
    setInputs(changedInputs);
3✔
68
    setError('');
3✔
69
  };
70

71
  const removeInput = index => {
178✔
UNCOV
72
    let changedInputs = [...inputs];
×
UNCOV
73
    changedInputs.splice(index, 1);
×
UNCOV
74
    setInputs(changedInputs);
×
UNCOV
75
    const inputObject = reducePairs(changedInputs);
×
UNCOV
76
    onInputChange(inputObject);
×
UNCOV
77
    setError('');
×
78
  };
79

80
  return (
178✔
81
    <div>
82
      {inputs.map((input, index) => {
83
        const hasError = Boolean(index === inputs.length - 1 && (errortext || error));
211✔
84
        const hasRemovalDisabled = !(inputs[index].key && inputs[index].value);
211✔
85
        const Helptip = inputs[index].helptip?.component;
211✔
86
        return (
211✔
87
          <div className="key-value-container relative" key={index}>
88
            <FormControl>
89
              <Input disabled={disabled} value={input.key} placeholder="Key" onChange={e => updateInputs('key', index, e)} type="text" />
57✔
90
              {hasError && <FormHelperText>{errortext || error}</FormHelperText>}
279✔
91
            </FormControl>
92
            <FormControl>
93
              <Input disabled={disabled} value={`${input.value}`} placeholder="Value" onChange={e => updateInputs('value', index, e)} type="text" />
85✔
94
            </FormControl>
95
            {inputs.length > 1 && !hasRemovalDisabled ? (
488✔
UNCOV
96
              <IconButton disabled={disabled} onClick={() => removeInput(index)} size="large">
×
97
                <ClearIcon fontSize="small" />
98
              </IconButton>
99
            ) : (
100
              <span />
101
            )}
102
            {Helptip && <Helptip anchor={{ left: -35, top: 5 }} {...inputs[index].helptip.props} />}
212✔
103
          </div>
104
        );
105
      })}
106
      <div className="key-value-container">
107
        <div style={{ minWidth: theme.spacing(30) }}>
108
          <Fab
109
            disabled={disabled || !inputs[inputs.length - 1].key || !inputs[inputs.length - 1].value}
488✔
110
            style={{ marginBottom: 10 }}
111
            color="secondary"
112
            size="small"
113
            onClick={addKeyValue}
114
          >
115
            <ContentAddIcon />
116
          </Fab>
117
        </div>
118
        <div style={{ minWidth: theme.spacing(30) }} />
119
        {inputs.length > 1 ? (
178✔
120
          <a className="margin-left-small" onClick={onClearClick}>
121
            clear all
122
          </a>
123
        ) : (
124
          <div />
125
        )}
126
      </div>
127
    </div>
128
  );
129
};
130

131
export default KeyValueEditor;
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