UNPKG

base-test-suite

Version:
247 lines (208 loc) 8.07 kB
'use strict'; var fs = require('fs'); var path = require('path'); var assert = require('assert'); var fixtures = path.resolve.bind(path, __dirname, 'fixtures'); var support = require('../../support'); var hasProperties = support.hasProperties; module.exports = function(App, options, runner) { var app; describe('app.create', function() { describe('inflections', function() { beforeEach(function() { app = new App(); }); it('should expose the create method', function() { assert.equal(typeof app.create, 'function'); }); it('should add a collection to `views`', function() { app.create('pages'); assert.equal(typeof app.views.pages, 'object'); assert.equal(typeof app.pages, 'function'); }); it('should add a pluralized collection to `views`', function() { app.create('page'); assert.equal(typeof app.views.pages, 'object'); assert.equal(typeof app.page, 'function'); }); }); describe('renderable views', function() { beforeEach(function() { app = new App(); app.create('pages'); app.create('partials', {viewType: 'partial'}); app.create('layout', {viewType: 'layout'}); }); it('should add renderable views when no type is defined', function() { app.pages.addView('foo', {content: 'bar'}); assert(app.views.pages.hasOwnProperty('foo')); }); it('should add view collection name to view._name', function() { app.pages.addView('foo', {content: 'bar'}); assert.equal(app.views.pages.foo._name, 'page'); }); it('should add partial views when partial type is defined', function() { app.partials.addView('abc', {content: 'xyz'}); assert(app.views.partials.hasOwnProperty('abc')); }); it('should add layout views when layout type is defined', function() { app.layouts.addView('foo', {content: 'bar'}); assert(app.views.layouts.hasOwnProperty('foo')); }); it('should set viewType on renderable views', function() { app.pages.addView('foo', {content: 'bar'}); var view = app.pages.getView('foo'); assert(view.isType('renderable')); assert(!view.isType('layout')); assert(!view.isType('partial')); }); it('should set viewType on partial views', function() { app.partials.addView('foo', {content: 'bar'}); var view = app.partials.getView('foo'); assert(view.isType('partial')); assert(!view.isType('layout')); assert(!view.isType('renderable')); }); it('should set viewType on layout views', function() { app.layouts.addView('foo', {content: 'bar'}); var view = app.layouts.getView('foo'); assert(view.isType('layout')); assert(!view.isType('renderable')); assert(!view.isType('partial')); }); }); describe('custom constructors', function() { beforeEach(function() { var Vinyl = require('vinyl'); Vinyl.prototype.custom = function(key) { this[key] = 'nonsense'; return this; }; app = new App({View: Vinyl}); app.create('pages'); }); it('should create views from key-value pairs:', function() { app.page('a.hbs', {path: 'a.hbs', content: 'a'}); app.page('b.hbs', {path: 'b.hbs', content: 'b'}); app.page('c.hbs', {path: 'c.hbs', content: 'c'}); var a = app.pages.getView('a.hbs'); a.custom('foo'); assert.equal(a.foo, 'nonsense'); }); }); describe('custom instances', function() { it('should create views from custom `View` and `Views` instance/ctor:', function() { var Vinyl = require('vinyl'); Vinyl.prototype.read = function(file) { return fs.readFileSync(file.path); }; var Views = App.Views; var views = new Views({View: Vinyl}); views.addView('a.hbs', {path: 'a.hbs', content: 'a'}); views.addView('b.hbs', {path: 'b.hbs', content: 'b'}); views.addView('c.hbs', {path: 'c.hbs', content: 'c'}); app = new App(); app.create('pages', views); var a = app.pages.getView('a.hbs'); assert(a instanceof Vinyl); assert(Vinyl.isVinyl(a)); assert.equal(typeof a.read, 'function'); views.addView('d.hbs', {path: 'd.hbs', content: 'd'}); var d = app.pages.getView('d.hbs'); assert(d instanceof Vinyl); assert(Vinyl.isVinyl(d)); }); }); describe('chaining', function() { beforeEach(function() { app = new App(); app.engine('tmpl', require('engine-base')); app.create('page'); }); it('should create views from key-value pairs:', function() { app.page('a.hbs', {content: 'a'}); app.page('b.hbs', {content: 'b'}); app.page('c.hbs', {content: 'c'}); hasProperties(app.views.pages, ['a.hbs', 'b.hbs', 'c.hbs']); assert.equal(app.views.pages['a.hbs'].contents.toString(), 'a'); }); it('should create views from file paths:', function() { app.page(fixtures('pages/a.hbs')); app.page(fixtures('pages/b.hbs')); app.page(fixtures('pages/c.hbs')); hasProperties(app.views.pages, [ fixtures('pages/a.hbs'), fixtures('pages/b.hbs'), fixtures('pages/c.hbs') ]); }); }); describe('instance', function() { beforeEach(function() { app = new App(); app.engine('tmpl', require('engine-base')); }); it('should return the collection instance', function() { var collection = app.create('pages'); assert(collection instanceof App.Views); collection.option('renameKey', function(key) { return path.basename(key); }); collection .use(function(views) { views.read = function(name) { var view = this.getView(name); if (!view.contents || view.contents.length === 0) { view.contents = fs.readFileSync(view.path); } }; }); collection.addView(fixtures('templates/a.tmpl')); collection.read('a.tmpl'); assert.equal(collection.getView('a.tmpl').contents.toString(), '<%= name %>'); }); }); describe('viewType', function() { beforeEach(function() { app = new App(); app.engine('tmpl', require('engine-base')); }); it('should add collection to the given viewType', function() { app.create('layout', {viewType: 'layout'}); assert.equal(app.layouts.options.viewType[0], 'layout'); }); it('should add a collection to multiple viewTypes', function() { app.create('foo', {viewType: ['layout', 'renderable']}); assert.deepEqual(app.foos.options.viewType, ['layout', 'renderable']); }); }); describe('events', function() { beforeEach(function() { app = new App(); app.engine('tmpl', require('engine-base')); }); it('should emit `create` when a collection is created:', function() { app.on('postCreate', function(collection) { if (collection.options.plural === 'layouts') { collection.options.foo = 'bar'; } }); app.create('layout'); app.layout('one', {path: 'two', contents: '...'}); assert.equal(app.layouts.options.foo, 'bar'); }); }); describe('collection instantiation', function() { it('should expose collection instance methods that are created after instantiation on the app collection loader', function() { app.create('pages'); app.pages.use(function(collection) { collection.define('foo', function(msg) { return 'foo ' + msg; }); }); assert(app.pages.foo); assert.equal(typeof app.pages.foo, 'function'); }); }); }); };