2018-06-15 11:03:24 -04:00
|
|
|
import DiscourseURL from "discourse/lib/url";
|
2015-08-10 17:11:27 -04:00
|
|
|
|
2014-07-30 18:56:01 -04:00
|
|
|
var testMouseTrap;
|
2018-06-15 11:03:24 -04:00
|
|
|
import KeyboardShortcuts from "discourse/lib/keyboard-shortcuts";
|
2014-07-30 18:56:01 -04:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.module("lib:keyboard-shortcuts", {
|
|
|
|
beforeEach() {
|
2014-07-30 18:56:01 -04:00
|
|
|
var _bindings = {};
|
2013-09-20 19:33:49 -04:00
|
|
|
|
2014-07-30 18:56:01 -04:00
|
|
|
testMouseTrap = {
|
2013-09-20 19:33:49 -04:00
|
|
|
bind: function(bindings, callback) {
|
2019-05-16 04:34:19 -04:00
|
|
|
var registerBinding = function(binding) {
|
2014-07-30 18:56:01 -04:00
|
|
|
_bindings[binding] = callback;
|
2019-05-16 04:34:19 -04:00
|
|
|
}.bind(this);
|
2013-09-20 19:33:49 -04:00
|
|
|
|
|
|
|
if (_.isArray(bindings)) {
|
2018-11-20 08:46:58 -05:00
|
|
|
bindings.forEach(registerBinding, this);
|
2018-06-15 11:03:24 -04:00
|
|
|
} else {
|
2013-09-20 19:33:49 -04:00
|
|
|
registerBinding(bindings);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
trigger: function(binding) {
|
2014-07-30 18:56:01 -04:00
|
|
|
_bindings[binding].call();
|
2013-09-20 19:33:49 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-10 17:11:27 -04:00
|
|
|
sandbox.stub(DiscourseURL, "routeTo");
|
2013-09-20 19:33:49 -04:00
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
$("#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'>",
|
|
|
|
" <button class='star'></button>",
|
|
|
|
" <button class='create'></button>",
|
|
|
|
" <button class='share'></button>",
|
|
|
|
" <button id='dismiss-new-top'></button>",
|
|
|
|
" <button id='dismiss-topics-top'></button>",
|
|
|
|
"</div>",
|
|
|
|
"<div class='alert alert-info clickable'></div>",
|
|
|
|
"<button id='create-topic'></button>",
|
|
|
|
"<div id='user-notifications'></div>",
|
|
|
|
"<div id='toggle-hamburger-menu'></div>",
|
|
|
|
"<div id='search-button'></div>",
|
|
|
|
"<div id='current-user'></div>",
|
|
|
|
"<div id='keyboard-help'></div>"
|
|
|
|
].join("\n")
|
|
|
|
);
|
2013-09-20 19:33:49 -04:00
|
|
|
},
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
afterEach() {
|
2013-09-20 19:33:49 -04:00
|
|
|
$("#qunit-scratch").html("");
|
2019-05-28 06:15:12 -04:00
|
|
|
testMouseTrap = undefined;
|
2013-09-20 19:33:49 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-11-20 08:46:58 -05:00
|
|
|
var pathBindings = KeyboardShortcuts.PATH_BINDINGS || {};
|
|
|
|
Object.keys(pathBindings).forEach(path => {
|
|
|
|
const binding = pathBindings[path];
|
2013-09-20 19:33:49 -04:00
|
|
|
var testName = binding + " goes to " + path;
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
test(testName, function(assert) {
|
2019-01-10 05:06:01 -05:00
|
|
|
KeyboardShortcuts.bindEvents(testMouseTrap, Discourse.__container__);
|
2014-07-30 18:56:01 -04:00
|
|
|
testMouseTrap.trigger(binding);
|
2013-09-20 19:33:49 -04:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.ok(DiscourseURL.routeTo.calledWith(path));
|
2013-09-20 19:33:49 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-11-20 08:46:58 -05:00
|
|
|
var clickBindings = KeyboardShortcuts.CLICK_BINDINGS || {};
|
|
|
|
Object.keys(clickBindings).forEach(selector => {
|
|
|
|
const binding = clickBindings[selector];
|
2013-09-20 19:33:49 -04:00
|
|
|
var bindings = binding.split(",");
|
|
|
|
|
|
|
|
var testName = binding + " clicks on " + selector;
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
test(testName, function(assert) {
|
2019-01-10 05:06:01 -05:00
|
|
|
KeyboardShortcuts.bindEvents(testMouseTrap, Discourse.__container__);
|
2013-09-20 19:33:49 -04:00
|
|
|
$(selector).on("click", function() {
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.ok(true, selector + " was clicked");
|
2013-09-20 19:33:49 -04:00
|
|
|
});
|
|
|
|
|
2018-11-20 08:46:58 -05:00
|
|
|
bindings.forEach(function(b) {
|
|
|
|
testMouseTrap.trigger(b);
|
|
|
|
}, this);
|
2013-09-20 19:33:49 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-11-20 08:46:58 -05:00
|
|
|
var functionBindings = KeyboardShortcuts.FUNCTION_BINDINGS || {};
|
|
|
|
Object.keys(functionBindings).forEach(func => {
|
|
|
|
const binding = functionBindings[func];
|
2013-09-20 19:33:49 -04:00
|
|
|
var testName = binding + " calls " + func;
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
test(testName, function(assert) {
|
2015-08-10 17:11:27 -04:00
|
|
|
sandbox.stub(KeyboardShortcuts, func, function() {
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.ok(true, func + " is called when " + binding + " is triggered");
|
2013-09-20 19:33:49 -04:00
|
|
|
});
|
2019-01-10 05:06:01 -05:00
|
|
|
KeyboardShortcuts.bindEvents(testMouseTrap, Discourse.__container__);
|
2013-09-20 19:33:49 -04:00
|
|
|
|
2014-07-30 18:56:01 -04:00
|
|
|
testMouseTrap.trigger(binding);
|
2013-09-20 19:33:49 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("selectDown calls _moveSelection with 1", assert => {
|
2019-01-10 05:06:01 -05:00
|
|
|
var stub = sandbox.stub(KeyboardShortcuts, "_moveSelection");
|
2013-09-20 19:33:49 -04:00
|
|
|
|
2015-08-10 17:11:27 -04:00
|
|
|
KeyboardShortcuts.selectDown();
|
2019-01-10 05:06:01 -05:00
|
|
|
assert.ok(stub.calledWith(1), "_moveSelection is called with 1");
|
2013-09-20 19:33:49 -04:00
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("selectUp calls _moveSelection with -1", assert => {
|
2019-01-10 05:06:01 -05:00
|
|
|
var stub = sandbox.stub(KeyboardShortcuts, "_moveSelection");
|
2013-09-20 19:33:49 -04:00
|
|
|
|
2015-08-10 17:11:27 -04:00
|
|
|
KeyboardShortcuts.selectUp();
|
2019-01-10 05:06:01 -05:00
|
|
|
assert.ok(stub.calledWith(-1), "_moveSelection is called with -1");
|
2013-09-20 19:33:49 -04:00
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("goBack calls history.back", assert => {
|
2014-07-30 18:56:01 -04:00
|
|
|
var called = false;
|
2018-10-04 11:06:14 -04:00
|
|
|
sandbox.stub(history, "back").callsFake(function() {
|
2014-07-30 18:56:01 -04:00
|
|
|
called = true;
|
|
|
|
});
|
2013-09-20 19:33:49 -04:00
|
|
|
|
2015-08-10 17:11:27 -04:00
|
|
|
KeyboardShortcuts.goBack();
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.ok(called, "history.back is called");
|
2013-09-20 19:33:49 -04:00
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("nextSection calls _changeSection with 1", assert => {
|
2018-06-15 11:03:24 -04:00
|
|
|
var spy = sandbox.spy(KeyboardShortcuts, "_changeSection");
|
2013-09-20 19:33:49 -04:00
|
|
|
|
2015-08-10 17:11:27 -04:00
|
|
|
KeyboardShortcuts.nextSection();
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.ok(spy.calledWith(1), "_changeSection is called with 1");
|
2013-09-20 19:33:49 -04:00
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("prevSection calls _changeSection with -1", assert => {
|
2018-06-15 11:03:24 -04:00
|
|
|
var spy = sandbox.spy(KeyboardShortcuts, "_changeSection");
|
2013-09-20 19:33:49 -04:00
|
|
|
|
2015-08-10 17:11:27 -04:00
|
|
|
KeyboardShortcuts.prevSection();
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.ok(spy.calledWith(-1), "_changeSection is called with -1");
|
2013-09-20 19:33:49 -04:00
|
|
|
});
|