This repository has been archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor(required): use different definition of a valid value * refactor: update compose-validators test step descriptions * refactor(email): update to case-insensitive regex and allow empty string * feat(validators): introduce alphaNumeric validator and test * refactor: extract is-empty helper from required * chore(validators): fix test step descriptions for alpha-numeric * feat(validators): introduce boolean validator and tests * chore: stumped out various validators * refactor: keep all validation helpers in a single file * chore: introduce test helper to run a validator for an array of values * feat(validators): introduce alphaNumeric and test * feat(validators): introduce boolean and test * feat(validators): introduce createCharacterLengthRange and test * feat(validators): introduce createNumberRange and test * chore: fix import to restore build * refactor(validators): improve email validation logic and tests * fix(cypress): adjust error message in e2e tests so they pass again * feat(validators): introduce create-equal-to and tests * chore(validators): fix typos in the test descriptions * chore(validators): amend text in createEqualTo * feat(validators): introduce integer and tests * feat(validators): introduce number and test * feat(validators): introduce password and test * refactor(validators): extract repeated test into helper function * feat(validators): introduce createPattern and test * feat(validators): introduce internationalPhoneNumber and test * feat(validators): introduce string and test * feat(validators): introduce url and test * feat(validators): introduce username and test * refactor(validators): validate range-fn arguments and delegate to helper * feat(validators): introduce createMinNumber and test * feat(validators): introduce createMinNumber and test * feat(validators): introduce createMaxCharacterLength and test * feat(validators): introduce createMinCharacterLength and test * test(create-character-length-range): throws an error with invalid args * fix(create-equal-to): throw error for invalid argument incl test * fix(create-pattern): throw error for invalid pattern argument incl test * chore(validators): add regex attribution to url * chore(validators): removed redundant helpers * chore(validators): export validators from `./src/validators/index.js` * chore(validators): fix typo * chore(validators): fix import paths and generate translations * fix(validators): alphaNumeric should allow spaces * fix(validators): remove check on other field being empty * fix(validators): use zero as lower bound in createMaxCharacterLength * fix(validators): rename password to dhis2Password to signify intent * fix(validators): rename username to dhis2Username to signify intent * fix(validators): replace broken imports * fix(validators): only strip leading plus signs from intl phone number * refactor(validators): rename `required` to `hasValue` * fix(validators): correct broken import Co-authored-by: Jan-Gerke Salomon <Mohammer5@users.noreply.github.com>
- Loading branch information
1 parent
80f17e1
commit 75cd2ea
Showing
53 changed files
with
1,368 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
coverage/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import '../common' | ||
import { Then } from 'cypress-cucumber-preprocessor/steps' | ||
import { requiredMessage } from '../../../../src/validators/required.js' | ||
|
||
Then('an error message is shown', () => { | ||
cy.get('.error').should('contain', 'Required') | ||
cy.get('.error').should('contain', requiredMessage) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import { Given, Then } from 'cypress-cucumber-preprocessor/steps' | ||
import { requiredMessage } from '../../../../src/validators/required.js' | ||
|
||
Given('an empty, required Input is rendered', () => { | ||
cy.visitStory('Input', 'Required') | ||
cy.visitStory('Testing:Input', 'Required') | ||
cy.verifyFormValue('agree', undefined) | ||
}) | ||
|
||
Then('an error message is shown', () => { | ||
cy.get('.error').should('contain', 'Required') | ||
cy.get('.error').should('contain', requiredMessage) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import '../common' | ||
import { Then } from 'cypress-cucumber-preprocessor/steps' | ||
import { requiredMessage } from '../../../../src/validators/required' | ||
|
||
Then('an error message is shown', () => { | ||
cy.get('.error').should('contain', 'Required') | ||
cy.get('.error').should('contain', requiredMessage) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import '../common' | ||
import { Then } from 'cypress-cucumber-preprocessor/steps' | ||
import { requiredMessage } from '../../../../src/validators/required' | ||
|
||
Then('an error message is shown', () => { | ||
cy.get('.error').should('contain', 'Required') | ||
cy.get('.error').should('contain', requiredMessage) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import '../common' | ||
import { Then } from 'cypress-cucumber-preprocessor/steps' | ||
import { requiredMessage } from '../../../../src/validators/required' | ||
|
||
Then('an error message is shown', () => { | ||
cy.get('.error').should('contain', 'Required') | ||
cy.get('.error').should('contain', requiredMessage) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import { Given, Then } from 'cypress-cucumber-preprocessor/steps' | ||
import { requiredMessage } from '../../../../src/validators/required' | ||
|
||
Given('an empty, required TextArea is rendered', () => { | ||
cy.visitStory('TextArea', 'Required') | ||
cy.verifyFormValue('agree', undefined) | ||
}) | ||
|
||
Then('an error message is shown', () => { | ||
cy.get('.error').should('contain', 'Required') | ||
cy.get('.error').should('contain', requiredMessage) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { alphaNumeric, invalidAlphaNumericMessage } from '../alphaNumeric.js' | ||
import { testValidatorValues, allowsEmptyValues } from './helpers/index.js' | ||
|
||
describe('validator: alphaNumeric', () => { | ||
allowsEmptyValues(alphaNumeric) | ||
|
||
describe('allows alpha-numeric values', () => { | ||
testValidatorValues(alphaNumeric, undefined, [ | ||
'123456', | ||
'abcdef', | ||
'a1b2c3', | ||
'A1B2C3d4e5', | ||
'I have spaces', | ||
]) | ||
}) | ||
|
||
describe('rejects non-alpha-numeric values', () => { | ||
testValidatorValues(alphaNumeric, invalidAlphaNumericMessage, [ | ||
'.,/|~', | ||
true, | ||
false, | ||
0, | ||
1, | ||
]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { boolean, invalidBooleanMessage } from '../boolean' | ||
import { testValidatorValues, allowsEmptyValues } from './helpers/index.js' | ||
|
||
describe('validator: boolean', () => { | ||
allowsEmptyValues(boolean) | ||
|
||
describe('allows boolean values', () => { | ||
testValidatorValues(boolean, undefined, [true, false]) | ||
}) | ||
|
||
describe('rejects non-boolean values', () => { | ||
testValidatorValues(boolean, invalidBooleanMessage, [ | ||
'text', | ||
3, | ||
{}, | ||
[], | ||
() => {}, | ||
]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/validators/__test__/createCharacterLengthRange.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { createCharacterLengthRange } from '../createCharacterLengthRange.js' | ||
import { testValidatorValues, allowsEmptyValues } from './helpers/index.js' | ||
import { requiredArgumentErrorMessage } from '../helpers/index.js' | ||
|
||
describe('validator: createCharacterLengthRange', () => { | ||
const betweenSixAndTenChars = createCharacterLengthRange(6, 10) | ||
const inValidMsg = 'Please enter between 6 and 10 characters' | ||
|
||
it('should throw an error when lower or upper bound are not a number', () => { | ||
expect(() => { | ||
createCharacterLengthRange(undefined, undefined) | ||
}).toThrowError(requiredArgumentErrorMessage) | ||
expect(() => { | ||
createCharacterLengthRange('test', 'test') | ||
}).toThrowError(requiredArgumentErrorMessage) | ||
expect(() => { | ||
createCharacterLengthRange(1, undefined) | ||
}).toThrowError(requiredArgumentErrorMessage) | ||
expect(() => { | ||
createCharacterLengthRange(undefined, 0) | ||
}).toThrowError(requiredArgumentErrorMessage) | ||
}) | ||
|
||
it('should create a function', () => { | ||
expect(typeof betweenSixAndTenChars).toEqual('function') | ||
}) | ||
|
||
allowsEmptyValues(betweenSixAndTenChars) | ||
|
||
describe('allows within-range strings', () => { | ||
testValidatorValues(betweenSixAndTenChars, undefined, [ | ||
'abcdef', // 6 | ||
'abcdefgh', | ||
'abcdefghij', // 10 | ||
]) | ||
}) | ||
|
||
describe('rejects non-string values', () => { | ||
testValidatorValues(betweenSixAndTenChars, inValidMsg, [ | ||
true, | ||
3, | ||
{}, | ||
[], | ||
() => {}, | ||
]) | ||
}) | ||
|
||
describe('rejects out-of-range strings', () => { | ||
testValidatorValues(betweenSixAndTenChars, inValidMsg, [ | ||
'a', | ||
'abcde', // 5 | ||
'abcdefghijk', // 11 | ||
'abcdefghijklmnopqrstuvw', | ||
]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { createEqualTo } from '../createEqualTo.js' | ||
import { allowsEmptyValues } from './helpers/index.js' | ||
import { requiredArgumentErrorMessage } from '../helpers/index.js' | ||
|
||
describe('validator: createEqualTo', () => { | ||
const equalToFoo = createEqualTo('foo') | ||
|
||
it('should throw an error when key is not a string', () => { | ||
expect(() => { | ||
createEqualTo(undefined) | ||
}).toThrowError(requiredArgumentErrorMessage) | ||
expect(() => { | ||
createEqualTo({}) | ||
}).toThrowError(requiredArgumentErrorMessage) | ||
}) | ||
|
||
it('should create a function', () => { | ||
expect(typeof equalToFoo).toEqual('function') | ||
}) | ||
|
||
allowsEmptyValues(equalToFoo) | ||
|
||
it('should return undefined when the fields have equal values', () => { | ||
const sameValue = 'abcde' | ||
|
||
expect(equalToFoo(sameValue, { foo: sameValue })).toEqual(undefined) | ||
}) | ||
|
||
it('should return an error string when the fields have inequal values', () => { | ||
const inValidFooMsg = | ||
'Please make sure the value of this input matches the value in "foo".' | ||
|
||
expect(equalToFoo('this', { foo: 'that' })).toEqual(inValidFooMsg) | ||
}) | ||
|
||
it('should use the property description in the error string if provided', () => { | ||
const equalToBar = createEqualTo('bar', 'Barista') | ||
const inValidBarMsg = | ||
'Please make sure the value of this input matches the value in "Barista".' | ||
|
||
expect(equalToBar('this', { bar: 'that' })).toEqual(inValidBarMsg) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { createMaxCharacterLength } from '../createMaxCharacterLength.js' | ||
import { testValidatorValues } from './helpers/index.js' | ||
|
||
describe('validator: createMaxCharacterLength', () => { | ||
const maxSixChars = createMaxCharacterLength(6) | ||
const errorMessage = 'Please enter a maximum of 6 characters' | ||
|
||
/* | ||
* Since createMaxCharacterLength calls createNumberRange internally | ||
* a lot of things have been tested there and here we focus | ||
* purely on the bounderies | ||
*/ | ||
|
||
describe('allows strings with a lower or equal length than the upper bound', () => { | ||
testValidatorValues(maxSixChars, undefined, ['a', '123456']) | ||
}) | ||
|
||
describe('rejects strings a length above the upper bound', () => { | ||
testValidatorValues(maxSixChars, errorMessage, [ | ||
'1234567', | ||
'some even longer text here....', | ||
]) | ||
}) | ||
}) |
Oops, something went wrong.