BVB Source Codes

web-design-standards Show dispatch.spec.js Source code

Return Download web-design-standards: download dispatch.spec.js Source code - Download web-design-standards Source code - Type:.js
  1. var should = require('should');
  2. var dispatch = require('../../../src/js/utils/dispatch');
  3. var sinon = require('sinon');
  4.  
  5. describe('dispatch', function () {
  6.  
  7.   var oldDocument = global.document;
  8.  
  9.   after(function () {
  10.     global.document = oldDocument;
  11.     global.window = document.defaultView;
  12.   });
  13.  
  14.   it('attaches an event to an element', function () {
  15.     var element = document.body;
  16.     var flag = false;
  17.     var listener = function (e) { flag = true; };
  18.     var dispatcher = dispatch(element, 'click', listener);
  19.     click(element);
  20.     flag.should.equal(true);
  21.     dispatcher.off();
  22.   });
  23.  
  24.   it('can attach a listener for multiple events', function () {
  25.     var element = document.body;
  26.     var flag = false;
  27.     var listener = function (e) { flag = true; };
  28.     var dispatcher = dispatch(element, 'click touchstart', listener);
  29.    
  30.     click(element);
  31.     flag.should.equal(true);
  32.    
  33.     flag = false;
  34.     touchstart(element);
  35.     flag.should.equal(true);
  36.    
  37.     dispatcher.off();    
  38.   });
  39.  
  40.   describe('it returns an object which', function () {
  41.     it('contains an "off" method for detaching the listener', function () {
  42.       // confirm that the listener has been added
  43.       var element = document.body;
  44.       var flag = false;
  45.       var listener = function (e) { flag = true; };
  46.       var dispatcher = dispatch(element, 'click touchstart', listener);
  47.       click(element);
  48.       flag.should.equal(true);
  49.       // now detach it
  50.       flag = false;
  51.       dispatcher.off();
  52.       click(element);
  53.       flag.should.equal(false);
  54.       touchstart(element);
  55.       flag.should.equal(false);      
  56.     });
  57.  
  58.     it('contains a "trigger" method for calling the listener', function () {
  59.       var element = document.body;
  60.       var flag = false;
  61.       var listener = function (e) { flag = true; };
  62.       var dispatcher = dispatch(element, 'click', listener);
  63.       dispatcher.trigger();
  64.       flag.should.equal(true);
  65.       dispatcher.off();      
  66.     });
  67.   });
  68. });
  69.  
  70. function click (el) {
  71.   var evt = document.createEvent('HTMLEvents');
  72.   evt.initEvent('click', false, true);
  73.   el.dispatchEvent(evt);
  74. }
  75.  
  76. function touchstart (el) {
  77.   var evt = document.createEvent('HTMLEvents');
  78.   evt.initEvent('touchstart', false, true);
  79.   el.dispatchEvent(evt);
  80. }
  81.  
downloaddispatch.spec.js Source code - Download web-design-standards Source code
Related Source Codes/Software:
KineticJS - KineticJS is an HTML5 Canvas JavaScript framework ... 2017-01-07
yui3 - A library for building richly interactive web appl... 2017-01-07
TextBlob - Simple, Pythonic, text processing--Sentiment analy... 2017-01-07
monaco-editor - A browser based code editor 2017-01-07
grumpy - Grumpy is a Python to Go source code transcompiler... 2017-01-07
Knuff - The debug application for Apple Push Notification ... 2017-01-07
RoundedImageView - A fast ImageView that supports rounded corners, ov... 2017-01-07
flowchart.js - Draws simple SVG flow chart diagrams from textual ... 2017-01-08
TextFieldEffects - Custom UITextFields effects inspired by Codrops, b... 2017-01-08
glide - Package Management for Golang h... 2017-01-08
Codiad - Web Based, Cloud IDE http:/... 2017-05-01
jQuery-Tags-Input - Magically convert a simple text input into a cool ... 2017-05-01
SpoofMAC - 2017-05-01
bowser - a browser detector 2017-04-30
Timepiece - Intuitive date handling in Swift 2017-04-30
angularUtils - A place where I will collect useful re-usable Angu... 2017-04-30
Learning-SICP - The construction and interpretation of computer pr... 2017-04-30
SketchToAppStore - A sketch framework to make creating screenshots fo... 2017-04-30
hyperapp - 1kb JavaScript library for building frontend appli... 2017-04-30
split - 2017-04-30

 Back to top