Skip to content

Commit

Permalink
feat: Prohibits putting only spaces in a document name
Browse files Browse the repository at this point in the history
it's no longer possible to validate the step
if there is only spaces in the document name.

Tests are juste refactored to deal with new isError prop.
No code change in them.
We don't test the isError case here. See next commit.
  • Loading branch information
JF-Cozy committed Sep 21, 2022
1 parent 58f0c26 commit 5568408
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,22 @@ const InputTextAdapter = ({
const [currentValue, setCurrentValue] = useState(defaultValue || '')
const [isTooShort, setIsTooShort] = useState(false)
const [hasOnlySpaces, setHasOnlySpaces] = useState(false)
const isError = isTooShort || hasOnlySpaces

const { inputType, expectedLength, isRequired } = useMemo(
() => makeConstraintsOfInput(attrs),
[attrs]
)

const isValidInputValue = useMemo(
() =>
checkConstraintsOfIinput(currentValue.length, expectedLength, isRequired),
[currentValue.length, expectedLength, isRequired]
checkConstraintsOfIinput({
valueLength: currentValue.length,
expectedLength,
isRequired,
isError
}),
[currentValue.length, expectedLength, isRequired, isError]
)

useEffect(() => {
Expand Down Expand Up @@ -131,7 +138,7 @@ const InputTextAdapter = ({
type="text"
variant="outlined"
label={inputLabel ? t(inputLabel) : ''}
error={isTooShort || hasOnlySpaces}
error={isError}
helperText={helperText}
inputProps={{
style: {
Expand Down Expand Up @@ -160,7 +167,7 @@ const InputTextAdapter = ({
type="text"
variant="outlined"
label={inputLabel ? t(inputLabel) : ''}
error={isTooShort || hasOnlySpaces}
error={isError}
helperText={helperText}
value={currentValue}
inputProps={{
Expand Down
8 changes: 5 additions & 3 deletions packages/cozy-mespapiers-lib/src/utils/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,16 @@ export const makeConstraintsOfInput = attrs => {
* @param {boolean} isRequired - If value is required
* @returns {boolean}
*/
export const checkConstraintsOfIinput = (
export const checkConstraintsOfIinput = ({
valueLength,
expectedLength,
isRequired
) => {
isRequired,
isError
}) => {
const { min, max } = expectedLength

return [
!isError,
valueLength !== 0 || !isRequired,
valueLength === 0 || min == null || valueLength >= min,
valueLength === 0 || max == null || valueLength <= max
Expand Down
71 changes: 38 additions & 33 deletions packages/cozy-mespapiers-lib/src/utils/input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,42 +43,47 @@ describe('Input Utils', () => {

describe('checkInputConstraints', () => {
it.each`
valueLength | expectedLength | isRequired | result
${0} | ${{ min: null, max: null }} | ${false} | ${true}
${0} | ${{ min: null, max: null }} | ${true} | ${false}
${0} | ${{ min: null, max: 0 }} | ${false} | ${true}
${0} | ${{ min: null, max: 0 }} | ${true} | ${false}
${0} | ${{ min: 0, max: null }} | ${false} | ${true}
${0} | ${{ min: 0, max: null }} | ${true} | ${false}
${0} | ${{ min: 0, max: 0 }} | ${false} | ${true}
${0} | ${{ min: 0, max: 0 }} | ${true} | ${false}
${0} | ${{ min: null, max: 20 }} | ${false} | ${true}
${0} | ${{ min: null, max: 20 }} | ${true} | ${false}
${0} | ${{ min: 20, max: null }} | ${false} | ${true}
${0} | ${{ min: 20, max: null }} | ${true} | ${false}
${0} | ${{ min: 10, max: 30 }} | ${false} | ${true}
${0} | ${{ min: 30, max: 10 }} | ${false} | ${true}
${0} | ${{ min: 10, max: 30 }} | ${true} | ${false}
${0} | ${{ min: 30, max: 10 }} | ${true} | ${false}
${10} | ${{ min: 10, max: 30 }} | ${true} | ${true}
${10} | ${{ min: 30, max: 10 }} | ${true} | ${false}
${20} | ${{ min: 10, max: 30 }} | ${true} | ${true}
${20} | ${{ min: 30, max: 10 }} | ${true} | ${false}
${30} | ${{ min: 10, max: 30 }} | ${true} | ${true}
${30} | ${{ min: 30, max: 10 }} | ${true} | ${false}
${40} | ${{ min: 10, max: 30 }} | ${true} | ${false}
${40} | ${{ min: 30, max: 10 }} | ${true} | ${false}
${20} | ${{ min: null, max: 10 }} | ${false} | ${false}
${20} | ${{ min: null, max: 20 }} | ${false} | ${true}
${20} | ${{ min: null, max: 30 }} | ${false} | ${true}
${20} | ${{ min: 10, max: null }} | ${false} | ${true}
${20} | ${{ min: 20, max: null }} | ${false} | ${true}
${20} | ${{ min: 30, max: null }} | ${false} | ${false}
valueLength | expectedLength | isRequired | isError | result
${0} | ${{ min: null, max: null }} | ${false} | ${false} | ${true}
${0} | ${{ min: null, max: null }} | ${true} | ${false} | ${false}
${0} | ${{ min: null, max: 0 }} | ${false} | ${false} | ${true}
${0} | ${{ min: null, max: 0 }} | ${true} | ${false} | ${false}
${0} | ${{ min: 0, max: null }} | ${false} | ${false} | ${true}
${0} | ${{ min: 0, max: null }} | ${true} | ${false} | ${false}
${0} | ${{ min: 0, max: 0 }} | ${false} | ${false} | ${true}
${0} | ${{ min: 0, max: 0 }} | ${true} | ${false} | ${false}
${0} | ${{ min: null, max: 20 }} | ${false} | ${false} | ${true}
${0} | ${{ min: null, max: 20 }} | ${true} | ${false} | ${false}
${0} | ${{ min: 20, max: null }} | ${false} | ${false} | ${true}
${0} | ${{ min: 20, max: null }} | ${true} | ${false} | ${false}
${0} | ${{ min: 10, max: 30 }} | ${false} | ${false} | ${true}
${0} | ${{ min: 30, max: 10 }} | ${false} | ${false} | ${true}
${0} | ${{ min: 10, max: 30 }} | ${true} | ${false} | ${false}
${0} | ${{ min: 30, max: 10 }} | ${true} | ${false} | ${false}
${10} | ${{ min: 10, max: 30 }} | ${true} | ${false} | ${true}
${10} | ${{ min: 30, max: 10 }} | ${true} | ${false} | ${false}
${20} | ${{ min: 10, max: 30 }} | ${true} | ${false} | ${true}
${20} | ${{ min: 30, max: 10 }} | ${true} | ${false} | ${false}
${30} | ${{ min: 10, max: 30 }} | ${true} | ${false} | ${true}
${30} | ${{ min: 30, max: 10 }} | ${true} | ${false} | ${false}
${40} | ${{ min: 10, max: 30 }} | ${true} | ${false} | ${false}
${40} | ${{ min: 30, max: 10 }} | ${true} | ${false} | ${false}
${20} | ${{ min: null, max: 10 }} | ${false} | ${false} | ${false}
${20} | ${{ min: null, max: 20 }} | ${false} | ${false} | ${true}
${20} | ${{ min: null, max: 30 }} | ${false} | ${false} | ${true}
${20} | ${{ min: 10, max: null }} | ${false} | ${false} | ${true}
${20} | ${{ min: 20, max: null }} | ${false} | ${false} | ${true}
${20} | ${{ min: 30, max: null }} | ${false} | ${false} | ${false}
`(
`should return $result when passed argument: ($valueLength, $expectedLength, $isRequired)`,
({ valueLength, expectedLength, isRequired, result }) => {
({ valueLength, expectedLength, isRequired, isError, result }) => {
expect(
checkConstraintsOfIinput(valueLength, expectedLength, isRequired)
checkConstraintsOfIinput({
valueLength,
expectedLength,
isRequired,
isError
})
).toEqual(result)
}
)
Expand Down

0 comments on commit 5568408

Please sign in to comment.