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

mendersoftware / gui / 1057181131

01 Nov 2023 04:10AM UTC coverage: 82.824% (-17.1%) from 99.964%
1057181131

Pull #4125

gitlab-ci

web-flow
chore: Bump axios from 1.5.1 to 1.6.0 in /tests/e2e_tests

Bumps [axios](https://github.com/axios/axios) from 1.5.1 to 1.6.0.
- [Release notes](https://github.com/axios/axios/releases)
- [Changelog](https://github.com/axios/axios/blob/v1.x/CHANGELOG.md)
- [Commits](https://github.com/axios/axios/compare/v1.5.1...v1.6.0)

---
updated-dependencies:
- dependency-name: axios
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #4125: chore: Bump axios from 1.5.1 to 1.6.0 in /tests/e2e_tests

4349 of 6284 branches covered (0.0%)

8313 of 10037 relevant lines covered (82.82%)

202.07 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: '' };
18✔
21

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

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

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

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

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

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

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

80
  return (
241✔
81
    <div>
82
      {inputs.map((input, index) => {
83
        const hasError = Boolean(index === inputs.length - 1 && (errortext || error));
273✔
84
        const hasRemovalDisabled = !(inputs[index].key && inputs[index].value);
273✔
85
        const Helptip = inputs[index].helptip?.component;
273✔
86
        return (
273✔
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" />
78✔
90
              {hasError && <FormHelperText>{errortext || error}</FormHelperText>}
341✔
91
            </FormControl>
92
            <FormControl>
93
              <Input disabled={disabled} value={`${input.value}`} placeholder="Value" onChange={e => updateInputs('value', index, e)} type="text" />
112✔
94
            </FormControl>
95
            {inputs.length > 1 && !hasRemovalDisabled ? (
610✔
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} />}
274✔
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}
661✔
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 ? (
241✔
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