encho253

Untitled

Mar 20th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*globals describe, it, require, before, global*/
  2. var expect = require('chai').expect;
  3. var jsdom = require('jsdom');
  4. var jq = require('jquery');
  5. var result = require('../tasks/task-2')();
  6.  
  7. describe('Task #2 Tests', function () {
  8.  
  9.   before(function (done) {
  10.     jsdom.env({
  11.       html: '',
  12.       done: function (errors, window) {
  13.         global.window = window;
  14.         global.document = window.document;
  15.         global.$ = jq(window);
  16.         Object.keys(window)
  17.           .filter(function (prop) {
  18.             return prop.toLowerCase().indexOf('html') >= 0;
  19.           }).forEach(function (prop) {
  20.             global[prop] = window[prop];
  21.           });
  22.         done();
  23.       }
  24.     });
  25.   });
  26.  
  27.   describe('Valid Tests', function () {
  28.     it('expect nothing to change, whenm no elements with class `button` or `content`', function () {
  29.       var html = '<div id="root"><div></div><ul><li><a href="#">link</a></li></ul></div>';
  30.       document.body.innerHTML = html;
  31.       result('#root');
  32.       expect(document.body.innerHTML).to.equal(html);
  33.     });
  34.  
  35.     it('expect to change the content of all `.button` to "hide"', function () {
  36.       var container = document.createElement('div'),
  37.         count = 15,
  38.         possibleTags = ['a', 'button', 'p', 'div'],
  39.         i,
  40.         len,
  41.         buttonNode,
  42.         contentNode,
  43.         tag;
  44.  
  45.       container.id = 'root';
  46.       for (i = 0; i < count; i += 1) {
  47.         tag = possibleTags[(Math.random() * possibleTags.length) | 0];
  48.         buttonNode = document.createElement(tag);
  49.         buttonNode.className = 'button';
  50.         container.appendChild(buttonNode);
  51.  
  52.         possibleTags[(Math.random() * possibleTags.length) | 0];
  53.         contentNode = document.createElement(tag);
  54.         contentNode.className = 'content';
  55.         container.appendChild(contentNode);
  56.       }
  57.       document.body.innerHTML = container.outerHTML;
  58.  
  59.       result('#root');
  60.       var btns = document.getElementsByClassName('button');
  61.       expect(btns).to.have.length(count);
  62.       for (i = 0, len = btns.length; i < len; i += 1) {
  63.         expect(btns[i].innerHTML).to.equal('hide');
  64.       }
  65.     });
  66.  
  67.     it('expect to hide the next content on button click, and then show it again, when there are other elements', function () {
  68.       function createDummyNode() {
  69.         var tags = ['a', 'button', 'p', 'div', 'ul', 'li', 'ol', 'input', 'table', 'tr', 'br', 'hr', 'span'];
  70.         var node = document.createElement(tags[(Math.random * tags.length) | 0]);
  71.         node.innerHTML = 'Dummy Element: ' + Math.random();
  72.         return node;
  73.       }
  74.       var container = document.createElement('div'),
  75.         count = 150,
  76.         possibleTags = ['a', 'button', 'p', 'div'],
  77.         i,
  78.         buttonNode,
  79.         contentNode,
  80.         tag;
  81.  
  82.       container.id = 'root';
  83.       var dymmyObjectChance = 70;
  84.       for (i = 0; i < count; i += 1) {
  85.         if (Math.random() * 100 < dymmyObjectChance) {
  86.           container.appendChild(createDummyNode());
  87.         }
  88.         tag = possibleTags[(Math.random() * possibleTags.length) | 0];
  89.         buttonNode = document.createElement(tag);
  90.         buttonNode.className = 'button';
  91.         if (i === ((count / 2) | 0)) {
  92.           buttonNode.id = 'the-button';
  93.         }
  94.         container.appendChild(buttonNode);
  95.  
  96.         possibleTags[(Math.random() * possibleTags.length) | 0];
  97.         contentNode = document.createElement(tag);
  98.         contentNode.className = 'content';
  99.  
  100.         if (Math.random() * 100 < dymmyObjectChance) {
  101.           container.appendChild(createDummyNode());
  102.         }
  103.         container.appendChild(contentNode);
  104.  
  105.         if (Math.random() * 100 < dymmyObjectChance) {
  106.           container.appendChild(createDummyNode());
  107.         }
  108.       }
  109.       document.body.innerHTML = container.outerHTML;
  110.  
  111.       result('#root');
  112.  
  113.       var theButton = document.getElementById('the-button');
  114.  
  115.       var theContent = theButton.nextElementSibling;
  116.       while (theContent && theContent.className.indexOf('content') < 0) {
  117.         theContent = theContent.nextElementSibling;
  118.       }
  119.  
  120.       var event = document.createEvent('MouseEvents');
  121.       event.initMouseEvent('click', true, true);
  122.       theButton.dispatchEvent(event);
  123.  
  124.       expect(theButton).to.exist;
  125.       expect(theContent).to.exist;
  126.       expect(theButton.innerHTML).to.equal('show');
  127.       expect(theContent.style.display).to.equal('none');
  128.  
  129.       theButton.dispatchEvent(event);
  130.       expect(theButton.innerHTML).to.equal('hide');
  131.       expect(theContent.style.display).to.equal('');
  132.     });
  133.  
  134.     it('expect to hide the next content on button click, and then show it again', function () {
  135.       var container = document.createElement('div'),
  136.         count = 15,
  137.         possibleTags = ['a', 'button', 'p', 'div'],
  138.         i,
  139.         buttonNode,
  140.         contentNode,
  141.         tag;
  142.  
  143.       container.id = 'root';
  144.       for (i = 0; i < count; i += 1) {
  145.         tag = possibleTags[(Math.random() * possibleTags.length) | 0];
  146.         buttonNode = document.createElement(tag);
  147.         buttonNode.className = 'button';
  148.         if (i === ((count / 2) | 0)) {
  149.           buttonNode.id = 'the-button';
  150.         }
  151.         container.appendChild(buttonNode);
  152.  
  153.         possibleTags[(Math.random() * possibleTags.length) | 0];
  154.         contentNode = document.createElement(tag);
  155.         contentNode.className = 'content';
  156.         container.appendChild(contentNode);
  157.       }
  158.       document.body.innerHTML = container.outerHTML;
  159.  
  160.       result('#root');
  161.  
  162.       var theButton = document.getElementById('the-button');
  163.  
  164.       var theContent = theButton.nextElementSibling;
  165.       while (theContent && theContent.className.indexOf('content') < 0) {
  166.         theContent = theContent.nextElementSibling;
  167.       }
  168.  
  169.       var event = document.createEvent('MouseEvents');
  170.       event.initMouseEvent('click', true, true);
  171.       theButton.dispatchEvent(event);
  172.  
  173.       expect(theButton).to.exist;
  174.       expect(theContent).to.exist;
  175.       expect(theButton.innerHTML).to.equal('show');
  176.       expect(theContent.style.display).to.equal('none');
  177.  
  178.       theButton.dispatchEvent(event);
  179.       expect(theButton.innerHTML).to.equal('hide');
  180.       expect(theContent.style.display).to.equal('');
  181.     });
  182.   });
  183.  
  184.   describe('Invalid', function () {
  185.     it('Expect to throw, when no params are provided', function () {
  186.       function test() {
  187.         result();
  188.       }
  189.       expect(test).to.throw();
  190.     });
  191.  
  192.     /* Selector validation */
  193.     it('Expect to throw, when undefined is provided as selector', function () {
  194.       function test() {
  195.         result(undefined);
  196.       }
  197.       expect(test).to.throw();
  198.     });
  199.  
  200.     it('Expect to throw, when null is provided as selector', function () {
  201.       function test() {
  202.         result(null);
  203.       }
  204.       expect(test).to.throw();
  205.     });
  206.  
  207.     it('Expect to throw, when selector that selects nothing is provided', function () {
  208.       function test() {
  209.         result('TEST_NO_SUCH_SELECTOR');
  210.       }
  211.       expect(test).to.throw();
  212.     });
  213.   });
  214.  
  215. });
Advertisement
Add Comment
Please, Sign In to add comment