diff --git a/Gruntfile.coffee b/Gruntfile.coffee index adabbe25..a5c5e46a 100644 --- a/Gruntfile.coffee +++ b/Gruntfile.coffee @@ -12,33 +12,8 @@ banner = """ * For all details and documentation: * http://chaplinjs.org */ - """ -umdHead = ''' -(function(root, factory) { - if (typeof define === 'function' && define.amd) { - define(['backbone', 'underscore'], factory); - } else if (typeof module === 'object' && module && module.exports) { - module.exports = factory(require('backbone'), require('underscore')); - } else if (typeof require === 'function') { - factory(window.Backbone, window._ || window.Backbone.utils); - } else { - throw new Error('Chaplin requires Common.js or AMD modules'); - } -}(this, function(Backbone, _) { - function require(name) { - return {backbone: Backbone, underscore: _}[name]; - } - - require = -''' - -umdTail = ''' - return require(1); -})) -''' - setupJSDOM = -> require('jsdom-global')(undefined, url: 'https://github.com' @@ -72,7 +47,6 @@ module.exports = (grunt) -> reporter: 'spec' require: [ 'coffee-script/register' - 'coffee-coverage/register-istanbul' setupJSDOM -> require.cache[require.resolve 'jquery'] = {} 'backbone.nativeview' @@ -90,37 +64,33 @@ module.exports = (grunt) -> ] src: 'test/*.coffee' - makeReport: - src: 'coverage/coverage-coffee.json', - options: - type: 'html' - dir: 'coverage' + rollup: + options: { + plugins: [ + require('rollup-plugin-coffee-script')() + require('rollup-plugin-node-resolve')(extensions: ['.coffee']) + ] + external: [ + 'underscore' + 'backbone' + ] + globals: + underscore: '_' + backbone: 'Backbone' + format: 'umd' + moduleName: 'Chaplin' + intro: '_ = _ || Backbone.utils;' # support exoskeleton + banner + } - browserify: dist: files: - 'build/chaplin.js': ['./src/chaplin.coffee'] - options: { - banner - external: ['backbone', 'underscore'] - transform: ['coffeeify'] - browserifyOptions: - debug: true - extensions: ['.coffee'] - postBundleCB: (err, src, next) -> - if err - next err - else - src = umdHead + src + umdTail - next null, new Buffer src - } + 'build/chaplin.js': 'src/chaplin.coffee' # Minify # ====== uglify: - options: - mangle: true - universal: + dist: files: 'build/chaplin.min.js': 'build/chaplin.js' @@ -234,8 +204,8 @@ module.exports = (grunt) -> grunt.log.ok() grunt.registerTask 'check:versions', [ - 'check:versions:component', - 'check:versions:changelog', + 'check:versions:component' + 'check:versions:changelog' 'check:versions:docs' ] @@ -312,16 +282,12 @@ module.exports = (grunt) -> # Tests # ===== grunt.registerTask 'lint', 'coffeelint' - grunt.registerTask 'test', 'mochaTest:native' - grunt.registerTask 'test:jquery', 'mochaTest:jquery' - - # Coverage - # ======== - grunt.registerTask 'coverage', ['mochaTest:native', 'makeReport'] + grunt.registerTask 'test', ['rollup', 'mochaTest:native'] + grunt.registerTask 'test:jquery', ['rollup', 'mochaTest:jquery'] # Building # ======== - grunt.registerTask 'build', ['browserify', 'uglify', 'compress'] + grunt.registerTask 'build', ['rollup', 'uglify', 'compress'] # Default # ======= diff --git a/package.json b/package.json index 4db22e82..8bdba123 100644 --- a/package.json +++ b/package.json @@ -10,26 +10,25 @@ "devDependencies": { "backbone.nativeview": "~0.3.3", "chai": "~4.1.1", - "coffee-coverage": "1.0.1", "coffee-script": "~1.12.7", - "coffeeify": "~2.0.1", - "grunt": "~0.4.5", - "grunt-browserify": "~4.0.1", - "grunt-cli": "~0.1.13", - "grunt-coffeelint": "~0.0.15", - "grunt-contrib-compress": "0.14.x", - "grunt-contrib-uglify": "~0.11.1", - "grunt-contrib-watch": "~0.6.1", - "grunt-istanbul": "0.7.0", + "grunt": "~1.0.1", + "grunt-cli": "~1.2.0", + "grunt-coffeelint": "~0.0.16", + "grunt-contrib-compress": "~1.4.3", + "grunt-contrib-uglify": "~3.0.1", + "grunt-contrib-watch": "~1.0.0", "grunt-mocha-test": "~0.13.2", - "grunt-transbrute": "0.2.x", - "jquery": "2.2.x", - "jsdom": "~11.1.0", + "grunt-rollup": "~5.0.0", + "grunt-transbrute": "~1.0.1", + "jquery": "~2.2.0", + "jsdom": "~11.2.0", "jsdom-global": "~3.0.2", "mocha": "~3.5.0", "prompt": "~0.2.14", - "sinon": "~2.3.6", - "sinon-chai": "~2.12.0" + "rollup-plugin-coffee-script": "~1.1.0", + "rollup-plugin-node-resolve": "~3.0.0", + "sinon": "~3.2.1", + "sinon-chai": "~2.13.0" }, "main": "build/chaplin.js", "scripts": { diff --git a/src/chaplin.coffee b/src/chaplin.coffee index 363dbdcf..19527fc2 100644 --- a/src/chaplin.coffee +++ b/src/chaplin.coffee @@ -1,23 +1,41 @@ -'use strict' +import Application from './chaplin/application' +import Composer from './chaplin/composer' +import Controller from './chaplin/controllers/controller' +import Dispatcher from './chaplin/dispatcher' +import Composition from './chaplin/lib/composition' +import EventBroker from './chaplin/lib/event_broker' +import History from './chaplin/lib/history' +import Route from './chaplin/lib/route' +import Router from './chaplin/lib/router' +import support from './chaplin/lib/support' +import SyncMachine from './chaplin/lib/sync_machine' +import utils from './chaplin/lib/utils' +import mediator from './chaplin/mediator' +import Collection from './chaplin/models/collection' +import Model from './chaplin/models/model' +import CollectionView from './chaplin/views/collection_view' +import Layout from './chaplin/views/layout' +import View from './chaplin/views/view' # Main entry point into Chaplin module. # Load all components and expose them. -module.exports = - Application: require './chaplin/application' - Composer: require './chaplin/composer' - Controller: require './chaplin/controllers/controller' - Dispatcher: require './chaplin/dispatcher' - Composition: require './chaplin/lib/composition' - EventBroker: require './chaplin/lib/event_broker' - History: require './chaplin/lib/history' - Route: require './chaplin/lib/route' - Router: require './chaplin/lib/router' - support: require './chaplin/lib/support' - SyncMachine: require './chaplin/lib/sync_machine' - utils: require './chaplin/lib/utils' - mediator: require './chaplin/mediator' - Collection: require './chaplin/models/collection' - Model: require './chaplin/models/model' - CollectionView: require './chaplin/views/collection_view' - Layout: require './chaplin/views/layout' - View: require './chaplin/views/view' +export default { + Application + Composer + Controller + Dispatcher + Composition + EventBroker + History + Route + Router + support + SyncMachine + utils + mediator + Collection + Model + CollectionView + Layout + View +} diff --git a/src/chaplin/application.coffee b/src/chaplin/application.coffee index a639adb8..5fcf7f78 100644 --- a/src/chaplin/application.coffee +++ b/src/chaplin/application.coffee @@ -1,23 +1,21 @@ -'use strict' - # Third-party libraries. -_ = require 'underscore' -Backbone = require 'backbone' +import _ from 'underscore' +import Backbone from 'backbone' # CoffeeScript classes which are instantiated with `new` -Composer = require './composer' -Dispatcher = require './dispatcher' -Router = require './lib/router' -Layout = require './views/layout' +import Composer from './composer' +import Dispatcher from './dispatcher' +import Router from './lib/router' +import Layout from './views/layout' # A mix-in that should be mixed to class. -EventBroker = require './lib/event_broker' +import EventBroker from './lib/event_broker' # Independent global event bus that is used by itself, so lowercased. -mediator = require './mediator' +import mediator from './mediator' # The bootstrapper is the entry point for Chaplin apps. -module.exports = class Application +export default class Application # Borrow the `extend` method from a dear friend. @extend = Backbone.Model.extend @@ -130,6 +128,8 @@ module.exports = class Application # Seal the application instance to prevent further changes. Object.seal this + disposed: false + dispose: -> # Am I already disposed? return if @disposed diff --git a/src/chaplin/composer.coffee b/src/chaplin/composer.coffee index 5a92d069..4b582f75 100644 --- a/src/chaplin/composer.coffee +++ b/src/chaplin/composer.coffee @@ -1,11 +1,9 @@ -'use strict' +import _ from 'underscore' +import Backbone from 'backbone' -_ = require 'underscore' -Backbone = require 'backbone' - -Composition = require './lib/composition' -EventBroker = require './lib/event_broker' -mediator = require './mediator' +import Composition from './lib/composition' +import EventBroker from './lib/event_broker' +import mediator from './mediator' # Composer # -------- @@ -18,7 +16,7 @@ mediator = require './mediator' # is routed to where a view that was composed is not re-composed, the # composed view is disposed. -module.exports = class Composer +export default class Composer # Borrow the static extend method from Backbone @extend = Backbone.Model.extend diff --git a/src/chaplin/controllers/controller.coffee b/src/chaplin/controllers/controller.coffee index 4fcbf543..25756850 100644 --- a/src/chaplin/controllers/controller.coffee +++ b/src/chaplin/controllers/controller.coffee @@ -1,13 +1,11 @@ -'use strict' +import _ from 'underscore' +import Backbone from 'backbone' -_ = require 'underscore' -Backbone = require 'backbone' +import mediator from '../mediator' +import EventBroker from '../lib/event_broker' +import utils from '../lib/utils' -mediator = require '../mediator' -EventBroker = require '../lib/event_broker' -utils = require '../lib/utils' - -module.exports = class Controller +export default class Controller # Borrow the static extend method from Backbone. @extend = Backbone.Model.extend diff --git a/src/chaplin/dispatcher.coffee b/src/chaplin/dispatcher.coffee index 90b776e4..e6f6c318 100644 --- a/src/chaplin/dispatcher.coffee +++ b/src/chaplin/dispatcher.coffee @@ -1,13 +1,11 @@ -'use strict' +import _ from 'underscore' +import Backbone from 'backbone' -_ = require 'underscore' -Backbone = require 'backbone' +import EventBroker from './lib/event_broker' +import utils from './lib/utils' +import mediator from './mediator' -EventBroker = require './lib/event_broker' -utils = require './lib/utils' -mediator = require './mediator' - -module.exports = class Dispatcher +export default class Dispatcher # Borrow the static extend method from Backbone. @extend = Backbone.Model.extend diff --git a/src/chaplin/lib/composition.coffee b/src/chaplin/lib/composition.coffee index 13777df3..de0f7947 100644 --- a/src/chaplin/lib/composition.coffee +++ b/src/chaplin/lib/composition.coffee @@ -1,8 +1,7 @@ -'use strict' +import _ from 'underscore' +import Backbone from 'backbone' -_ = require 'underscore' -Backbone = require 'backbone' -EventBroker = require './event_broker' +import EventBroker from './event_broker' # Composition # ----------- @@ -11,7 +10,7 @@ EventBroker = require './event_broker' # controller that is used internally to inflate simple # calls to !composer:compose and may be extended and used to have complete # control over the composition process. -module.exports = class Composition +export default class Composition # Borrow the static extend method from Backbone. @extend = Backbone.Model.extend diff --git a/src/chaplin/lib/event_broker.coffee b/src/chaplin/lib/event_broker.coffee index 14cf2c16..10aba851 100644 --- a/src/chaplin/lib/event_broker.coffee +++ b/src/chaplin/lib/event_broker.coffee @@ -1,6 +1,4 @@ -'use strict' - -mediator = require '../mediator' +import mediator from '../mediator' # Add functionality to subscribe and publish to global # Publish/Subscribe events so they can be removed afterwards @@ -14,7 +12,7 @@ mediator = require '../mediator' # Since Backbone 0.9.2 this abstraction just serves the purpose # that a handler cannot be registered twice for the same event. -EventBroker = +export default Object.freeze subscribeEvent: (type, handler) -> if typeof type isnt 'string' throw new TypeError 'EventBroker#subscribeEvent: ' + @@ -66,9 +64,3 @@ EventBroker = # Publish global handler. mediator.publish type, args... - -# You’re frozen when your heart’s not open. -Object.freeze EventBroker - -# Return our creation. -module.exports = EventBroker diff --git a/src/chaplin/lib/history.coffee b/src/chaplin/lib/history.coffee index 3af188c0..af2a750f 100644 --- a/src/chaplin/lib/history.coffee +++ b/src/chaplin/lib/history.coffee @@ -1,7 +1,5 @@ -'use strict' - -_ = require 'underscore' -Backbone = require 'backbone' +import _ from 'underscore' +import Backbone from 'backbone' # Cached regex for stripping a leading hash/slash and trailing space. routeStripper = /^[#\/]|\s+$/g @@ -120,4 +118,4 @@ class History extends Backbone.History if options.trigger @loadUrl fragment -module.exports = if Backbone.$ then History else Backbone.History +export default (if Backbone.$ then History else Backbone.History) diff --git a/src/chaplin/lib/route.coffee b/src/chaplin/lib/route.coffee index b870a371..1a729c04 100644 --- a/src/chaplin/lib/route.coffee +++ b/src/chaplin/lib/route.coffee @@ -1,13 +1,11 @@ -'use strict' +import _ from 'underscore' +import Backbone from 'backbone' -_ = require 'underscore' -Backbone = require 'backbone' +import EventBroker from './event_broker' +import utils from './utils' +import Controller from '../controllers/controller' -EventBroker = require './event_broker' -utils = require './utils' -Controller = require '../controllers/controller' - -module.exports = class Route +export default class Route # Borrow the static extend method from Backbone. @extend = Backbone.Model.extend diff --git a/src/chaplin/lib/router.coffee b/src/chaplin/lib/router.coffee index c9024f6d..25f14e59 100644 --- a/src/chaplin/lib/router.coffee +++ b/src/chaplin/lib/router.coffee @@ -1,18 +1,16 @@ -'use strict' +import _ from 'underscore' +import Backbone from 'backbone' -_ = require 'underscore' -Backbone = require 'backbone' - -EventBroker = require './event_broker' -History = require './history' -Route = require './route' -utils = require './utils' -mediator = require '../mediator' +import EventBroker from './event_broker' +import History from './history' +import Route from './route' +import utils from './utils' +import mediator from '../mediator' # The router which is a replacement for Backbone.Router. # Like the standard router, it creates a Backbone.History # instance and registers routes on it. -module.exports = class Router # This class does not extend Backbone.Router. +export default class Router # This class does not extend Backbone.Router. # Borrow the static extend method from Backbone. @extend = Backbone.Model.extend diff --git a/src/chaplin/lib/support.coffee b/src/chaplin/lib/support.coffee index 82b22725..82f897a9 100644 --- a/src/chaplin/lib/support.coffee +++ b/src/chaplin/lib/support.coffee @@ -1,7 +1,3 @@ -'use strict' - -# Backwards-compatibility module -# ------------------------------ - -module.exports = - propertyDescriptors: yes \ No newline at end of file +export default { + propertyDescriptors: yes +} diff --git a/src/chaplin/lib/sync_machine.coffee b/src/chaplin/lib/sync_machine.coffee index a7615d2a..8570b1be 100644 --- a/src/chaplin/lib/sync_machine.coffee +++ b/src/chaplin/lib/sync_machine.coffee @@ -1,5 +1,3 @@ -'use strict' - # Simple finite state machine for synchronization of models/collections # Three states: unsynced, syncing and synced # Several transitions between them @@ -9,9 +7,8 @@ # (named after the events above) UNSYNCED = 'unsynced' -SYNCING = 'syncing' -SYNCED = 'synced' - +SYNCING = 'syncing' +SYNCED = 'synced' STATE_CHANGE = 'syncStateChange' SyncMachine = @@ -74,15 +71,10 @@ SyncMachine = # Create shortcut methods to bind a handler to a state change # ----------------------------------------------------------- - -for event in [UNSYNCED, SYNCING, SYNCED, STATE_CHANGE] - do (event) -> - SyncMachine[event] = (callback, context = this) -> - @on event, callback, context - callback.call(context) if @_syncState is event +[UNSYNCED, SYNCING, SYNCED, STATE_CHANGE].forEach (event) -> + SyncMachine[event] = (callback, context = this) -> + @on event, callback, context + callback.call(context) if @_syncState is event # You’re frozen when your heart’s not open. -Object.freeze SyncMachine - -# Return our creation. -module.exports = SyncMachine +export default Object.freeze SyncMachine diff --git a/src/chaplin/lib/utils.coffee b/src/chaplin/lib/utils.coffee index 5cf7897c..e9e0e61d 100644 --- a/src/chaplin/lib/utils.coffee +++ b/src/chaplin/lib/utils.coffee @@ -1,11 +1,8 @@ -'use strict' - -# Utilities -# --------- +import mediator from '../mediator' utils = isEmpty: (object) -> - not Object.getOwnPropertyNames(object).length + Object.getOwnPropertyNames(object).length is 0 # Simple duck-typing serializer for models and collections. serialize: (data) -> @@ -54,7 +51,7 @@ utils = # Escapes a string to use in a regex. escapeRegExp: (str) -> - return String(str or '').replace /([.*+?^=!:${}()|[\]\/\\])/g, '\\$1' + String(str or '').replace /([.*+?^=!:${}()|[\]\/\\])/g, '\\$1' # Event handling helpers @@ -69,23 +66,19 @@ utils = # Returns the url for a named route and any params. reverse: (criteria, params, query) -> - require('../mediator').execute 'router:reverse', - criteria, params, query + mediator.execute 'router:reverse', criteria, params, query # Redirects to URL, route name or controller and action pair. redirectTo: (pathDesc, params, options) -> - require('../mediator').execute 'router:route', - pathDesc, params, options + mediator.execute 'router:route', pathDesc, params, options # Determines module system and returns module loader function. loadModule: do -> - {define, require} = window - if typeof define is 'function' and define.amd (moduleName, handler) -> require [moduleName], handler else - enqueue = setImmediate ? setTimeout + enqueue = setImmediate or setTimeout (moduleName, handler) -> enqueue -> handler require moduleName @@ -152,11 +145,4 @@ utils.indexOf = (array, item) -> array.indexOf item utils.isArray = Array.isArray utils.queryParams = utils.querystring -# Finish -# ------ - -# Seal the utils object. -Object.seal utils - -# Return our creation. -module.exports = utils +export default Object.seal utils diff --git a/src/chaplin/mediator.coffee b/src/chaplin/mediator.coffee index 35f304ca..77d67843 100644 --- a/src/chaplin/mediator.coffee +++ b/src/chaplin/mediator.coffee @@ -1,7 +1,5 @@ -'use strict' - -Backbone = require 'backbone' -utils = require './lib/utils' +import Backbone from 'backbone' +import utils from './lib/utils' # Mediator # -------- @@ -42,7 +40,7 @@ handlers = mediator._handlers = {} # Sets a handler function for requests. mediator.setHandler = (name, method, instance) -> - handlers[name] = {instance, method} + handlers[name] = {method, instance} # Retrieves a handler function and executes it. mediator.execute = (options, args...) -> @@ -85,4 +83,4 @@ utils.readonly mediator, 'setHandler', 'execute', 'removeHandlers', 'seal' # Return our creation. -module.exports = mediator +export default mediator diff --git a/src/chaplin/models/collection.coffee b/src/chaplin/models/collection.coffee index b7792f2d..fe608438 100644 --- a/src/chaplin/models/collection.coffee +++ b/src/chaplin/models/collection.coffee @@ -1,15 +1,13 @@ -'use strict' +import _ from 'underscore' +import Backbone from 'backbone' -_ = require 'underscore' -Backbone = require 'backbone' - -Model = require './model' -EventBroker = require '../lib/event_broker' -utils = require '../lib/utils' +import Model from './model' +import EventBroker from '../lib/event_broker' +import utils from '../lib/utils' # Abstract class which extends the standard Backbone collection # in order to add some functionality. -module.exports = class Collection extends Backbone.Collection +export default class Collection extends Backbone.Collection # Mixin an EventBroker. _.extend @prototype, EventBroker @@ -47,9 +45,7 @@ module.exports = class Collection extends Backbone.Collection # Remove model constructor reference, internal model lists # and event handlers. delete this[prop] for prop in [ - 'model', - 'models', '_byCid', - '_callbacks' + 'model', 'models', '_byCid', '_callbacks' ] @_byId = {} diff --git a/src/chaplin/models/model.coffee b/src/chaplin/models/model.coffee index e8d62a5f..5b999e77 100644 --- a/src/chaplin/models/model.coffee +++ b/src/chaplin/models/model.coffee @@ -1,8 +1,6 @@ -'use strict' - -_ = require 'underscore' -Backbone = require 'backbone' -EventBroker = require '../lib/event_broker' +import _ from 'underscore' +import Backbone from 'backbone' +import EventBroker from '../lib/event_broker' # Private helper function for serializing attributes recursively, # creating objects which delegate to the original attributes @@ -54,7 +52,7 @@ serializeModelAttributes = (model, currentModel, modelStack) -> # Abstraction that adds some useful functionality to backbone model. -module.exports = class Model extends Backbone.Model +export default class Model extends Backbone.Model # Mixin an EventBroker. _.extend @prototype, EventBroker diff --git a/src/chaplin/views/collection_view.coffee b/src/chaplin/views/collection_view.coffee index 1b8c1b52..f728c489 100644 --- a/src/chaplin/views/collection_view.coffee +++ b/src/chaplin/views/collection_view.coffee @@ -1,9 +1,6 @@ -'use strict' - -Backbone = require 'backbone' - -View = require './view' -utils = require '../lib/utils' +import Backbone from 'backbone' +import View from './view' +import utils from '../lib/utils' # Shortcut to access the DOM manipulation library. {$} = Backbone @@ -106,7 +103,7 @@ insertView = do -> # Derive this class and declare at least `itemView` or override # `initItemView`. `initItemView` gets an item model and should instantiate # and return a corresponding item view. -module.exports = class CollectionView extends View +export default class CollectionView extends View # Configuration options # ===================== diff --git a/src/chaplin/views/layout.coffee b/src/chaplin/views/layout.coffee index 39efe20c..c636672a 100644 --- a/src/chaplin/views/layout.coffee +++ b/src/chaplin/views/layout.coffee @@ -1,17 +1,15 @@ -'use strict' +import _ from 'underscore' +import Backbone from 'backbone' -_ = require 'underscore' -Backbone = require 'backbone' - -View = require './view' -EventBroker = require '../lib/event_broker' -utils = require '../lib/utils' -mediator = require '../mediator' +import View from './view' +import EventBroker from '../lib/event_broker' +import utils from '../lib/utils' +import mediator from '../mediator' # Shortcut to access the DOM manipulation library. {$} = Backbone -module.exports = class Layout extends View +export default class Layout extends View # Bind to document body by default. el: 'body' diff --git a/src/chaplin/views/view.coffee b/src/chaplin/views/view.coffee index 3c456205..1e3737f3 100644 --- a/src/chaplin/views/view.coffee +++ b/src/chaplin/views/view.coffee @@ -1,11 +1,9 @@ -'use strict' +import _ from 'underscore' +import Backbone from 'backbone' -_ = require 'underscore' -Backbone = require 'backbone' - -EventBroker = require '../lib/event_broker' -utils = require '../lib/utils' -mediator = require '../mediator' +import EventBroker from '../lib/event_broker' +import utils from '../lib/utils' +import mediator from '../mediator' # Shortcut to access the DOM manipulation library. {$} = Backbone @@ -39,7 +37,7 @@ attach = do -> else actual[view.containerMethod] view.el -module.exports = class View extends Backbone.NativeView or Backbone.View +export default class View extends Backbone.NativeView or Backbone.View # Mixin an EventBroker. _.extend @prototype, EventBroker diff --git a/test/application_spec.coffee b/test/application_spec.coffee index 843247bd..cd1036f6 100644 --- a/test/application_spec.coffee +++ b/test/application_spec.coffee @@ -1,47 +1,46 @@ 'use strict' Backbone = require 'backbone' -{Application, Composer, Dispatcher} = require '../src/chaplin' -{EventBroker, Router, mediator, Layout} = require '../src/chaplin' +{Application, Composer, Dispatcher} = require '../build/chaplin' +{EventBroker, Router, mediator, Layout} = require '../build/chaplin' describe 'Application', -> app = null - getApp = (dontInit) -> - if dontInit - class extends Application then initialize: -> - else + getApp = (init) -> + if init Application + else + class extends Application + initialize: -> beforeEach -> - app = new (getApp yes) + app = new (getApp no) afterEach -> app.dispose() - it 'should be a simple object', -> - expect(app).to.be.an 'object' + it 'should be an instance of Application', -> expect(app).to.be.an.instanceof Application - it 'should mixin a EventBroker', -> - prototype = Application.prototype - expect(prototype).to.contain.all.keys EventBroker + it 'should mix in a EventBroker', -> + expect(Application.prototype).to.contain.all.keys EventBroker it 'should have initialize function', -> - expect(app.initialize).to.be.a 'function' + expect(app).to.respondTo 'initialize' app.initialize() it 'should create a dispatcher', -> - expect(app.initDispatcher).to.be.a 'function' + expect(app).to.respondTo 'initDispatcher' app.initDispatcher() expect(app.dispatcher).to.be.an.instanceof Dispatcher it 'should create a layout', -> - expect(app.initLayout).to.be.a 'function' + expect(app).to.respondTo 'initLayout' app.initLayout() expect(app.layout).to.be.an.instanceof Layout it 'should create a composer', -> - expect(app.initComposer).to.be.a 'function' + expect(app).to.respondTo 'initComposer' app.initComposer() expect(app.composer).to.be.an.instanceof Composer @@ -54,11 +53,11 @@ describe 'Application', -> passedMatch = null routesCalled = no routes = (match) -> - routesCalled = yes passedMatch = match + routesCalled = yes - expect(app.initRouter).to.be.a 'function' - expect(app.initRouter.length).to.equal 2 + expect(app).to.respondTo 'initRouter' + expect(app.initRouter).to.have.lengthOf 2 app.initRouter routes, root: '/', pushState: false expect(app.router).to.be.an.instanceof Router @@ -78,12 +77,12 @@ describe 'Application', -> expect(app).to.be.sealed it 'should throw an error on double-init', -> - app = new (getApp no) + app = new (getApp yes) expect(-> app.initialize()).to.throw Error it 'should dispose itself correctly', -> - expect(app.disposed).not.to.be.ok - expect(app.dispose).to.be.a 'function' + expect(app.disposed).to.be.false + expect(app).to.respondTo 'dispose' app.dispose() for key in ['dispatcher', 'layout', 'router', 'composer'] @@ -93,7 +92,7 @@ describe 'Application', -> expect(app).to.be.frozen it 'should be extendable', -> - expect(Application.extend).to.be.a 'function' + expect(Application).itself.to.respondTo 'extend' DerivedApplication = Application.extend() derivedApp = new DerivedApplication() diff --git a/test/collection_spec.coffee b/test/collection_spec.coffee index b123dbd6..f7841fa8 100644 --- a/test/collection_spec.coffee +++ b/test/collection_spec.coffee @@ -1,7 +1,7 @@ 'use strict' Backbone = require 'backbone' sinon = require 'sinon' -{EventBroker, mediator, Collection, Model} = require '../src/chaplin' +{EventBroker, mediator, Collection, Model} = require '../build/chaplin' describe 'Collection', -> collection = null @@ -21,7 +21,7 @@ describe 'Collection', -> model2 = new Backbone.Model id: 2, bar: 'bar' collection = new Collection [model1, model2] - expect(collection.serialize).to.be.a 'function' + expect(collection).to.respondTo 'serialize' expect(collection.serialize collection).to.deep.equal [ {id: 1, foo: 'foo'} {id: 2, bar: 'bar'} @@ -30,10 +30,10 @@ describe 'Collection', -> describe 'Disposal', -> it 'should dispose itself correctly', -> expect(collection.disposed).to.be.false - expect(collection.dispose).to.be.a 'function' + expect(collection).to.respondTo 'dispose' collection.dispose() - expect(collection.length).to.equal 0 + expect(collection).to.have.lengthOf 0 expect(collection.disposed).to.be.true expect(collection).to.be.frozen diff --git a/test/collection_view_spec.coffee b/test/collection_view_spec.coffee index 2085cab7..76bd3e12 100644 --- a/test/collection_view_spec.coffee +++ b/test/collection_view_spec.coffee @@ -1,8 +1,7 @@ 'use strict' $ = require 'jquery' sinon = require 'sinon' -{SyncMachine, utils, Collection, Model} = require '../src/chaplin' -{CollectionView, View} = require '../src/chaplin' +{SyncMachine, utils, Collection, Model, CollectionView, View} = require '../build/chaplin' describe 'CollectionView', -> # Initialize shared variables @@ -191,7 +190,7 @@ describe 'CollectionView', -> basicSetup() collection.reset() children = getViewChildren() - expect(children.length).to.equal 0 + expect(children).to.have.lengthOf 0 describe 'Sorting', -> @@ -658,7 +657,7 @@ describe 'CollectionView', -> it 'should dispose itself correctly', -> basicSetup() - expect(collectionView.dispose).to.be.a 'function' + expect(collectionView).to.respondTo 'dispose' viewsByCid = collectionView.getItemViews() expect(collectionView.disposed).to.be.false diff --git a/test/composer_spec.coffee b/test/composer_spec.coffee index 264cd5c2..1afdccc2 100644 --- a/test/composer_spec.coffee +++ b/test/composer_spec.coffee @@ -1,7 +1,7 @@ 'use strict' sinon = require 'sinon' -{Composer, Controller, Dispatcher, Composition} = require '../src/chaplin' -{EventBroker, Router, mediator, Model, View} = require '../src/chaplin' +{Composer, Controller, Dispatcher, Composition} = require '../build/chaplin' +{EventBroker, Router, mediator, Model, View} = require '../build/chaplin' describe 'Composer', -> composer = null @@ -36,7 +36,7 @@ describe 'Composer', -> # ---------- it 'should initialize', -> - expect(composer.initialize).to.be.a 'function' + expect(composer).to.respondTo 'initialize' composer.initialize() expect(composer.compositions).to.deep.equal {} @@ -225,7 +225,7 @@ describe 'Composer', -> it 'should dispose itself correctly', -> expect(composer.disposed).to.be.false - expect(composer.dispose).to.be.a 'function' + expect(composer).to.respondTo 'dispose' composer.dispose() expect(composer).not.to.have.ownProperty 'compositions' @@ -236,7 +236,7 @@ describe 'Composer', -> # ---------- it 'should be extendable', -> - expect(Composer.extend).to.be.a 'function' + expect(Composer).itself.to.respondTo 'extend' DerivedComposer = Composer.extend() derivedComposer = new DerivedComposer() diff --git a/test/composition_spec.coffee b/test/composition_spec.coffee index e5e5b1c9..c54aaf84 100644 --- a/test/composition_spec.coffee +++ b/test/composition_spec.coffee @@ -1,5 +1,5 @@ 'use strict' -{Composition, EventBroker, mediator} = require '../src/chaplin' +{Composition, EventBroker, mediator} = require '../build/chaplin' describe 'Composition', -> composition = null @@ -24,7 +24,7 @@ describe 'Composition', -> # ---------- it 'should initialize', -> - expect(composition.initialize).to.be.a 'function' + expect(composition).to.respondTo 'initialize' composition.initialize() expect(composition.stale()).to.be.false @@ -35,7 +35,7 @@ describe 'Composition', -> it 'should dispose itself correctly', -> expect(composition.disposed).to.be.false - expect(composition.dispose).to.be.a 'function' + expect(composition).to.respondTo 'dispose' composition.dispose() expect(composition).not.to.have.property 'compositions' @@ -46,7 +46,7 @@ describe 'Composition', -> # ---------- it 'should be extendable', -> - expect(Composition.extend).to.be.a 'function' + expect(Composition).itself.to.respondTo 'extend' DerivedComposition = Composition.extend() derivedComposition = new DerivedComposition() diff --git a/test/controller_spec.coffee b/test/controller_spec.coffee index 9afdd5a9..726dbe1f 100644 --- a/test/controller_spec.coffee +++ b/test/controller_spec.coffee @@ -1,7 +1,7 @@ 'use strict' Backbone = require 'backbone' sinon = require 'sinon' -{Controller, EventBroker, mediator, Model, View} = require '../src/chaplin' +{Controller, EventBroker, mediator, Model, View} = require '../build/chaplin' describe 'Controller', -> controller = null @@ -22,7 +22,7 @@ describe 'Controller', -> expect(prototype).to.contain.all.keys EventBroker it 'should be extendable', -> - expect(Controller.extend).to.be.a 'function' + expect(Controller).itself.to.respondTo 'extend' DerivedController = Controller.extend() derivedController = new DerivedController() @@ -31,7 +31,7 @@ describe 'Controller', -> derivedController.dispose() it 'should redirect to a URL', -> - expect(controller.redirectTo).to.be.a 'function' + expect(controller).to.respondTo 'redirectTo' routerRoute = sinon.spy() mediator.setHandler 'router:route', routerRoute @@ -79,18 +79,20 @@ describe 'Controller', -> it 'should adjust page title', -> spy = sinon.spy() + title = 'meh' + mediator.setHandler 'adjustTitle', spy - controller.adjustTitle 'meh' + controller.adjustTitle title expect(spy).to.have.been.calledOnce - expect(spy).to.have.been.calledWith 'meh' + expect(spy).to.have.been.calledWith title describe 'Disposal', -> mediator.setHandler 'region:unregister', -> it 'should dispose itself correctly', -> expect(controller.disposed).to.be.false - expect(controller.dispose).to.be.a 'function' + expect(controller).to.respondTo 'dispose' controller.dispose() expect(controller.disposed).to.be.true diff --git a/test/dispatcher_spec.coffee b/test/dispatcher_spec.coffee index 16d7124f..e235a54a 100644 --- a/test/dispatcher_spec.coffee +++ b/test/dispatcher_spec.coffee @@ -1,7 +1,7 @@ 'use strict' sinon = require 'sinon' {uniqueId} = require 'underscore' -{Composer, Controller, Dispatcher, utils, mediator} = require '../src/chaplin' +{Composer, Controller, Dispatcher, utils, mediator} = require '../build/chaplin' describe 'Dispatcher', -> # Initialize shared variables @@ -394,7 +394,7 @@ describe 'Dispatcher', -> dispose.restore() it 'should dispose itself correctly', -> - expect(dispatcher.dispose).to.be.a 'function' + expect(dispatcher).to.respondTo 'dispose' dispatcher.dispose() initialize = sinon.spy Test1Controller.prototype, 'initialize' @@ -407,7 +407,7 @@ describe 'Dispatcher', -> initialize.restore() it 'should be extendable', -> - expect(Dispatcher.extend).to.be.a 'function' + expect(Dispatcher).itself.to.respondTo 'extend' DerivedDispatcher = Dispatcher.extend() derivedDispatcher = new DerivedDispatcher() diff --git a/test/event_broker_spec.coffee b/test/event_broker_spec.coffee index 50a2454e..29709c85 100644 --- a/test/event_broker_spec.coffee +++ b/test/event_broker_spec.coffee @@ -1,13 +1,13 @@ 'use strict' sinon = require 'sinon' -{EventBroker, mediator} = require '../src/chaplin' +{EventBroker, mediator} = require '../build/chaplin' describe 'EventBroker', -> # Create a simple object which mixes in the EventBroker eventBroker = Object.assign {}, EventBroker it 'should subscribe to events', -> - expect(eventBroker.subscribeEvent).to.be.a 'function' + expect(eventBroker).to.respondTo 'subscribeEvent' # We could mock mediator.publish here and test if it was called, # well, better testing the outcome. @@ -49,7 +49,7 @@ describe 'EventBroker', -> expect(spy).to.have.been.calledOn eventBroker it 'should unsubscribe from events', -> - expect(eventBroker.unsubscribeEvent).to.be.a 'function' + expect(eventBroker).to.respondTo 'unsubscribeEvent' type = 'eventBrokerTest' spy = sinon.spy() @@ -60,7 +60,7 @@ describe 'EventBroker', -> expect(spy).to.not.have.been.called it 'should unsubscribe from all events', -> - expect(eventBroker.unsubscribeAllEvents).to.be.a 'function' + expect(eventBroker).to.respondTo 'unsubscribeAllEvents' spy = sinon.spy() unrelatedHandler = sinon.spy() @@ -84,7 +84,7 @@ describe 'EventBroker', -> mediator.unsubscribe 'four', unrelatedHandler it 'should publish events', -> - expect(eventBroker.publishEvent).to.be.a 'function' + expect(eventBroker).to.respondTo 'publishEvent' type = 'eventBrokerTest' spy = sinon.spy() diff --git a/test/layout_spec.coffee b/test/layout_spec.coffee index 3437b8ec..c33aef69 100644 --- a/test/layout_spec.coffee +++ b/test/layout_spec.coffee @@ -1,7 +1,7 @@ 'use strict' $ = require 'jquery' sinon = require 'sinon' -{Controller, mediator, Layout, View} = require '../src/chaplin' +{Controller, mediator, Layout, View} = require '../build/chaplin' describe 'Layout', -> # Initialize shared variables @@ -412,7 +412,7 @@ describe 'Layout', -> spy2 = sinon.spy() layout.delegateEvents 'click #testbed': spy2 - expect(layout.dispose).to.be.a 'function' + expect(layout).to.respondTo 'dispose' layout.dispose() expect(layout.disposed).to.be.true @@ -425,7 +425,7 @@ describe 'Layout', -> expect(spy2).to.not.have.been.called it 'should be extendable', -> - expect(Layout.extend).to.be.a 'function' + expect(Layout).itself.to.respondTo 'extend' DerivedLayout = Layout.extend() derivedLayout = new DerivedLayout() diff --git a/test/mediator_spec.coffee b/test/mediator_spec.coffee index 3bf877b7..7490e739 100644 --- a/test/mediator_spec.coffee +++ b/test/mediator_spec.coffee @@ -1,20 +1,22 @@ 'use strict' sinon = require 'sinon' -{mediator, Model} = require '../src/chaplin' +{mediator, Model} = require '../build/chaplin' describe 'mediator', -> it 'should be a simple object', -> expect(mediator).to.be.an 'object' - it 'should have seal method and be sealed', -> - expect(mediator.seal).to.be.a 'function' + it 'should be sealed', -> expect(mediator).to.be.sealed + it 'should have seal method and be sealed', -> + expect(mediator).to.respondTo 'seal' + it 'should have Pub/Sub methods', -> - expect(mediator.subscribe).to.be.a 'function' - expect(mediator.subscribeOnce).to.be.a 'function' - expect(mediator.unsubscribe).to.be.a 'function' - expect(mediator.publish).to.be.a 'function' + expect(mediator).to.respondTo 'subscribe' + expect(mediator).to.respondTo 'subscribeOnce' + expect(mediator).to.respondTo 'unsubscribe' + expect(mediator).to.respondTo 'publish' it 'should have readonly Pub/Sub and Resp/Req methods', -> methods = [ @@ -64,9 +66,9 @@ describe 'mediator', -> expect(spy).to.not.have.been.calledWith payload it 'should have response / request methods', -> - expect(mediator.setHandler).to.be.a 'function' - expect(mediator.execute).to.be.a 'function' - expect(mediator.removeHandlers).to.be.a 'function' + expect(mediator).to.respondTo 'setHandler' + expect(mediator).to.respondTo 'execute' + expect(mediator).to.respondTo 'removeHandlers' it 'should allow to set and execute handlers', -> response = 'austrian' diff --git a/test/model_spec.coffee b/test/model_spec.coffee index cca2a475..3f92292b 100644 --- a/test/model_spec.coffee +++ b/test/model_spec.coffee @@ -1,7 +1,7 @@ 'use strict' Backbone = require 'backbone' sinon = require 'sinon' -{EventBroker, mediator, Model} = require '../src/chaplin' +{EventBroker, mediator, Model} = require '../build/chaplin' describe 'Model', -> model = null @@ -130,7 +130,7 @@ describe 'Model', -> describe 'Disposal', -> it 'should dispose itself correctly', -> expect(model.disposed).to.be.false - expect(model.dispose).to.be.a 'function' + expect(model).to.respondTo 'dispose' model.dispose() expect(model.disposed).to.be.true diff --git a/test/router_spec.coffee b/test/router_spec.coffee index 410a79b5..4a33a3a0 100644 --- a/test/router_spec.coffee +++ b/test/router_spec.coffee @@ -1,7 +1,7 @@ 'use strict' Backbone = require 'backbone' sinon = require 'sinon' -{Route, Router, utils, mediator} = require '../src/chaplin' +{Route, Router, utils, mediator} = require '../build/chaplin' describe 'Router and Route', -> # Initialize shared variables @@ -33,7 +33,7 @@ describe 'Router and Route', -> it 'should allow to start the Backbone.History', -> spy = sinon.spy Backbone.history, 'start' - expect(router.startHistory).to.be.a 'function' + expect(router).to.respondTo 'startHistory' router.startHistory() expect(Backbone.History.started).to.be.true expect(spy).to.have.been.calledOnce @@ -58,7 +58,7 @@ describe 'Router and Route', -> it 'should allow to stop the Backbone.History', -> router.startHistory() spy = sinon.spy Backbone.history, 'stop' - expect(router.stopHistory).to.be.a 'function' + expect(router).to.respondTo 'stopHistory' router.stopHistory() expect(Backbone.History.started).to.be.false expect(spy).to.have.been.calledOnce @@ -67,7 +67,7 @@ describe 'Router and Route', -> describe 'Creating Routes', -> it 'should have a match method which returns a route', -> - expect(router.match).to.be.a 'function' + expect(router).to.respondTo 'match' route = router.match '', 'null#null' expect(route).to.be.an.instanceof Route @@ -807,7 +807,7 @@ describe 'Router and Route', -> describe 'Disposal', -> it 'should dispose itself correctly', -> - expect(router.dispose).to.be.a 'function' + expect(router).to.respondTo 'dispose' router.dispose() # It should stop Backbone.History @@ -827,15 +827,13 @@ describe 'Router and Route', -> describe 'Extendability', -> it 'should be extendable', -> - expect(Router.extend).to.be.a 'function' - expect(Route.extend).to.be.a 'function' - + expect(Router).itself.to.respondTo 'extend' DerivedRouter = Router.extend() derivedRouter = new DerivedRouter() expect(derivedRouter).to.be.an.instanceof Router + derivedRouter.dispose() + expect(Route).itself.to.respondTo 'extend' DerivedRoute = Route.extend() derivedRoute = new DerivedRoute 'foo', 'foo#bar' expect(derivedRoute).to.be.an.instanceof Route - - derivedRouter.dispose() diff --git a/test/sync_machine_spec.coffee b/test/sync_machine_spec.coffee index 5981d73d..7c71005b 100644 --- a/test/sync_machine_spec.coffee +++ b/test/sync_machine_spec.coffee @@ -1,15 +1,13 @@ 'use strict' Backbone = require 'backbone' sinon = require 'sinon' -{SyncMachine} = require '../src/chaplin' +{SyncMachine} = require '../build/chaplin' describe 'SyncMachine', -> machine = null beforeEach -> - machine = {} - Object.assign machine, Backbone.Events - Object.assign machine, SyncMachine + machine = Object.assign {}, Backbone.Events, SyncMachine it 'should change its state', -> expect(machine.syncState()).to.equal 'unsynced' diff --git a/test/utils_spec.coffee b/test/utils_spec.coffee index 5fdf44aa..b2b8c42e 100644 --- a/test/utils_spec.coffee +++ b/test/utils_spec.coffee @@ -1,6 +1,6 @@ 'use strict' Backbone = require 'backbone' -{utils, mediator} = require '../src/chaplin' +{utils, mediator} = require '../build/chaplin' describe 'utils', -> class A @@ -185,5 +185,5 @@ describe 'utils', -> expect(parse 'c=2', -> []).to.deep.equal {} it 'should have old methods', -> - expect(utils.queryParams.stringify).to.be.a 'function' - expect(utils.queryParams.parse).to.be.a 'function' + expect(utils.queryParams).to.respondTo 'stringify' + expect(utils.queryParams).to.respondTo 'parse' diff --git a/test/view_spec.coffee b/test/view_spec.coffee index cfe1355b..2c2cad10 100644 --- a/test/view_spec.coffee +++ b/test/view_spec.coffee @@ -2,8 +2,8 @@ $ = require 'jquery' Backbone = require 'backbone' sinon = require 'sinon' -{EventBroker, SyncMachine, mediator} = require '../src/chaplin' -{Collection, Model, View} = require '../src/chaplin' +{EventBroker, SyncMachine, mediator} = require '../build/chaplin' +{Collection, Model, View} = require '../build/chaplin' describe 'View', -> renderCalled = false @@ -73,7 +73,7 @@ describe 'View', -> expect(prototype).to.contain.all.keys EventBroker it 'should render', -> - expect(view.render).to.be.a 'function' + expect(view).to.respondTo 'render' renderResult = view.render() expect(renderResult).to.be.equal renderReturnValue @@ -212,8 +212,8 @@ describe 'View', -> it 'should register and remove user input event handlers', -> view.dispose() view = new TestView container: testbed - expect(view.delegate).to.be.a 'function' - expect(view.undelegate).to.be.a 'function' + expect(view).to.respondTo 'delegate' + expect(view).to.respondTo 'undelegate' spy = sinon.spy() handler = view.delegate 'click', spy @@ -361,7 +361,7 @@ describe 'View', -> expect(e.handler).to.have.been.calledOn e it 'should add and return subviews', -> - expect(view.subview).to.be.a 'function' + expect(view).to.respondTo 'subview' subview = new View() view.subview 'fooSubview', subview @@ -374,7 +374,7 @@ describe 'View', -> expect(view.subviews).to.have.lengthOf 1 it 'should remove subviews', -> - expect(view.removeSubview).to.be.a 'function' + expect(view).to.respondTo 'removeSubview' # By name subview = new View() @@ -703,7 +703,7 @@ describe 'View', -> it 'should dispose itself correctly', -> expect(view.disposed).to.be.false - expect(view.dispose).to.be.a 'function' + expect(view).to.respondTo 'dispose' view.dispose() expect(view.disposed).to.be.true @@ -714,7 +714,7 @@ describe 'View', -> document.body.appendChild view.el expect(document.querySelector '#disposed-view').to.be.ok view.dispose() - expect(document.querySelector '#disposed-view').not.to.be.ok + expect(document.querySelector '#disposed-view').to.be.null it 'should call Backbone.View#remove', -> sinon.spy view, 'remove'