discourse/test/javascripts/components/keyboard-shortcuts-test.js.es6

148 lines
4.1 KiB
Plaintext
Raw Normal View History

2014-07-30 18:56:01 -04:00
var testMouseTrap;
module("Discourse.KeyboardShortcuts", {
setup: function() {
2014-07-30 18:56:01 -04:00
var _bindings = {};
2014-07-30 18:56:01 -04:00
testMouseTrap = {
bind: function(bindings, callback) {
var registerBinding = _.bind(function(binding) {
2014-07-30 18:56:01 -04:00
_bindings[binding] = callback;
}, this);
if (_.isArray(bindings)) {
_.each(bindings, registerBinding, this);
}
else {
registerBinding(bindings);
}
},
trigger: function(binding) {
2014-07-30 18:56:01 -04:00
_bindings[binding].call();
}
};
2014-07-30 18:56:01 -04:00
sandbox.stub(Discourse.URL, "routeTo");
$("#qunit-fixture").html([
"<article class='topic-post selected'>",
"<a class='post-date'></a>" +
"</article>",
"<div class='notification-options'>",
" <ul>",
" <li data-id='0'><a></a></li>",
" <li data-id='1'><a></a></li>",
" <li data-id='2'><a></a></li>",
" <li data-id='3'><a></a></li>",
" </ul>",
"</div>",
"<table class='topic-list'>",
" <tr class='topic-list-item selected'><td>",
" <a class='title'></a>",
" </td></tr>",
"</table>",
"<div id='topic-footer-buttons'>",
2014-01-09 16:22:54 -05:00
" <button class='star'></button>",
" <button class='create'></button>",
" <button class='share'></button>",
"</div>",
"<button id='create-topic'></button>",
"<div id='user-notifications'></div>",
2014-08-27 00:47:23 -04:00
"<div id='site-map'></div>",
"<div id='search-button'></div>",
2014-08-27 00:47:23 -04:00
"<div id='current-user'></div>",
"<div id='keyboard-help'></div>"
].join("\n"));
},
teardown: function() {
$("#qunit-scratch").html("");
}
});
var pathBindings = Discourse.KeyboardShortcuts.PATH_BINDINGS;
_.each(pathBindings, function(path, binding) {
var testName = binding + " goes to " + path;
test(testName, function() {
2014-07-30 18:56:01 -04:00
Discourse.KeyboardShortcuts.bindEvents(testMouseTrap);
testMouseTrap.trigger(binding);
ok(Discourse.URL.routeTo.calledWith(path));
});
});
var clickBindings = Discourse.KeyboardShortcuts.CLICK_BINDINGS;
_.each(clickBindings, function(selector, binding) {
var bindings = binding.split(",");
var testName = binding + " clicks on " + selector;
2014-07-30 18:56:01 -04:00
test(testName, function() {
Discourse.KeyboardShortcuts.bindEvents(testMouseTrap);
$(selector).on("click", function() {
ok(true, selector + " was clicked");
});
_.each(bindings, function(binding) {
2014-07-30 18:56:01 -04:00
testMouseTrap.trigger(binding);
}, this);
});
});
var functionBindings = Discourse.KeyboardShortcuts.FUNCTION_BINDINGS;
_.each(functionBindings, function(func, binding) {
var testName = binding + " calls " + func;
test(testName, function() {
2014-07-30 18:56:01 -04:00
sandbox.stub(Discourse.KeyboardShortcuts, func, function() {
ok(true, func + " is called when " + binding + " is triggered");
});
2014-07-30 18:56:01 -04:00
Discourse.KeyboardShortcuts.bindEvents(testMouseTrap);
2014-07-30 18:56:01 -04:00
testMouseTrap.trigger(binding);
});
});
test("selectDown calls _moveSelection with 1", function() {
2014-07-30 18:56:01 -04:00
var spy = sandbox.spy(Discourse.KeyboardShortcuts, '_moveSelection');
Discourse.KeyboardShortcuts.selectDown();
ok(spy.calledWith(1), "_moveSelection is called with 1");
});
test("selectUp calls _moveSelection with -1", function() {
2014-07-30 18:56:01 -04:00
var spy = sandbox.spy(Discourse.KeyboardShortcuts, '_moveSelection');
Discourse.KeyboardShortcuts.selectUp();
ok(spy.calledWith(-1), "_moveSelection is called with -1");
});
test("goBack calls history.back", function() {
2014-07-30 18:56:01 -04:00
var called = false;
sandbox.stub(history, 'back', function() {
called = true;
});
Discourse.KeyboardShortcuts.goBack();
ok(called, "history.back is called");
});
test("nextSection calls _changeSection with 1", function() {
2014-07-30 18:56:01 -04:00
var spy = sandbox.spy(Discourse.KeyboardShortcuts, '_changeSection');
Discourse.KeyboardShortcuts.nextSection();
ok(spy.calledWith(1), "_changeSection is called with 1");
});
test("prevSection calls _changeSection with -1", function() {
2014-07-30 18:56:01 -04:00
var spy = sandbox.spy(Discourse.KeyboardShortcuts, '_changeSection');
Discourse.KeyboardShortcuts.prevSection();
ok(spy.calledWith(-1), "_changeSection is called with -1");
});