Skip to content

Instantly share code, notes, and snippets.

@dima117
Last active April 6, 2018 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dima117/dd844c2a73474c7e946345a5f621742e to your computer and use it in GitHub Desktop.
Save dima117/dd844c2a73474c7e946345a5f621742e to your computer and use it in GitHub Desktop.
{
...
"scripts": {
"test": "mocha-headless-chrome -f tests/test.html"
},
...
}
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();
}
}
<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>
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();
});
});
it('должна отправлять запрос', function() {
// подготовка
searchForm.input.value = 'субботиник';
// действие
searchForm.button.click();
// проверка
assert.equal(server.lastRequest.url, 'http://example.com2');
});
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