]> git.ipfire.org Git - thirdparty/foundation/foundation-sites.git/commitdiff
Added unit tests for `trapFocus()` and `releaseFocus()`.
authorMarius Olbertz <marius.olbertz@gmail.com>
Sat, 5 Nov 2016 09:39:18 +0000 (10:39 +0100)
committerMarius Olbertz <marius.olbertz@gmail.com>
Sat, 5 Nov 2016 09:39:18 +0000 (10:39 +0100)
test/javascript/util/keyboard.js

index 5c3f00256d73ea347d601a4bd4f753f62480d9c4..fcee539d2dfd89c9731b688e58ba02581e592ae2 100644 (file)
@@ -188,4 +188,81 @@ describe('Keyboard util', function() {
       $html.remove();
     });
   });
+
+  describe('trapFocus()', function() {
+    it('moves the focus to the first focusable element', function() {
+      let $html = $(`<div>
+            <a href="#">Link1</a>
+            <a href="#">Link2</a>
+            <a href="#">Link3</a>
+          </div>`).appendTo('body');
+
+      Foundation.Keyboard.trapFocus($html);
+      $html.find('a').last().focus();
+
+      // Three links, so move focus foreward three times
+      $(document.activeElement).trigger(new $.Event('keydown', createEvent(keyCodes['TAB'])));
+
+      document.activeElement.should.be.equal($html.find('a').eq(0)[0]);
+
+      $html.remove();
+    });
+
+    it('moves the focus to the last focusable element', function() {
+      let $html = $(`<div>
+            <a href="#">Link1</a>
+            <a href="#">Link2</a>
+            <a href="#">Link3</a>
+          </div>`).appendTo('body');
+
+      Foundation.Keyboard.trapFocus($html);
+      $html.find('a').first().focus();
+
+      $(document.activeElement).trigger(new $.Event('keydown', createEvent(keyCodes['TAB'], {shift: true})));
+
+      document.activeElement.should.be.equal($html.find('a').eq(2)[0]);
+
+      $html.remove();
+    });
+  });
+
+  describe('releaseFocus()', function() {
+    it('stops trapping the focus at the end', function() {
+      let $html = $(`<div>
+            <a href="#">Link1</a>
+            <a href="#">Link2</a>
+            <a href="#">Link3</a>
+          </div>`).appendTo('body');
+
+      Foundation.Keyboard.trapFocus($html);
+      $html.find('a').last().focus();
+
+      Foundation.Keyboard.releaseFocus($html);
+
+      $(document.activeElement).trigger(new $.Event('keydown', createEvent(keyCodes['TAB'])));
+
+      document.activeElement.should.not.be.equal($html.find('a').eq(0)[0]);
+
+      $html.remove();
+    });
+
+    it('stops trapping the focus at the top', function() {
+      let $html = $(`<div>
+            <a href="#">Link1</a>
+            <a href="#">Link2</a>
+            <a href="#">Link3</a>
+          </div>`).appendTo('body');
+
+      Foundation.Keyboard.trapFocus($html);
+      $html.find('a').first().focus();
+
+      Foundation.Keyboard.releaseFocus($html);
+
+      $(document.activeElement).trigger(new $.Event('keydown', createEvent(keyCodes['TAB'], {shift: true})));
+
+      document.activeElement.should.not.be.equal($html.find('a').eq(2)[0]);
+
+      $html.remove();
+    });
+  });
 });