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

mendersoftware / gui / 951400782

pending completion
951400782

Pull #3900

gitlab-ci

web-flow
chore: bump @testing-library/jest-dom from 5.16.5 to 5.17.0

Bumps [@testing-library/jest-dom](https://github.com/testing-library/jest-dom) from 5.16.5 to 5.17.0.
- [Release notes](https://github.com/testing-library/jest-dom/releases)
- [Changelog](https://github.com/testing-library/jest-dom/blob/main/CHANGELOG.md)
- [Commits](https://github.com/testing-library/jest-dom/compare/v5.16.5...v5.17.0)

---
updated-dependencies:
- dependency-name: "@testing-library/jest-dom"
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #3900: chore: bump @testing-library/jest-dom from 5.16.5 to 5.17.0

4446 of 6414 branches covered (69.32%)

8342 of 10084 relevant lines covered (82.73%)

186.0 hits per line

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

82.98
/src/js/components/common/forms/form.js
1
// Copyright 2016 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 from 'react';
15
import { FormProvider, useForm } from 'react-hook-form';
16

17
import { Button } from '@mui/material';
18

19
import validator from 'validator';
20

21
const getErrorMsg = (validateMethod, args) => {
17✔
22
  switch (validateMethod) {
245!
23
    case 'isLength':
24
      if (args[0] === 1) {
51!
25
        return 'This field is required';
×
26
      } else if (args[0] > 1) {
51!
27
        return `Must be at least ${args[0]} characters long`;
51✔
28
      }
29
      break;
×
30
    case 'isAlpha':
31
      return 'This field must contain only letters';
×
32
    case 'isAlphanumeric':
33
      return 'This field must contain only letters or numbers';
×
34
    case 'isNumeric':
35
      return 'Please enter a valid code';
×
36
    case 'isEmail':
37
      return 'Please enter a valid email address';
162✔
38
    case 'isNot':
39
      if (args[0] === args[1]) {
32!
40
        return `This field should have a value other than ${args[0]}`;
×
41
      }
42
      break;
32✔
43
    default:
44
      return 'There is an error with this field';
×
45
  }
46
};
47

48
const tryApplyValidations = (value, validations, initialValidationResult) =>
17✔
49
  validations.split(',').reduce((accu, validation) => {
629✔
50
    if (!accu.isValid || !validation) {
995✔
51
      return accu;
171✔
52
    }
53
    var args = validation.split(':');
824✔
54
    var validateMethod = args.shift();
824✔
55
    // We use JSON.parse to convert the string values passed to the
56
    // correct type. Ex. 'isLength:1' will make '1' actually a number
57
    args = args.map(arg => JSON.parse(JSON.stringify(arg)));
824✔
58

59
    var tmpArgs = args;
824✔
60
    // We then merge two arrays, ending up with the value
61
    // to pass first, then options, if any. ['valueFromInput', 5]
62
    args = [value].concat(args);
824✔
63
    try {
824✔
64
      // So the next line of code is actually:
65
      // validator.isLength('valueFromInput', 5)
66
      if (!validator[validateMethod].apply(validator, args)) {
824✔
67
        return { errortext: getErrorMsg(validateMethod, tmpArgs), isValid: false };
213✔
68
      }
69
    } catch {
70
      const errortext = getErrorMsg(validateMethod, args) || '';
32✔
71
      return { errortext, isValid: !errortext };
32✔
72
    }
73
    return accu;
579✔
74
  }, initialValidationResult);
75

76
const runPasswordValidations = ({ required, value, validations, isValid, errortext }) => {
17✔
77
  if (required && !value) {
308!
78
    return { isValid: false, errortext: 'Password is required' };
×
79
  } else if (required || value) {
308✔
80
    isValid = tryApplyValidations(value, validations, { isValid, errortext }).isValid;
299✔
81
    return { isValid, errortext: !isValid ? 'Password too weak' : errortext };
299✔
82
  }
83
  return { isValid, errortext };
9✔
84
};
85

86
export const runValidations = ({ required, value, id, validations }) => {
17✔
87
  let isValid = true;
709✔
88
  let errortext = '';
709✔
89
  if (id && id.includes('password')) {
709✔
90
    return runPasswordValidations({ required, value, validations, isValid, errortext });
308✔
91
  } else {
92
    if (value || required) {
401✔
93
      return tryApplyValidations(value, validations, { isValid, errortext });
330✔
94
    }
95
  }
96
  return { isValid, errortext };
71✔
97
};
98

99
export const Form = ({
17✔
100
  autocomplete,
101
  buttonColor,
102
  children,
103
  className = '',
344✔
104
  defaultValues = {},
696✔
105
  handleCancel,
106
  id,
107
  onSubmit,
108
  showButtons,
109
  submitButtonId,
110
  submitLabel
111
}) => {
112
  const methods = useForm({ mode: 'onChange', defaultValues });
811✔
113
  const {
114
    handleSubmit,
115
    formState: { isValid }
116
  } = methods;
811✔
117
  return (
811✔
118
    <FormProvider {...methods}>
119
      <form autoComplete={autocomplete} className={className} id={id} noValidate onSubmit={handleSubmit(onSubmit)}>
120
        {children}
121
        {!!showButtons && (
1,541✔
122
          <div className="flexbox" style={{ justifyContent: 'flex-end', height: 'min-content', marginTop: 32 }}>
123
            {!!handleCancel && (
793✔
124
              <Button key="cancel" onClick={handleCancel} style={{ marginRight: 10, display: 'inline-block' }}>
125
                Cancel
126
              </Button>
127
            )}
128
            <Button variant="contained" type="submit" disabled={!isValid} id={submitButtonId} color={buttonColor}>
129
              {submitLabel}
130
            </Button>
131
          </div>
132
        )}
133
      </form>
134
    </FormProvider>
135
  );
136
};
137

138
export default Form;
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