-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from browserstack/autocomplete-test-cases
Added unit test cases for autocomplete
- Loading branch information
Showing
5 changed files
with
369 additions
and
166 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 |
---|---|---|
@@ -1,55 +1,77 @@ | ||
import { isValidAutocomplete } from "../../commons/text"; | ||
import { isValidAutocomplete } from '../../commons/text'; | ||
import { ErrorHandler } from '../../../../a11y-engine-core/lib/core/errors/error-handler'; | ||
|
||
function checkIsElementValidAutocomplete(node, options, virtualNode) { | ||
const autocomplete = virtualNode.attr('autocomplete')?.toLowerCase().trim(); | ||
// if element has autocomplete attribute as off then it is not a violation | ||
if(autocomplete === "off" || autocomplete==="chrome-off") { | ||
return true; | ||
} | ||
|
||
// if it is on then we check whether name / id have valid autocomplete value or not | ||
// same for the case if autocomplete is not present or has a non-standard value | ||
if(!autocomplete || autocomplete === "on" || !isValidAutocomplete(autocomplete, options)) { | ||
const name = virtualNode.attr('name'); | ||
const id = virtualNode.attr('id'); | ||
if((name && isValidAutocomplete(name, options)) || (id && isValidAutocomplete(id, options))) | ||
return true; | ||
return false; | ||
} | ||
|
||
// if element autocomplete attribute is neither off nor on then we check if its a standard value | ||
if(isValidAutocomplete(autocomplete, options)) { | ||
const autocomplete = virtualNode | ||
.attr('autocomplete') | ||
?.toLowerCase() | ||
.trim(); | ||
// if element has autocomplete attribute as off then it is not a violation | ||
if (autocomplete === 'off' || autocomplete === 'chrome-off') { | ||
return true; | ||
} | ||
|
||
// if it is on then we check whether name / id have valid autocomplete value or not | ||
// same for the case if autocomplete is not present or has a non-standard value | ||
if ( | ||
!autocomplete || | ||
autocomplete === 'on' || | ||
!isValidAutocomplete(autocomplete, options) | ||
) { | ||
const name = virtualNode.attr('name'); | ||
const id = virtualNode.attr('id'); | ||
if ( | ||
(name && isValidAutocomplete(name, options)) || | ||
(id && isValidAutocomplete(id, options)) | ||
) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function autocompleteA11yEvaluate(node, options, virtualNode) { | ||
try { | ||
const autocomplete = virtualNode.attr('autocomplete'); | ||
|
||
// check if the autocomplete applicable element is inside form or exist freely | ||
const closestForm = virtualNode.actualNode.closest("form"); | ||
|
||
//if it exists inside the form and autocomplete for form is off | ||
if(closestForm && (closestForm.getAttribute('autocomplete')?.toLowerCase().trim() === "off" | ||
|| closestForm.getAttribute('autocomplete')?.toLowerCase().trim() === "chrome-off")) { | ||
// if autocomplete attribute is not present for element then its a pass in this scenario | ||
// otherwise check all posibilities with the method | ||
return autocomplete ? checkIsElementValidAutocomplete(node, options, virtualNode) : true; | ||
} else { | ||
// The else case is if form is present and it has autocomplete as on or not set and | ||
// the other case this handles is that element exists independently | ||
|
||
// this method would check for all posibilities | ||
return checkIsElementValidAutocomplete(node, options, virtualNode); | ||
} | ||
} | ||
catch(err) { | ||
ErrorHandler.addCheckError("autocomplete-attribute-valid-check", err); | ||
return undefined; | ||
|
||
// if element autocomplete attribute is neither off nor on then we check if its a standard value | ||
if (isValidAutocomplete(autocomplete, options)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function autocompleteA11yEvaluate(node, options, virtualNode) { | ||
try { | ||
const autocomplete = virtualNode.attr('autocomplete'); | ||
|
||
// check if the autocomplete applicable element is inside form or exist freely | ||
const closestForm = virtualNode.actualNode.closest('form'); | ||
|
||
//if it exists inside the form and autocomplete for form is off | ||
if ( | ||
closestForm && | ||
(closestForm | ||
.getAttribute('autocomplete') | ||
?.toLowerCase() | ||
.trim() === 'off' || | ||
closestForm | ||
.getAttribute('autocomplete') | ||
?.toLowerCase() | ||
.trim() === 'chrome-off') | ||
) { | ||
// if autocomplete attribute is not present for element then its a pass in this scenario | ||
// otherwise check all posibilities with the method | ||
return autocomplete | ||
? checkIsElementValidAutocomplete(node, options, virtualNode) | ||
: true; | ||
} else { | ||
// The else case is if form is present and it has autocomplete as on or not set and | ||
// the other case this handles is that element exists independently | ||
|
||
// this method would check for all posibilities | ||
return checkIsElementValidAutocomplete(node, options, virtualNode); | ||
} | ||
} catch (err) { | ||
ErrorHandler.addCheckError('autocomplete-attribute-valid-check', err); | ||
return undefined; | ||
} | ||
|
||
export default autocompleteA11yEvaluate; | ||
} | ||
|
||
export default autocompleteA11yEvaluate; |
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
Oops, something went wrong.