问题
I want to see if I can test form field validation using mocha & chai.
I have the following files:
./src/index.html
:
<!doctype html>
<html>
<head>
<title>TDD Project</title>
<style>
#container {width: 600px; max-width: 100%; margin: 1em auto;}
</style>
<link rel="stylesheet" href="../node_modules/mocha/mocha.css">
</head>
<body>
<div id="container">
<form>
<input type="text" id="text" placeholder="enter some text" required maxlength="10" pattern="^[a-z,A-Z]{1,10}$"><br />
<input type="text" id="number" placeholder="enter a number" required maxlength="10" pattern="\d{10}"><br />
<input type="submit">
</form>
</div>
<div id="mocha"></div>
<script src="../node_modules/mocha/mocha.js"></script>
<script src="../node_modules/chai/chai.js"></script>
<script>mocha.setup('bdd')</script>
<script src="../test/test2.js"></script>
<script>
mocha.run();
</script>
</body>
</html>
./test/test.js
:
var assert = require("chai").assert;
function FormController() {
function isValidField(fieldID){
field = document.getElementById(fieldID);
return(field.checkValidity() = true ? true : false);
}
return {
isValidField
}
}
describe("Simple assert", function() {
it("foo != bar", function() {
assert('foo' != 'bar', 'foo is not bar');
});
it('should return true if field is valid', function(){
var isValidText = FormController.isValidField(text);
var isValidNumber = FormController.isValidField(number);
assert.equal(isValidText, true);
assert.equal(isValidNumber, true);
});
})
I have run the commands
> cd test
> browserify test.js > test2.js
I receive the error:
Error: Parsing file /Users/user/Documents/Projects/Contact_Form/test/test.js: Assigning to rvalue (6:15)
The problem appears to be with field.checkValidity()
but I don't understand why.
Is it because the file test.js
cannot access the DOM?
checkValidity()
is a "HTML5 constraint validation API" discussed on this page.
Help appreciated.
回答1:
From this answer:
You get rvalue error, when you use = instead of == in a condition checking block.
来源:https://stackoverflow.com/questions/53385821/cant-browserify-file-error-parsing-file-html5-checkvalidity