Comparing version 1.0.0-alpha-3 to 1.0.0-alpha-4
28
index.js
@@ -321,5 +321,13 @@ 'use strict'; | ||
/** | ||
* Default character set, UTF-8. | ||
* | ||
* @type {String} | ||
* @private | ||
*/ | ||
Pagelet.writable('_charset', 'UTF-8'); | ||
/** | ||
* Default content type of the Pagelet. | ||
* | ||
* @type {Object} | ||
* @type {String} | ||
* @private | ||
@@ -673,3 +681,3 @@ */ | ||
Pagelet.set('contentType', function get() { | ||
return this._contentType +';charset='+ this.charset; | ||
return this._contentType +';charset='+ this._charset; | ||
}, function set(value) { | ||
@@ -689,3 +697,3 @@ return this._contentType = value; | ||
}, function set(value) { | ||
return this._bootstrap = value; | ||
if (value instanceof Pagelet) return this._bootstrap = value; | ||
}); | ||
@@ -702,3 +710,3 @@ | ||
return 'function' !== typeof this.if // No conditional check needed. | ||
|| this._active && this._active !== null; // Conditional check has been done. | ||
|| this._active !== null && this._active; // Conditional check has been done. | ||
}, function set(value) { | ||
@@ -868,3 +876,3 @@ return this._active = !!value; | ||
* @param {Request} req The HTTP request. | ||
* @param {Function} list Array of possible alternate pagelets that take it's place. | ||
* @param {Function} list Array of optional alternate pagelets that take it's place. | ||
* @param {Function} fn The authorized callback. | ||
@@ -884,2 +892,4 @@ * @returns {Pagelet} | ||
* Callback for the `pagelet.if` function to see if we're enabled or disabled. | ||
* Use cached value in _active to prevent the same Pagelet being authorized | ||
* multiple times for the same request. | ||
* | ||
@@ -890,6 +900,6 @@ * @param {Boolean} value Are we enabled or disabled. | ||
function enabled(value) { | ||
fn.call(pagelet, pagelet.active = value); | ||
fn.call(pagelet, pagelet.active = value || false); | ||
} | ||
if ('boolean' === typeof this._active) { | ||
if ('boolean' === typeof pagelet._active) { | ||
fn(pagelet.active); | ||
@@ -900,3 +910,3 @@ } else if ('function' !== typeof this.if) { | ||
if (pagelet.if.length === 2) pagelet.if(req, enabled); | ||
else pagelet.if(req, list || [], enabled); | ||
else pagelet.if(req, list, enabled); | ||
} | ||
@@ -914,3 +924,3 @@ | ||
Pagelet.readable('destroy', destroy([ | ||
'_temper', '_bigpipe', '_enabled', '_disabled', '_pagelets' | ||
'_temper', '_bigpipe', '_enabled', '_disabled', '_children' | ||
], { | ||
@@ -917,0 +927,0 @@ after: 'removeAllListeners' |
{ | ||
"name": "pagelet", | ||
"version": "1.0.0-alpha-3", | ||
"version": "1.0.0-alpha-4", | ||
"description": "pagelet", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
describe('Pagelet', function () { | ||
'use strict'; | ||
var Pagelet = require('../').extend({ name: 'test' }) | ||
var Pagelet = require('../') | ||
, Temper = require('temper') | ||
, Pipe = require('bigpipe') | ||
, BigPipe = require('bigpipe') | ||
, assume = require('assume') | ||
@@ -17,7 +17,13 @@ , React = require('react') | ||
var temper = new Temper | ||
, bigpipe = new Pipe(server); | ||
, bigpipe = new BigPipe(server); | ||
// | ||
// Stub for no operation callbacks. | ||
// | ||
function noop() {} | ||
beforeEach(function () { | ||
P = Pagelet.extend({ | ||
directory: __dirname, | ||
error: 'fixtures/error.html', | ||
view: 'fixtures/view.html', | ||
@@ -62,3 +68,3 @@ css: 'fixtures/style.css', | ||
assume(pagelet._bigpipe).to.be.an('object'); | ||
assume(pagelet._bigpipe).to.be.instanceof(Pipe); | ||
assume(pagelet._bigpipe).to.be.instanceof(BigPipe); | ||
assume(property.writable).to.equal(true); | ||
@@ -70,3 +76,8 @@ assume(property.enumerable).to.equal(false); | ||
describe('.on', function () { | ||
it('sets the pathname', function () { | ||
it('is a function', function () { | ||
assume(Pagelet.on).is.a('function'); | ||
assume(Pagelet.on.length).to.equal(1); | ||
}); | ||
it('sets the directory property to dirname', function () { | ||
var pagelet = Pagelet.extend({}); | ||
@@ -91,5 +102,28 @@ assume(pagelet.prototype.directory).to.equal(''); | ||
it('resolves the `error` view'); | ||
it('resolves the `error` view', function () { | ||
assume(P.prototype.error).to.equal('fixtures/error.html'); | ||
P.on(module); | ||
assume(P.prototype.error).to.equal(__dirname +'/fixtures/error.html'); | ||
}); | ||
}); | ||
describe('.destroy', function () { | ||
it('is a function', function () { | ||
assume(pagelet.destroy).to.be.a('function'); | ||
assume(pagelet.destroy.length).to.equal(0); | ||
}); | ||
it('cleans object references from the Pagelet instance', function () { | ||
var local = new Pagelet({ temper: temper, bigpipe: bigpipe }); | ||
local.on('test', noop) | ||
local.destroy(); | ||
assume(local).to.have.property('_temper', null); | ||
assume(local).to.have.property('_bigpipe', null); | ||
assume(local).to.have.property('_children', null); | ||
assume(local).to.have.property('_events', null); | ||
}); | ||
}); | ||
describe('.discover', function () { | ||
@@ -118,3 +152,15 @@ it('emits discover and returns immediatly if the parent pagelet has no children', function (done) { | ||
describe('.length', function () { | ||
it('is a getter that returns the childrens length', function () { | ||
it('is a getter', function () { | ||
var props = Object.getOwnPropertyDescriptor(Pagelet.prototype, 'length'); | ||
assume(Pagelet.prototype).to.have.property('length'); | ||
assume(props).to.have.property('get'); | ||
assume(props.get).to.be.a('function'); | ||
assume(props).to.have.property('set', void 0); | ||
assume(props).to.have.property('enumerable', false); | ||
assume(props).to.have.property('configurable', false); | ||
}); | ||
it('returns the childrens length', function () { | ||
pagelet._children = [ 1, 2, 3 ]; | ||
@@ -166,4 +212,246 @@ assume(pagelet.length).to.equal(3); | ||
}); | ||
it('provides empty object as fallback for data', function() { | ||
var result = new (P.extend().on(module))({ temper: temper }).template(); | ||
assume(result).to.be.a('string'); | ||
assume(result).to.equal('<h1>Some {test} fixture</h1>'); | ||
}); | ||
}); | ||
describe('.contentType', function () { | ||
it('is a getter', function () { | ||
var props = Object.getOwnPropertyDescriptor(Pagelet.prototype, 'contentType'); | ||
assume(Pagelet.prototype).to.have.property('contentType'); | ||
assume(props).to.have.property('get'); | ||
assume(props.get).to.be.a('function'); | ||
assume(props).to.have.property('enumerable', false); | ||
assume(props).to.have.property('configurable', false); | ||
}); | ||
it('is a setter', function () { | ||
var props = Object.getOwnPropertyDescriptor(Pagelet.prototype, 'contentType'); | ||
assume(Pagelet.prototype).to.have.property('contentType'); | ||
assume(props).to.have.property('set'); | ||
assume(props.get).to.be.a('function'); | ||
assume(props).to.have.property('enumerable', false); | ||
assume(props).to.have.property('configurable', false); | ||
}); | ||
it('sets the Content-Type', function () { | ||
pagelet.contentType = 'application/test'; | ||
assume(pagelet._contentType).to.equal('application/test'); | ||
}); | ||
it('returns the Content-Type of the pagelet appended with the charset', function () { | ||
assume(pagelet.contentType).to.equal('text/html;charset=UTF-8'); | ||
pagelet._contentType = 'application/test'; | ||
assume(pagelet.contentType).to.equal('application/test;charset=UTF-8'); | ||
pagelet._charset = 'UTF-7'; | ||
assume(pagelet.contentType).to.equal('application/test;charset=UTF-7'); | ||
}); | ||
}); | ||
describe('.bootstrap', function () { | ||
it('is a getter', function () { | ||
var props = Object.getOwnPropertyDescriptor(Pagelet.prototype, 'bootstrap'); | ||
assume(Pagelet.prototype).to.have.property('bootstrap'); | ||
assume(props).to.have.property('get'); | ||
assume(props.get).to.be.a('function'); | ||
assume(props).to.have.property('enumerable', false); | ||
assume(props).to.have.property('configurable', false); | ||
}); | ||
it('is a setter', function () { | ||
var props = Object.getOwnPropertyDescriptor(Pagelet.prototype, 'bootstrap'); | ||
assume(Pagelet.prototype).to.have.property('bootstrap'); | ||
assume(props).to.have.property('set'); | ||
assume(props.get).to.be.a('function'); | ||
assume(props).to.have.property('enumerable', false); | ||
assume(props).to.have.property('configurable', false); | ||
}); | ||
it('sets a reference to a bootstrap pagelet', function () { | ||
var bootstrap = new Pagelet; | ||
pagelet.bootstrap = bootstrap; | ||
assume(pagelet._bootstrap).to.equal(bootstrap); | ||
}); | ||
it('only accepts pagelet instances', function () { | ||
pagelet.bootstrap = 'will not be set'; | ||
assume(pagelet._bootstrap).to.equal(void 0); | ||
}); | ||
it('returns a reference to the bootstrap pagelet or empty object', function () { | ||
assume(Object.keys(pagelet.bootstrap).length).to.equal(0); | ||
assume(pagelet.bootstrap.name).to.equal(void 0); | ||
var bootstrap = new Pagelet({ name: 'bootstrap' }); | ||
pagelet.bootstrap = bootstrap; | ||
assume(pagelet.bootstrap).to.equal(bootstrap); | ||
}); | ||
it('returns a reference to self if it is a boostrap pagelet', function () { | ||
var bootstrap = new (Pagelet.extend({ name: 'bootstrap' })); | ||
assume(bootstrap.bootstrap).to.equal(bootstrap); | ||
}); | ||
}); | ||
describe('.active', function () { | ||
it('is a getter', function () { | ||
var props = Object.getOwnPropertyDescriptor(Pagelet.prototype, 'active'); | ||
assume(Pagelet.prototype).to.have.property('active'); | ||
assume(props).to.have.property('get'); | ||
assume(props.get).to.be.a('function'); | ||
assume(props).to.have.property('enumerable', false); | ||
assume(props).to.have.property('configurable', false); | ||
}); | ||
it('is a setter', function () { | ||
var props = Object.getOwnPropertyDescriptor(Pagelet.prototype, 'active'); | ||
assume(Pagelet.prototype).to.have.property('active'); | ||
assume(props).to.have.property('set'); | ||
assume(props.get).to.be.a('function'); | ||
assume(props).to.have.property('enumerable', false); | ||
assume(props).to.have.property('configurable', false); | ||
}); | ||
it('sets the provided value to _active as boolean', function () { | ||
pagelet.active = 'true'; | ||
assume(pagelet._active).to.equal(true); | ||
pagelet.active = false; | ||
assume(pagelet._active).to.equal(false); | ||
}); | ||
it('returns true if no conditional method is available', function () { | ||
assume(pagelet.active).to.equal(true); | ||
pagelet._active = false; | ||
assume(pagelet.active).to.equal(true); | ||
}); | ||
it('returns the boolean value of _active if a conditional method is available', function () { | ||
var Conditional = P.extend({ if: noop }) | ||
, conditional = new Conditional; | ||
conditional._active = true; | ||
assume(conditional.active).to.equal(true); | ||
conditional._active = null; | ||
assume(conditional.active).to.equal(false); | ||
conditional._active = false; | ||
assume(conditional.active).to.equal(false); | ||
}); | ||
}); | ||
describe('.conditional', function () { | ||
it('is a function', function () { | ||
assume(pagelet.conditional).to.be.a('function'); | ||
assume(pagelet.conditional.length).to.equal(3); | ||
}); | ||
it('has an optional list argument for alternate pagelets', function (done) { | ||
pagelet.conditional({}, function (authorized) { | ||
assume(authorized).to.equal(true); | ||
done(); | ||
}); | ||
}); | ||
it('will use cached boolean value of authenticate', function (done) { | ||
var Conditional = P.extend({ | ||
if: function stubAuth(req, enabled) { | ||
assume(enabled).to.be.a('function'); | ||
enabled(req.test === 'stubbed req'); | ||
} | ||
}), conditional; | ||
var conditional = new Conditional; | ||
conditional._active = false; | ||
conditional.conditional({}, function (authorized) { | ||
assume(authorized).to.equal(false); | ||
conditional._active = 'invalid boolean'; | ||
conditional.conditional({}, function (authorized) { | ||
assume(authorized).to.equal(false); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('will authorize if no authorization method is provided', function (done) { | ||
pagelet.conditional({}, [], function (authorized) { | ||
assume(authorized).to.equal(true); | ||
assume(pagelet._active).to.equal(true); | ||
done(); | ||
}); | ||
}); | ||
it('will call authorization method without conditional pagelets', function (done) { | ||
var Conditional = P.extend({ | ||
if: function stubAuth(req, enabled) { | ||
assume(enabled).to.be.a('function'); | ||
enabled(req.test === 'stubbed req'); | ||
} | ||
}); | ||
new Conditional().conditional({ test: 'stubbed req' }, function (auth) { | ||
assume(auth).to.equal(true); | ||
done(); | ||
}); | ||
}); | ||
it('will call authorization method with conditional pagelets', function (done) { | ||
var Conditional = P.extend({ | ||
if: function stubAuth(req, list, enabled) { | ||
assume(list).to.be.an('array'); | ||
assume(list.length).to.equal(1); | ||
assume(list[0]).to.be.instanceof(Pagelet); | ||
assume(enabled).to.be.a('function'); | ||
enabled(req.test !== 'stubbed req'); | ||
} | ||
}); | ||
new Conditional().conditional({ test: 'stubbed req' }, [pagelet], function (auth) { | ||
assume(auth).to.equal(false); | ||
done(); | ||
}); | ||
}); | ||
it('will default to not authorized if no value is provided to the callback', function (done) { | ||
var Conditional = P.extend({ | ||
if: function stubAuth(req, list, enabled) { | ||
assume(list).to.be.an('array'); | ||
assume(list.length).to.equal(0); | ||
assume(enabled).to.be.a('function'); | ||
enabled(); | ||
} | ||
}); | ||
new Conditional().conditional({ test: 'stubbed req' }, function (auth) { | ||
assume(auth).to.equal(false); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('.children', function () { | ||
@@ -170,0 +458,0 @@ it('is a function', function () { |
Sorry, the diff of this file is not supported yet
72843
16
1541