Last active
April 6, 2018 16:15
-
-
Save dima117/dd844c2a73474c7e946345a5f621742e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{ | |
... | |
"scripts": { | |
"test": "mocha-headless-chrome -f tests/test.html" | |
}, | |
... | |
} |
This file contains hidden or 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
class SearchForm { | |
onClick(e) { | |
e.preventDefault(); | |
let xhr = new XMLHttpRequest(); | |
xhr.open("GET", "http://example.com"); | |
xhr.send(new FormData(this.form)); | |
this.form.reset(); | |
} | |
render(parent) { | |
this.form = document.createElement('form'); | |
this.form.innerHTML = | |
`<input type="text" name="query" /> | |
<input type="button" value="Найти" />`; | |
this.input = this.form.querySelector('input[type=text]'); | |
this.button = this.form.querySelector('input[type=button]'); | |
this.button.addEventListener('click', this.onClick.bind(this)); | |
parent.appendChild(this.form); | |
} | |
destroy() { | |
this.form.remove(); | |
} | |
} |
This file contains hidden or 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
<html> | |
<head> | |
<meta charset="utf-8"> | |
<link href="../node_modules/mocha/mocha.css" rel="stylesheet" /> | |
<script src="../node_modules/mocha/mocha.js"></script> | |
<script src="../node_modules/sinon/pkg/sinon.js"></script> | |
<script src="../node_modules/chai/chai.js"></script> | |
<script>mocha.setup('bdd');</script> | |
</head> | |
<body> | |
<div id="mocha"></div> | |
<script src="../src/search-form.js"></script> | |
<script src="../tests/test.js"></script> | |
<script>mocha.run();</script> | |
</body> | |
</html> |
This file contains hidden or 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
const assert = chai.assert; | |
describe('форма поиска', function() { | |
let searchForm, server; | |
beforeEach(function() { | |
searchForm = new SearchForm(); | |
searchForm.render(document.body); | |
server = sinon.createFakeServer({ | |
respondImmediately: true | |
}); | |
}); | |
afterEach(function () { | |
searchForm.destroy(); | |
server.restore(); | |
}); | |
}); |
This file contains hidden or 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
it('должна отправлять запрос', function() { | |
// подготовка | |
searchForm.input.value = 'субботиник'; | |
// действие | |
searchForm.button.click(); | |
// проверка | |
assert.equal(server.lastRequest.url, 'http://example.com2'); | |
}); |
This file contains hidden or 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
it('должно очищаться после запроса', function() { | |
// подготовка | |
searchForm.input.value = 'субботиник'; | |
// действие | |
searchForm.button.click(); | |
// проверка | |
assert.equal(searchForm.input.value, ''); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment