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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
/*global Autolinker, _, describe, beforeEach, afterEach, it, expect */
describe( "Autolinker.HtmlTag", function() {
var HtmlTag = Autolinker.HtmlTag;
it( "should be able to be instantiated with no arguments", function() {
expect( function() {
var tag = new HtmlTag();
} ).not.toThrow();
} );
it( "should be able to be configured via the config options", function() {
var tag = new HtmlTag( {
tagName : 'a',
attrs : { attr1: 'value1', attr2: 'value2' },
innerHtml : "Hello"
} );
expect( tag.getTagName() ).toBe( 'a' );
expect( tag.getAttrs() ).toEqual( { attr1: 'value1', attr2: 'value2' } );
expect( tag.getInnerHtml() ).toBe( "Hello" );
} );
it( "should be able to be configured via setters", function() {
var tag = new HtmlTag();
tag.setTagName( 'a' );
tag.setAttrs( { attr1: 'value1', attr2: 'value2' } );
tag.setInnerHtml( "Hello" );
expect( tag.getTagName() ).toBe( 'a' );
expect( tag.getAttrs() ).toEqual( { attr1: 'value1', attr2: 'value2' } );
expect( tag.getInnerHtml() ).toBe( "Hello" );
} );
describe( 'setTagName()', function() {
it( "should set, and override, the tag name", function() {
var tag = new HtmlTag();
tag.setTagName( 'a' );
expect( tag.getTagName() ).toBe( 'a' );
tag.setTagName( 'button' );
expect( tag.getTagName() ).toBe( 'button' );
} );
it( "should return a reference to the HtmlTag instance, to allow method chaining", function() {
var tag = new HtmlTag();
expect( tag.setTagName( 'a' ) ).toBe( tag ); // return value should be the HtmlTag itself
} );
} );
describe( 'setAttr()', function() {
it( "should set, then override, attribute properties", function() {
var tag = new HtmlTag();
tag.setAttr( 'attr1', 'value1' ); // note: this call should lazily instantiate the `attrs` map
tag.setAttr( 'attr2', 'value2' );
expect( tag.getAttrs() ).toEqual( { attr1: 'value1', attr2: 'value2' } );
tag.setAttr( 'attr1', 42 );
expect( tag.getAttrs() ).toEqual( { attr1: 42, attr2: 'value2' } );
} );
it( "should return a reference to the HtmlTag instance, to allow method chaining", function() {
var tag = new HtmlTag();
expect( tag.setAttr( 'href', 'test' ) ).toBe( tag ); // return value should be the HtmlTag itself
} );
} );
describe( 'setAttrs()', function() {
it( "should set, then override, attribute properties", function() {
var tag = new HtmlTag();
tag.setAttrs( { attr1: 'value1', attr2: 'value2' } ); // note: this call should lazily instantiate the `attrs` map
expect( tag.getAttrs() ).toEqual( { attr1: 'value1', attr2: 'value2' } );
tag.setAttrs( { attr1: 42 } );
expect( tag.getAttrs() ).toEqual( { attr1: 42, attr2: 'value2' } );
} );
it( "should return a reference to the HtmlTag instance, to allow method chaining", function() {
var tag = new HtmlTag();
expect( tag.setAttrs( { 'href': 'test' } ) ).toBe( tag ); // return value should be the HtmlTag itself
} );
} );
describe( 'setClass()', function() {
var tag;
beforeEach( function() {
tag = new HtmlTag();
} );
it( "should set the CSS class to the tag when there are none yet", function() {
tag.setClass( 'test' );
expect( tag.getClass() ).toBe( 'test' );
} );
it( "should overwrite the current CSS classes on the tag", function() {
tag.setClass( 'test' );
tag.setClass( 'test_override' );
expect( tag.getClass() ).toBe( 'test_override' );
} );
it( "should return a reference to the HtmlTag instance, to allow method chaining", function() {
expect( tag.setClass( 'test' ) ).toBe( tag ); // return value should be the HtmlTag itself
} );
} );
describe( 'addClass()', function() {
var tag;
beforeEach( function() {
tag = new HtmlTag();
} );
it( "should add a CSS class to the tag when there are none yet", function() {
tag.addClass( 'test' );
expect( tag.getClass() ).toBe( 'test' );
} );
it( "should add multiple CSS classes to the tag when there are none yet", function() {
tag.addClass( 'test1 test2' );
expect( tag.getClass() ).toBe( 'test1 test2' );
} );
it( "should add a CSS class to existing CSS classes", function() {
tag.addClass( 'test1' );
tag.addClass( 'test2' );
expect( tag.getClass() ).toBe( 'test1 test2' );
} );
it( "should add multiple CSS classes to existing CSS classes", function() {
tag.addClass( 'test1 test2' );
tag.addClass( 'test3 test4' );
expect( tag.getClass() ).toBe( 'test1 test2 test3 test4' );
} );
it( "should not add duplicate CSS classes to the tag", function() {
tag.addClass( 'test1 test2' );
tag.addClass( 'test1 test3 test4 test4' );
expect( tag.getClass() ).toBe( 'test1 test2 test3 test4' );
} );
it( "should return a reference to the HtmlTag instance, to allow method chaining", function() {
expect( tag.addClass( 'test' ) ).toBe( tag ); // return value should be the HtmlTag itself
} );
} );
describe( 'removeClass()', function() {
var tag;
beforeEach( function() {
tag = new HtmlTag();
} );
it( "should have no effect when removing a CSS class from an HtmlTag with no CSS classes", function() {
expect( function() {
tag.removeClass( 'test' ); // simply make sure an error isn't thrown
} ).not.toThrow();
} );
it( "should remove a single CSS class from the HtmlTag", function() {
tag.addClass( 'test1 test2 test3 test4' );
tag.removeClass( 'test1' );
expect( tag.getClass() ).toBe( 'test2 test3 test4' );
} );
it( "should remove multiple CSS classes from the HtmlTag", function() {
tag.addClass( 'test1 test2 test3 test4' );
tag.removeClass( 'test1 test3' );
expect( tag.getClass() ).toBe( 'test2 test4' );
} );
it( "should return a reference to the HtmlTag instance, to allow method chaining", function() {
expect( tag.removeClass( 'test' ) ).toBe( tag ); // return value should be the HtmlTag itself
} );
} );
describe( 'getClass()', function() {
it( "should return an empty string when there are no CSS classes on the HtmlTag", function() {
var tag = new HtmlTag();
expect( tag.getClass() ).toBe( "" );
} );
it( "should return the CSS classes configured on the HtmlTag", function() {
var tag = new HtmlTag( {
attrs: { 'class': "test1 test2" }
} );
expect( tag.getClass() ).toBe( "test1 test2" );
} );
it( "should return the CSS classes set using addClass()", function() {
var tag = new HtmlTag();
tag.addClass( "test1 test2" );
expect( tag.getClass() ).toBe( "test1 test2" );
} );
} );
describe( 'hasClass()', function() {
var tag;
beforeEach( function() {
tag = new HtmlTag();
} );
it( "should return `false` when there are no CSS classes on the HtmlTag", function() {
expect( tag.hasClass( 'test' ) ).toBe( false );
} );
it( "should return `true` for a CSS class that exists on the HtmlTag", function() {
tag.addClass( 'test' );
expect( tag.hasClass( 'test' ) ).toBe( true );
} );
it( "should return `true` for a CSS class that exists on the HtmlTag, when there are multiple CSS classes", function() {
tag.addClass( 'test1 test2' );
expect( tag.hasClass( 'test1' ) ).toBe( true );
expect( tag.hasClass( 'test2' ) ).toBe( true );
expect( tag.hasClass( 'test3' ) ).toBe( false );
} );
it( "should return `false` for a CSS class that is a substring of a CSS class on the HtmlTag", function() {
tag.addClass( 'longCssClass' );
expect( tag.hasClass( 'longCss' ) ).toBe( false );
} );
} );
describe( 'setInnerHtml()', function() {
it( "should set, then override, the tag's inner HTML", function() {
var tag = new HtmlTag();
tag.setInnerHtml( "test" );
expect( tag.getInnerHtml() ).toBe( "test" );
tag.setInnerHtml( "test2" );
expect( tag.getInnerHtml() ).toBe( "test2" );
} );
it( "should return a reference to the HtmlTag instance, to allow method chaining", function() {
var tag = new HtmlTag();
expect( tag.setInnerHtml( "test" ) ).toBe( tag ); // return value should be the HtmlTag itself
} );
} );
describe( 'getInnerHtml()', function() {
it( "should return an empty string if no inner HTML has been set", function() {
var tag = new HtmlTag();
expect( tag.getInnerHtml() ).toBe( "" );
} );
it( "should return the inner HTML set during construction", function() {
var tag = new HtmlTag( { innerHtml: "test" } );
expect( tag.getInnerHtml() ).toBe( "test" );
} );
it( "should return the inner HTML set with setInnerHtml()", function() {
var tag = new HtmlTag();
tag.setInnerHtml( "test" );
expect( tag.getInnerHtml() ).toBe( "test" );
} );
} );
describe( 'toAnchorString()', function() {
it( "should populate only the tag name when no attribute are set, and no inner HTML is set", function() {
var tag = new HtmlTag( {
tagName : 'a'
} );
expect( tag.toAnchorString() ).toBe( '<a></a>' );
} );
it( "should populate only the tag name and inner HTML when no attribute are set", function() {
var tag = new HtmlTag( {
tagName : 'a',
innerHtml : "My Site"
} );
expect( tag.toAnchorString() ).toBe( '<a>My Site</a>' );
} );
it( "should populate both the tag name and attributes when set", function() {
var tag = new HtmlTag( {
tagName : 'a',
attrs : { href: 'http://path/to/site', rel: 'nofollow' }
} );
expect( tag.toAnchorString() ).toBe( '<a href="http://path/to/site" rel="nofollow"></a>' );
} );
it( "should populate all 3: tag name, attributes, and inner HTML when set", function() {
var tag = new HtmlTag( {
tagName : 'a',
attrs : { href: 'http://path/to/site', rel: 'nofollow' },
innerHtml : "My Site"
} );
expect( tag.toAnchorString() ).toBe( '<a href="http://path/to/site" rel="nofollow">My Site</a>' );
} );
it( "should properly build an HTML string from just the mutator methods", function() {
var tag = new HtmlTag();
tag.setTagName( 'a' );
tag.addClass( 'test' );
tag.addClass( 'test2' );
tag.setAttr( 'href', 'http://path/to/site' );
tag.setInnerHtml( 'My Site' );
expect( tag.toAnchorString() ).toBe( '<a class="test test2" href="http://path/to/site">My Site</a>' );
} );
} );
} );
|