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
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top