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 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
|
'use strict';
require('mocha');
require('should');
var assert = require('assert');
var utils = require('./utils');
var Base = require('./');
var base;
describe('constructor', function() {
it('should return an instance of Base:', function() {
base = new Base();
assert(base instanceof Base);
});
it('should return an instance of Base without new:', function() {
base = Base();
assert(base instanceof Base);
});
it('should "visit" over an object to extend the instance', function() {
base = new Base({foo: 'bar'});
assert.equal(base.foo, 'bar');
var app = new Base({options: {a: true, b: false}});
assert(app.options);
assert.equal(app.options.a, true);
assert.equal(app.options.b, false);
});
it('should map "visit" over an array to extend the instance', function() {
base = new Base([{foo: 'bar'}, {baz: 'qux'}]);
assert.equal(base.foo, 'bar');
assert.equal(base.baz, 'qux');
});
it('should set options passed as the second argument', function() {
base = new Base(null, {abc: 'xyz'});
assert.equal(base.options.abc, 'xyz');
});
it('should merge options throughout the inheritance chain', function() {
function Foo(options) {
Base.call(this, null, options);
this.options.x = 'y';
}
Base.extend(Foo);
function Bar(options) {
Foo.call(this, options);
}
Foo.extend(Bar);
var bar = new Bar({a: 'b'});
assert.equal(bar.options.a, 'b');
assert.equal(bar.options.x, 'y');
});
it('should add foo', function() {
base = new Base({
foo: 'bar'
});
assert.equal(base.foo, 'bar');
});
it('should set isBase on the instance', function() {
base = new Base();
assert.equal(base.isBase, true);
});
});
describe('static properties', function() {
beforeEach(function() {
base = new Base();
});
it('should expose `.use` method', function() {
assert.equal(typeof Base.use, 'function');
});
it('should expose `.extend` method', function() {
assert.equal(typeof Base.extend, 'function');
});
it('should extend the given Ctor with static methods:', function() {
function Ctor() {
Base.call(this);
}
Base.extend(Ctor);
assert.equal(typeof Ctor.extend, 'function');
function foo() {}
Ctor.extend(foo);
assert.equal(typeof foo.extend, 'function');
});
describe('.extend', function() {
it('should set the extend method on the given object:', function() {
function Ctor() {}
Base.extend(Ctor);
assert.equal(typeof Ctor.extend, 'function');
});
});
describe('.use', function() {
it('should set the use method on the given object:', function() {
function Ctor() {}
Base.extend(Ctor);
assert.equal(typeof Ctor.use, 'function');
});
it('should use a globally loaded plugin through the static use method:', function() {
function Ctor() {
Base.call(this);
}
Base.extend(Ctor);
Ctor.use(function(app) {
app.foo = 'bar';
});
var inst = new Ctor();
assert.equal(inst.foo, 'bar');
});
it('should use a globally loaded plugin through the static use method with namespace:', function() {
var Foo = Base.namespace('foo');
Foo.use(function(app) {
app.set('bar', 'baz');
});
var inst = new Foo();
assert.equal(inst.get('bar'), 'baz');
assert.equal(inst.foo.bar, 'baz');
});
it('should use different globally installed plugins when using different namespaces:', function() {
var Foo = Base.namespace('foo');
var Bar = Base.namespace('bar');
Foo.use(function(app) {
app.set('bar', 'baz');
});
Bar.use(function(app) {
app.set('beep', 'boop');
});
var foo = new Foo();
var bar = new Bar();
assert.equal(foo.get('bar'), 'baz');
assert.equal(foo.foo.bar, 'baz');
assert.equal(typeof foo.get('beep'), 'undefined');
assert.equal(typeof foo.foo.beep, 'undefined');
assert.equal(bar.get('beep'), 'boop');
assert.equal(bar.bar.beep, 'boop');
assert.equal(typeof bar.get('bar'), 'undefined');
assert.equal(typeof bar.bar.bar, 'undefined');
});
});
describe('.mixin', function() {
it('should set the mixin method on the given object:', function() {
function Ctor() {}
Base.extend(Ctor);
assert.equal(typeof Base.mixin, 'function');
assert.equal(typeof Ctor.mixin, 'function');
});
it('should use a globally loaded mixin through the static mixin method:', function() {
function Ctor() {
Base.call(this);
}
Base.extend(Ctor);
Base.mixin(function(proto) {
proto.foo = 'bar';
});
var inst = new Ctor();
Ctor.mixin(function(proto) {
proto.bar = 'baz';
});
assert.equal(Base.prototype.foo, 'bar');
assert.equal(Ctor.prototype.bar, 'baz');
assert.equal(inst.foo, 'bar');
assert.equal(inst.bar, 'baz');
});
});
describe('.mixins', function() {
it('should set the mixins method on the given object:', function() {
function Ctor() {}
Base.extend(Ctor);
assert.equal(typeof Ctor.mixins, 'function');
});
it('should use a globally loaded mixin through the static mixins method:', function() {
function Ctor() {
Base.call(this);
}
Base.extend(Ctor);
Base.mixin(function fn(proto) {
proto.bar = 'bar';
return fn;
});
function Child() {
Ctor.call(this);
}
Base.extend(Child);
Base.mixins(Child);
Ctor.mixins(Child);
var inst = new Child();
assert.equal(Child.prototype.bar, 'bar');
assert.equal(inst.bar, 'bar');
});
});
});
describe('extend prototype methods', function() {
beforeEach(function() {
var Ctor = require('./');
Base = Ctor.namespace();
});
it('should extend the prototype of the given Ctor:', function() {
function Ctor() {
Base.call(this);
}
Base.extend(Ctor);
assert.equal(typeof Ctor.extend, 'function');
var ctor = new Ctor();
assert.equal(typeof ctor.set, 'function');
assert.equal(typeof ctor.get, 'function');
});
it('should expose `prototype.set` method', function() {
assert.equal(typeof Base.prototype.set, 'function');
});
it('should expose `prototype.get` method', function() {
assert.equal(typeof Base.prototype.get, 'function');
});
it('should expose `prototype.del` method', function() {
assert.equal(typeof Base.prototype.del, 'function');
});
it('should expose `prototype.visit` method', function() {
assert.equal(typeof Base.prototype.visit, 'function');
});
it('should expose `prototype.define` method', function() {
assert.equal(typeof Base.prototype.define, 'function');
});
it('should expose `prototype.mixin` method', function() {
assert.equal(typeof Base.prototype.mixin, 'function');
});
it('should add prototype methods to the given Ctor:', function() {
function Ctor() {
Base.call(this);
}
Base.extend(Ctor);
assert.equal(typeof Ctor.prototype.set, 'function');
assert.equal(typeof Ctor.prototype.get, 'function');
assert.equal(typeof Ctor.prototype.del, 'function');
assert.equal(typeof Ctor.prototype.visit, 'function');
assert.equal(typeof Ctor.prototype.define, 'function');
assert.equal(typeof Ctor.prototype.mixin, 'function');
function foo() {}
Ctor.extend(foo);
assert.equal(typeof foo.prototype.set, 'function');
});
});
describe('instance properties', function() {
beforeEach(function() {
var Ctor = require('./');
Base = Ctor.namespace();
base = new Base();
});
it('should expose the options property:', function() {
assert(base.options);
assert.equal(typeof base.options, 'object');
});
it('should expose the cache property:', function() {
assert(base.cache);
assert.equal(typeof base.cache, 'object');
});
});
describe('prototype methods', function() {
beforeEach(function() {
var Ctor = require('./');
Base = Ctor.namespace();
base = new Base();
});
describe('.use', function() {
beforeEach(function() {
base = new Base();
});
it('should expose the use method:', function() {
assert(base.use);
assert.equal(typeof base.use, 'function');
});
it('should call the function passed to `use`:', function(cb) {
base.use(function(app) {
assert(app);
cb();
});
});
it('should expose the app instance:', function(cb) {
base.foo = 'bar';
base.use(function(app) {
assert.equal(app.foo, 'bar');
cb();
});
});
it('should expose the app instance as "this":', function(cb) {
base.foo = 'bar';
base.use(function(app) {
assert.equal(this.foo, 'bar');
cb();
});
});
});
describe('when `isRegistered` is used', function() {
it('should not call a plugin more than once on the same instance', function() {
base.i = 0;
function plugin(app) {
if (app.isRegistered('foo')) return;
base.i++;
}
base.use(plugin);
base.use(plugin);
base.use(plugin);
base.use(plugin);
assert.equal(base.i, 1);
});
it('should not register a plugin when `false` is passed as the 2nd arg', function() {
function plugin(app) {
if (app.isRegistered('foo', false)) return;
this.foo = 'bar';
}
base.use(plugin);
assert(!base.registered.hasOwnProperty('foo'));
assert.equal(base.foo, 'bar');
});
it('should not call a plugin more than once on the same instance', function() {
base.i = 0;
function plugin(app) {
if (app.isRegistered('foo')) return;
base.i++;
}
base.use(plugin);
base.use(plugin);
base.use(plugin);
base.use(plugin);
assert.equal(base.i, 1);
});
});
describe('.define', function() {
it('should define a key-value pair on the instance:', function() {
base.define('foo', 'bar');
assert.equal(base.foo, 'bar');
});
it('should define an own property', function() {
base.define('foo', 'bar');
assert(base.hasOwnProperty('foo'));
});
it('should define a non-emumerable property', function() {
base.define('foo', 'bar');
assert(Object.keys(base).indexOf('foo') === -1);
});
it('should multiple properties', function() {
base.define({
foo: 'bar',
baz: 'qux'
});
assert(base.hasOwnProperty('foo'));
assert(base.hasOwnProperty('baz'));
});
});
describe('.set', function() {
it('should set a key-value pair on the instance:', function() {
base.set('foo', 'bar');
assert.equal(base.foo, 'bar');
});
it('should set nested property:', function() {
base.set('a.b.c', 'd');
assert.equal(base.a.b.c, 'd');
});
it('should set a nested property with the key as an array:', function() {
base.set(['a', 'b', 'c'], 'd');
assert.equal(base.a.b.c, 'd');
});
it('should set an object on the instance:', function() {
base.set({a: 'b'});
assert.equal(base.a, 'b');
});
});
describe('.get', function() {
it('should get a property from the instance:', function() {
base.set({a: 'b'});
assert.equal(base.get('a'), 'b');
});
it('should get a nested property from the instance:', function() {
base.set({a: {b: {c: 'd'}}});
assert.equal(base.get('a.b.c'), 'd');
});
it('should get a property using an array:', function() {
base.set({a: {b: {c: 'd'}}});
assert.equal(base.get(['a', 'b', 'c']), 'd');
});
it('should get a property using a list of arguments', function() {
base.set({a: {b: {c: 'd'}}});
assert.equal(base.get('a', 'b', 'c'), 'd');
assert.equal(base.get(['a', 'b'], 'c'), 'd');
assert.equal(base.get('a', ['b', 'c']), 'd');
assert.equal(base.get('a', 'b.c'), 'd');
});
});
describe('.has', function() {
it('should work with namespaces:', function() {
var Ctor = require('./');
Base = Ctor.namespace('cache');
var foo = new Base();
foo.set({a: 'b'});
assert.equal(foo.has('a'), true);
});
it('should check for a property from the instance:', function() {
base.set({a: 'b'});
assert.equal(base.has('a'), true);
});
it('should check for a nested property from the instance:', function() {
base.set({a: {b: {c: 'd'}}});
assert.equal(base.has('a.b.c'), true);
});
it('should check for a property using an array:', function() {
base.set({a: {b: {c: 'd'}}});
assert.equal(base.has(['a', 'b', 'c']), true);
});
it('should check for a property using a list of arguments', function() {
base.set({a: {b: {c: 'd'}}});
assert.equal(base.has('a', 'b', 'c'), true);
assert.equal(base.has(['a', 'b'], 'c'), true);
assert.equal(base.has('a', ['b', 'c']), true);
assert.equal(base.has('a', 'b.c'), true);
});
});
describe('.visit', function() {
it('should visit an object with the given method:', function() {
base.visit('set', {a: 'b', c: 'd'});
assert.equal(base.get('a'), 'b');
assert.equal(base.get('c'), 'd');
});
it('should visit an array with the given method:', function() {
base.visit('set', [{a: 'b', c: 'd'}]);
assert.equal(base.get('a'), 'b');
assert.equal(base.get('c'), 'd');
});
});
describe('.del', function() {
it('should remove a property:', function() {
base.set({a: 'b'});
assert.equal(base.a, 'b');
base.del('a');
assert.equal(typeof base.a, 'undefined');
});
it('should remove an array of properties:', function() {
base.set({
a: 'a'
});
base.set({
b: 'b'
});
assert.equal(base.a, 'a');
assert.equal(base.b, 'b');
base.del(['a', 'b']);
assert.equal(typeof base.a, 'undefined');
assert.equal(typeof base.b, 'undefined');
});
});
});
describe('.mixin', function() {
beforeEach(function() {
var Ctor = require('./');
Base = Ctor.namespace();
base = new Base();
});
it('should add a property to the base prototype:', function() {
base.mixin('a', function() {});
assert.equal(typeof base.a, 'function');
assert.equal(typeof Base.prototype.a, 'function');
});
it('should add to the prototype of an inheriting app:', function() {
function Foo() {
Base.call(this);
}
Base.extend(Foo);
var foo = new Foo();
foo.mixin('a', function() {});
assert.equal(typeof Foo.prototype.a, 'function');
assert.equal(typeof foo.a, 'function');
});
it('should add to inheriting app prototype:', function() {
function Foo() {
Base.call(this);
}
Base.extend(Foo);
var base = new Base();
var foo = new Foo();
base.mixin('abc', function() {});
foo.mixin('xyz', function() {});
assert.equal(typeof Base.prototype.abc, 'function');
assert.equal(typeof Foo.prototype.abc, 'function');
assert.equal(typeof base.abc, 'function');
assert.equal(typeof foo.abc, 'function');
assert(typeof Base.prototype.xyz !== 'function');
assert.equal(typeof Foo.prototype.xyz, 'function');
assert.equal(typeof foo.xyz, 'function');
assert(typeof base.xyz !== 'function');
});
it('should chain calls to mixin:', function() {
function Foo() {
Base.call(this);
}
Base.extend(Foo);
var base = new Base();
var foo = new Foo();
base.mixin('abc', function() {})
.mixin('def', function() {});
foo.mixin('xyz', function() {})
.mixin('uvw', function() {});
assert.equal(typeof Base.prototype.abc, 'function');
assert.equal(typeof Base.prototype.def, 'function');
assert.equal(typeof Foo.prototype.abc, 'function');
assert.equal(typeof Foo.prototype.def, 'function');
assert.equal(typeof base.abc, 'function');
assert.equal(typeof base.def, 'function');
assert.equal(typeof foo.abc, 'function');
assert.equal(typeof foo.def, 'function');
assert(typeof Base.prototype.xyz !== 'function');
assert(typeof Base.prototype.uvw !== 'function');
assert.equal(typeof Foo.prototype.xyz, 'function');
assert.equal(typeof Foo.prototype.uvw, 'function');
assert.equal(typeof foo.xyz, 'function');
assert.equal(typeof foo.uvw, 'function');
assert(typeof base.xyz !== 'function');
assert(typeof base.uvw !== 'function');
});
it('should not add to Base.prototype from an inheriting app:', function() {
function Foo() {
Base.call(this);
}
Base.extend(Foo);
var foo = new Foo();
var base = new Base();
foo.mixin('a', function() {});
// yes
assert.equal(typeof Foo.prototype.a, 'function');
assert.equal(typeof foo.a, 'function');
// no
assert(typeof Base.prototype.a !== 'function');
assert(typeof base.a !== 'function');
});
it('should NOT mixin from one inheriting prototype to another:', function() {
function Foo() { Base.call(this); }
Base.extend(Foo);
function Bar() { Base.call(this); }
Base.extend(Bar);
var foo = new Foo();
var bar = new Bar();
foo.mixin('a', function() {});
// yes
assert.equal(typeof Foo.prototype.a, 'function');
assert.equal(typeof foo.a, 'function');
// no
assert(typeof Bar.prototype.a !== 'function');
assert(typeof bar.a !== 'function');
});
it('should mixin from Base.prototype to all others:', function() {
function Foo() { Base.call(this); }
Base.extend(Foo);
function Bar() { Base.call(this); }
Base.extend(Bar);
var base = new Base();
var foo = new Foo();
var bar = new Bar();
base.mixin('xyz', function() {});
assert.equal(typeof Base.prototype.xyz, 'function');
assert.equal(typeof Foo.prototype.xyz, 'function');
assert.equal(typeof Bar.prototype.xyz, 'function');
assert.equal(typeof base.xyz, 'function');
assert.equal(typeof foo.xyz, 'function');
assert.equal(typeof bar.xyz, 'function');
});
});
describe('namespaces', function() {
beforeEach(function() {
Base = require('./');
});
describe('constructor', function() {
it('should expose `namespace`', function() {
assert.equal(typeof Base.namespace, 'function');
});
it('should extend the given Ctor with static methods:', function() {
var Foo = Base.namespace('cache');
function Ctor() {
Foo.call(this);
}
Foo.extend(Ctor);
assert.equal(typeof Ctor.extend, 'function');
function foo() {}
Ctor.extend(foo);
assert.equal(typeof foo.extend, 'function');
});
});
describe('prototype methods', function() {
beforeEach(function() {
var Custom = Base.namespace('cache');
base = new Custom();
});
describe('set', function() {
it('should set a key-value pair on the instance:', function() {
base.set('foo', 'bar');
assert.equal(base.cache.foo, 'bar');
});
it('should set an object on the instance:', function() {
base.set({
a: 'b'
});
assert.equal(base.cache.a, 'b');
});
});
describe('get', function() {
it('should get a property from the instance:', function() {
base.set({
a: 'b'
});
assert.equal(base.get('a'), 'b');
});
it('should visit an object with the given method:', function() {
base.visit('set', {
a: 'b',
c: 'd'
});
assert.equal(base.get('a'), 'b');
assert.equal(base.get('c'), 'd');
});
it('should visit an array with the given method:', function() {
base.visit('set', [{
a: 'b',
c: 'd'
}]);
assert.equal(base.get('a'), 'b');
assert.equal(base.get('c'), 'd');
});
});
describe('del', function() {
it('should remove a property:', function() {
base.set({
a: 'b'
});
assert.equal(base.cache.a, 'b');
base.del('a');
assert.equal(typeof base.cache.a, 'undefined');
});
it('should remove an array of properties:', function() {
base.set({
a: 'a'
});
base.set({
b: 'b'
});
assert.equal(base.cache.a, 'a');
assert.equal(base.cache.b, 'b');
base.del(['a', 'b']);
assert.equal(typeof base.cache.a, 'undefined');
assert.equal(typeof base.cache.b, 'undefined');
});
});
});
});
describe('.base', function() {
beforeEach(function() {
base = new Base();
});
it('should set a `base` property on the instance', function() {
assert(base.base);
assert.equal(typeof base.base, 'object');
});
it('should use `parent` to set app.base', function() {
var foo = new Base();
foo.abc = 'xyz';
var bar = new Base();
bar.parent = foo;
var baz = new Base();
baz.parent = bar;
assert(baz.base);
assert.equal(baz.base.abc, 'xyz');
});
});
describe('.is', function() {
beforeEach(function() {
var Ctor = require('./');
Base = Ctor.namespace();
base = new Base();
});
it('should set a name prefixed with `is` on the instance:', function() {
base.is('Foo');
assert(base.isFoo);
assert.equal(base.isFoo, true);
});
});
describe('events', function() {
beforeEach(function() {
base = new Base();
});
it('should emit and listen for events:', function(cb) {
base.on('foo', function(val) {
assert.equal(val, 'bar');
cb();
});
base.emit('foo', 'bar');
});
it('should emit set', function(cb) {
base.on('set', function(key, val) {
assert.equal(key, 'foo');
assert.equal(val, 'bar');
cb();
});
base.set('foo', 'bar');
});
it('should emit get', function(cb) {
base.on('get', function(key, val) {
assert.equal(key, 'foo');
assert.equal(val, 'bar');
cb();
});
base.set('foo', 'bar');
base.get('foo');
});
it('should emit has', function(cb) {
base.on('has', function(key, has) {
assert.equal(key, 'foo');
assert.equal(has, true);
cb();
});
base.set('foo', 'bar');
base.has('foo');
});
it('should emit del', function(cb) {
base.on('del', function(key, val) {
assert.equal(key, 'foo');
cb();
});
base.set('foo', 'bar');
base.del('foo');
});
});
|