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

mendersoftware / mender-server / 1593965839

18 Dec 2024 10:58AM UTC coverage: 73.514% (+0.7%) from 72.829%
1593965839

Pull #253

gitlab-ci

mineralsfree
chore(gui): aligned tests with edit billing profile

Ticket: MEN-7466
Changelog: None

Signed-off-by: Mikita Pilinka <mikita.pilinka@northern.tech>
Pull Request #253: MEN-7466-feat: updated billing section in My Organization settings

4257 of 6185 branches covered (68.83%)

Branch coverage included in aggregate %.

53 of 87 new or added lines in 11 files covered. (60.92%)

43 existing lines in 11 files now uncovered.

40083 of 54130 relevant lines covered (74.05%)

22.98 hits per line

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

70.75
/frontend/src/js/components/devices/widgets/attribute-autocomplete.js
1
// Copyright 2022 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

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

19
import { TIMEOUTS, emptyFilter } from '@northern.tech/store/constants';
20

21
import { defaultHeaders } from '../base-devices';
22
import { getFilterLabelByKey } from './filters';
23

24
const textFieldStyle = { marginTop: 0, marginBottom: 15 };
11✔
25

26
export const getOptionLabel = option => {
11✔
27
  const header = Object.values(defaultHeaders).find(
119✔
28
    ({ attribute }) => attribute.scope === option.scope && (attribute.name === option.key || attribute.alternative === option.key)
706✔
29
  );
30
  return header?.title || option.title || option.value || option.key || option;
119✔
31
};
32

33
const FilterOption = (props, option) => {
11✔
34
  let content = getOptionLabel(option);
20✔
35
  if (option.category === 'recently used') {
20!
36
    content = (
×
37
      <div className="flexbox center-aligned space-between" style={{ width: '100%' }}>
38
        <div>{content}</div>
39
        <div className="muted slightly-smaller">({option.scope})</div>
40
      </div>
41
    );
42
  }
43
  return <li {...props}>{content}</li>;
20✔
44
};
45

46
const optionsFilter = createFilterOptions();
11✔
47

48
const filterOptions = (options, params) => {
11✔
49
  const filtered = optionsFilter(options, params);
9✔
50
  if (filtered.length !== 1 && params.inputValue !== '') {
9✔
51
    filtered.push({
6✔
52
      inputValue: params.inputValue,
53
      key: 'custom',
54
      value: `Use "${params.inputValue}"`,
55
      category: 'custom',
56
      priority: 99
57
    });
58
  }
59
  return filtered;
9✔
60
};
61

62
export const AttributeAutoComplete = ({ attributes, disabled = false, filter = emptyFilter, label = 'Attribute', onRemove, onSelect, ...remainder }) => {
11!
63
  const [key, setKey] = useState(filter.key); // this refers to the selected filter with key as the id
17✔
64
  const [options, setOptions] = useState([]);
17✔
65
  const [reset, setReset] = useState(true);
17✔
66
  const [scope, setScope] = useState(filter.scope);
17✔
67
  const timer = useRef();
17✔
68

69
  useEffect(() => {
17✔
70
    return () => {
4✔
71
      clearTimeout(timer.current);
4✔
72
    };
73
  }, []);
74

75
  useEffect(() => {
17✔
76
    setKey(emptyFilter.key);
6✔
77
    setScope(emptyFilter.scope);
6✔
78
    let attributesClean = attributes.map(attr => {
6✔
79
      if (!attr.category && attr.scope) {
16!
UNCOV
80
        attr.category = attr.scope;
×
81
      }
82
      return attr;
16✔
83
    });
84
    setOptions(
6✔
85
      attributesClean.sort((a, b) =>
86
        a.category == b.category
26✔
87
          ? a.priority == b.priority
6!
88
            ? (a.key || '').localeCompare(b.key || '', { sensitivity: 'case' })
12!
89
            : a.priority - b.priority
90
          : (a.category || '').localeCompare(b.category || '', { sensitivity: 'case' })
40!
91
      )
92
    );
93
    // eslint-disable-next-line react-hooks/exhaustive-deps
94
  }, [attributes.length, reset]);
95

96
  useEffect(() => {
17✔
97
    setKey(filter.key);
4✔
98
  }, [filter.key]);
99

100
  useEffect(() => {
17✔
101
    clearTimeout(timer.current);
8✔
102
    timer.current = setTimeout(() => onSelect({ key, scope }), TIMEOUTS.debounceDefault);
8✔
103
    return () => {
8✔
104
      clearTimeout(timer.current);
8✔
105
    };
106
  }, [key, onSelect, scope]);
107

108
  const updateFilterKey = (value, selectedScope) => {
17✔
UNCOV
109
    if (!value) {
×
UNCOV
110
      return removeFilter();
×
111
    }
UNCOV
112
    const { key = value, scope: fallbackScope } = attributes.find(filter => filter.key === value) ?? {};
×
113
    setKey(key);
×
114
    setScope(selectedScope || fallbackScope);
×
115
  };
116

117
  const removeFilter = useCallback(() => {
17✔
118
    if (key) {
×
UNCOV
119
      onRemove({ key, scope });
×
120
    }
UNCOV
121
    setReset(!reset);
×
122
  }, [key, onRemove, reset, setReset, scope]);
123

124
  return (
17✔
125
    <Autocomplete
126
      {...remainder}
127
      autoComplete
128
      autoHighlight
129
      autoSelect
130
      disabled={disabled}
131
      freeSolo
132
      filterSelectedOptions
133
      filterOptions={filterOptions}
134
      getOptionLabel={getOptionLabel}
135
      groupBy={option => option.category}
20✔
136
      renderOption={FilterOption}
137
      id="filter-selection"
138
      includeInputInList={true}
139
      onChange={(e, changedValue) => {
140
        const { inputValue, key = changedValue, scope } = changedValue || {};
1!
141
        if (inputValue) {
1!
142
          // only circumvent updateFilterKey if we deal with a custom attribute - those will be treated as inventory attributes
143
          setKey(inputValue);
1✔
144
          return setScope(emptyFilter.scope);
1✔
145
        }
UNCOV
146
        updateFilterKey(key, scope);
×
147
      }}
148
      options={options}
149
      renderInput={params => <TextField {...params} label={label} style={textFieldStyle} />}
31✔
150
      key={reset}
151
      value={getFilterLabelByKey(key, attributes)}
152
    />
153
  );
154
};
155

156
export default AttributeAutoComplete;
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