我想看看是否可以使用mocha&chai测试表单字段验证。
我有以下文件:
./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);
});
})
我已经运行了命令
> cd test
> browserify test.js > test2.js
我收到错误:
Error: Parsing file /Users/user/Documents/Projects/Contact_Form/test/test.js: Assigning to rvalue (6:15)
问题似乎出在field.checkValidity()
上,但我不明白为什么。
是因为文件test.js
无法访问DOM吗?
checkValidity()
是在this page上讨论的“ HTML5约束验证API”。
帮助表示赞赏。