all files / jDollarX/lib-cov/ index.js

100% Statements 100/100
50% Branches 2/4
100% Functions 27/27
100% Lines 100/100
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223                                                                                                                                                                                                                                                     
/* eslint-env node, mocha */
/** @jsx J$X */
var sinon = require('sinon');
var jsdom = require('jsdom');
var jquery = require('jquery');
var J$X = require('../jDollarX');
 
var window = jsdom.jsdom().defaultView;
var $ = jquery(window);
 
J$X.use($);
 
describe('Build with tag name', function () {
  it('should work', function () {
    var $el = J$X('div', null);
    $el.is('div').should.ok();
  });
 
  it('should work with nested elements', function () {
    var $el = J$X(
      'div',
      null,
      J$X('span', null)
    );
    $el.children('span').length.should.be.equal(1);
  });
});
 
describe('Build with content', function () {
  it('should work with text', function () {
    var $el = J$X(
      'div',
      null,
      'foo'
    );
    $el.text().should.be.equal('foo');
  });
 
  it('should work with encoded text', function () {
    var $el = J$X(
      'div',
      null,
      "<a></a>"
    );
    $el.text().should.be.equal('<a></a>');
  });
 
  it('should work with number', function () {
    var $el = J$X(
      'div',
      null,
      2
    );
    $el.text().should.be.equal('2');
  });
 
  it('should work with boolean', function () {
    var $true = J$X(
      'div',
      null,
      true
    );
    var $false = J$X(
      'div',
      null,
      false
    );
    $true.text().should.be.equal('true');
    $false.text().should.be.equal('false');
  });
 
  it('should work with null', function () {
    var $el = J$X(
      'div',
      null,
      null
    );
    $el.text().should.be.empty();
  });
 
  it('should work with undefined', function () {
    var $el = J$X(
      'div',
      null,
      void 0
    );
    $el.text().should.be.empty();
  });
 
  it('should work with an array of content', function () {
    var $el = J$X(
      'div',
      null,
      [1, 'two', false]
    );
    $el.text().should.be.equal('1twofalse');
  });
 
  it('should work with children prop', function () {
    var $el = J$X('div', { children: ['a', J$X(
        'span',
        null,
        'b'
      )] });
    $el.contents().first().text().should.be.equal('a');
    $el.children('span').text().should.be.equal('b');
  });
 
  it('should use actual children but not children prop', function () {
    var $el = J$X(
      'div',
      { children: 'a' },
      'b'
    );
    $el.text().should.be.equal('b');
  });
});
 
describe('Build with attributes', function () {
  it('should work with custom attributes', function () {
    var $el = J$X('div', { 'data-foo': 'bar' });
    $el.attr('data-foo').should.be.equal('bar');
  });
 
  it('should work with class', function () {
    var classList = 'foo bar';
    var $el = J$X('div', { className: classList });
    $el.hasClass('bar').should.be.equal(true);
  });
 
  it('should work with style', function () {
    var inlineStyle = { display: 'inline' };
    var $el = J$X('div', { style: inlineStyle });
    $el.css('display').should.be.equal('inline');
  });
 
  it('should work with style of multiple words\' name', function () {
    var inlineStyle = { textAlign: 'center' };
    var $el = J$X('div', { style: inlineStyle });
    $el.css('text-align').should.be.equal('center');
  });
 
  it('should work with style value of number type', function () {
    var inlineStyle = {
      width: -0,
      height: +0,
      borderWidth: 1 / 0,
      margin: NaN,
      fontSize: 14
    };
    var $el = J$X('div', { style: inlineStyle });
    $el.css('height').should.be.equal('0px');
    $el.css('width').should.be.equal('0px');
    $el.css('border-width').should.be.empty();
    $el.css('margin').should.be.empty();
    $el.css('font-size').should.be.equal('14px');
  });
 
  it('should work with attributes & expression', function () {
    var $true = J$X('div', { 'data-foo': true ? 'True!' : 'False!' });
    var $false = J$X('div', { 'data-foo': false ? 'True!' : 'False!' });
    $true.attr('data-foo').should.be.equal('True!');
    $false.attr('data-foo').should.be.equal('False!');
  });
});
 
describe('Build with events', function () {
  it('should work with event & expression', function () {
    var foo = sinon.spy();
    var $el = J$X('div', { onClick: foo });
    $el.click();
    foo.calledOnce.should.be.true();
    foo.calledWith(sinon.match.instanceOf($.Event)).should.be.true();
  });
 
  it('should work with incorrect capitalized event', function () {
    var foo = sinon.spy();
    var $el = J$X('div', { onClIcK: foo });
    $el.click();
    foo.calledOnce.should.be.true();
    foo.calledWith(sinon.match.instanceOf($.Event)).should.be.true();
  });
 
  it('should not decide not on-EventName event', function () {
    var foo = sinon.spy();
    var $el = J$X('div', { oncLick: foo });
    $el.click();
    foo.neverCalledWith(sinon.match.instanceOf($.Event)).should.be.true();
  });
});
 
describe('Build with nested components', function () {
  it('should work', function () {
    function Editor(props) {
      /* eslint-disable react/prop-types */
      return J$X(
        'form',
        { action: props.action },
        J$X(
          'textarea',
          null,
          'abc'
        ),
        J$X(
          'button',
          null,
          props.submitText
        )
      );
      /* eslint-enable */
    }
 
    var submitText = 'Submit >.<';
    var $el = J$X(
      'div',
      null,
      J$X(Editor, { action: '//example.com', submitText: submitText })
    );
 
    $el.children('form').attr('action').should.be.equal('//example.com');
    $el.find('form button').text().should.be.equal(submitText);
  });
});