From 0eee8ca69263a3b68842348931586c0ceaf29e86 Mon Sep 17 00:00:00 2001 From: Aleksey Shvayka Date: Sun, 10 Sep 2017 16:35:19 +0300 Subject: [PATCH 01/15] remove extra require --- Gruntfile.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gruntfile.coffee b/Gruntfile.coffee index adabbe25..4af9acf9 100644 --- a/Gruntfile.coffee +++ b/Gruntfile.coffee @@ -47,7 +47,7 @@ setupJSDOM = -> setupChai = -> chai = require 'chai' chai.use require 'sinon-chai' - require 'chai/register-expect' + chai.should() module.exports = (grunt) -> From 890ec6bb7e441afda7bb2169a1ce4c2cb47ee9ba Mon Sep 17 00:00:00 2001 From: Aleksey Shvayka Date: Sun, 10 Sep 2017 16:35:29 +0300 Subject: [PATCH 02/15] flip negated condition --- test/application_spec.coffee | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/test/application_spec.coffee b/test/application_spec.coffee index 843247bd..57e20ea6 100644 --- a/test/application_spec.coffee +++ b/test/application_spec.coffee @@ -6,14 +6,15 @@ Backbone = require 'backbone' 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() @@ -78,7 +79,7 @@ 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', -> From 37d668b84fc0facf971f41c3bb4d308056f77004 Mon Sep 17 00:00:00 2001 From: Aleksey Shvayka Date: Sun, 10 Sep 2017 21:18:56 +0300 Subject: [PATCH 03/15] use ES modules in /src --- src/chaplin.coffee | 58 +++++++++++++++-------- src/chaplin/application.coffee | 20 ++++---- src/chaplin/composer.coffee | 14 +++--- src/chaplin/controllers/controller.coffee | 14 +++--- src/chaplin/dispatcher.coffee | 14 +++--- src/chaplin/lib/composition.coffee | 9 ++-- src/chaplin/lib/event_broker.coffee | 12 +---- src/chaplin/lib/history.coffee | 8 ++-- src/chaplin/lib/route.coffee | 14 +++--- src/chaplin/lib/router.coffee | 18 ++++--- src/chaplin/lib/support.coffee | 10 ++-- src/chaplin/lib/sync_machine.coffee | 7 +-- src/chaplin/lib/utils.coffee | 8 +--- src/chaplin/mediator.coffee | 8 ++-- src/chaplin/models/collection.coffee | 14 +++--- src/chaplin/models/model.coffee | 10 ++-- src/chaplin/views/collection_view.coffee | 11 ++--- src/chaplin/views/layout.coffee | 16 +++---- src/chaplin/views/view.coffee | 14 +++--- 19 files changed, 123 insertions(+), 156 deletions(-) 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..06f849f3 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 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..07d676f2 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 @@ -82,7 +80,4 @@ for event in [UNSYNCED, SYNCING, SYNCED, STATE_CHANGE] 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..9d488518 100644 --- a/src/chaplin/lib/utils.coffee +++ b/src/chaplin/lib/utils.coffee @@ -152,11 +152,5 @@ 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..2f2c1ffa 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 # -------- @@ -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..bbacc695 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 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 From 534e51c46621e389fef128294d8985d6bbc3c0c2 Mon Sep 17 00:00:00 2001 From: Aleksey Shvayka Date: Sun, 10 Sep 2017 21:19:10 +0300 Subject: [PATCH 04/15] minor code style tweaks --- src/chaplin/lib/sync_machine.coffee | 15 ++++++--------- src/chaplin/mediator.coffee | 2 +- src/chaplin/models/collection.coffee | 4 +--- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/chaplin/lib/sync_machine.coffee b/src/chaplin/lib/sync_machine.coffee index 07d676f2..8570b1be 100644 --- a/src/chaplin/lib/sync_machine.coffee +++ b/src/chaplin/lib/sync_machine.coffee @@ -7,9 +7,8 @@ # (named after the events above) UNSYNCED = 'unsynced' -SYNCING = 'syncing' -SYNCED = 'synced' - +SYNCING = 'syncing' +SYNCED = 'synced' STATE_CHANGE = 'syncStateChange' SyncMachine = @@ -72,12 +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. export default Object.freeze SyncMachine diff --git a/src/chaplin/mediator.coffee b/src/chaplin/mediator.coffee index 2f2c1ffa..77d67843 100644 --- a/src/chaplin/mediator.coffee +++ b/src/chaplin/mediator.coffee @@ -40,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...) -> diff --git a/src/chaplin/models/collection.coffee b/src/chaplin/models/collection.coffee index bbacc695..fe608438 100644 --- a/src/chaplin/models/collection.coffee +++ b/src/chaplin/models/collection.coffee @@ -45,9 +45,7 @@ export default 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 = {} From 6d42fa9d0bb1b0a5a8adeaf3f81b173ed1dedd23 Mon Sep 17 00:00:00 2001 From: Aleksey Shvayka Date: Sun, 10 Sep 2017 21:20:47 +0300 Subject: [PATCH 05/15] change require target in tests --- test/application_spec.coffee | 4 ++-- test/collection_spec.coffee | 2 +- test/collection_view_spec.coffee | 3 +-- test/composer_spec.coffee | 4 ++-- test/composition_spec.coffee | 2 +- test/controller_spec.coffee | 2 +- test/dispatcher_spec.coffee | 2 +- test/event_broker_spec.coffee | 2 +- test/layout_spec.coffee | 2 +- test/mediator_spec.coffee | 2 +- test/model_spec.coffee | 2 +- test/router_spec.coffee | 2 +- test/sync_machine_spec.coffee | 2 +- test/utils_spec.coffee | 2 +- test/view_spec.coffee | 4 ++-- 15 files changed, 18 insertions(+), 19 deletions(-) diff --git a/test/application_spec.coffee b/test/application_spec.coffee index 57e20ea6..ebc500d3 100644 --- a/test/application_spec.coffee +++ b/test/application_spec.coffee @@ -1,7 +1,7 @@ '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 diff --git a/test/collection_spec.coffee b/test/collection_spec.coffee index b123dbd6..2bc2a7b9 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 diff --git a/test/collection_view_spec.coffee b/test/collection_view_spec.coffee index 2085cab7..04ce4fe9 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 diff --git a/test/composer_spec.coffee b/test/composer_spec.coffee index 264cd5c2..5f53cace 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 diff --git a/test/composition_spec.coffee b/test/composition_spec.coffee index e5e5b1c9..b0504712 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 diff --git a/test/controller_spec.coffee b/test/controller_spec.coffee index 9afdd5a9..b38a42a0 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 diff --git a/test/dispatcher_spec.coffee b/test/dispatcher_spec.coffee index 16d7124f..09003316 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 diff --git a/test/event_broker_spec.coffee b/test/event_broker_spec.coffee index 50a2454e..53129a69 100644 --- a/test/event_broker_spec.coffee +++ b/test/event_broker_spec.coffee @@ -1,6 +1,6 @@ '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 diff --git a/test/layout_spec.coffee b/test/layout_spec.coffee index 3437b8ec..cac0aee6 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 diff --git a/test/mediator_spec.coffee b/test/mediator_spec.coffee index 3bf877b7..6eac10e1 100644 --- a/test/mediator_spec.coffee +++ b/test/mediator_spec.coffee @@ -1,6 +1,6 @@ 'use strict' sinon = require 'sinon' -{mediator, Model} = require '../src/chaplin' +{mediator, Model} = require '../build/chaplin' describe 'mediator', -> it 'should be a simple object', -> diff --git a/test/model_spec.coffee b/test/model_spec.coffee index cca2a475..f132a3b5 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 diff --git a/test/router_spec.coffee b/test/router_spec.coffee index 410a79b5..d6420d99 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 diff --git a/test/sync_machine_spec.coffee b/test/sync_machine_spec.coffee index 5981d73d..2a43aae1 100644 --- a/test/sync_machine_spec.coffee +++ b/test/sync_machine_spec.coffee @@ -1,7 +1,7 @@ 'use strict' Backbone = require 'backbone' sinon = require 'sinon' -{SyncMachine} = require '../src/chaplin' +{SyncMachine} = require '../build/chaplin' describe 'SyncMachine', -> machine = null diff --git a/test/utils_spec.coffee b/test/utils_spec.coffee index 5fdf44aa..b5ac5999 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 diff --git a/test/view_spec.coffee b/test/view_spec.coffee index cfe1355b..d4eb5ecc 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 From 698c96fd63becc788e52df2f5cdd695f0fdeffe6 Mon Sep 17 00:00:00 2001 From: Aleksey Shvayka Date: Sun, 10 Sep 2017 21:23:01 +0300 Subject: [PATCH 06/15] tests: prefer `.respondTo` over `.to.be.a` --- test/application_spec.coffee | 14 +++++++------- test/collection_spec.coffee | 4 ++-- test/collection_view_spec.coffee | 2 +- test/composer_spec.coffee | 6 +++--- test/composition_spec.coffee | 6 +++--- test/controller_spec.coffee | 6 +++--- test/dispatcher_spec.coffee | 4 ++-- test/event_broker_spec.coffee | 8 ++++---- test/layout_spec.coffee | 4 ++-- test/mediator_spec.coffee | 14 +++++++------- test/model_spec.coffee | 2 +- test/router_spec.coffee | 13 ++++++------- test/utils_spec.coffee | 4 ++-- test/view_spec.coffee | 12 ++++++------ 14 files changed, 49 insertions(+), 50 deletions(-) diff --git a/test/application_spec.coffee b/test/application_spec.coffee index ebc500d3..eee7ba48 100644 --- a/test/application_spec.coffee +++ b/test/application_spec.coffee @@ -28,21 +28,21 @@ describe 'Application', -> expect(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 @@ -58,8 +58,8 @@ describe 'Application', -> routesCalled = yes passedMatch = match - expect(app.initRouter).to.be.a 'function' expect(app.initRouter.length).to.equal 2 + expect(app).to.respondTo 'initRouter' app.initRouter routes, root: '/', pushState: false expect(app.router).to.be.an.instanceof Router @@ -84,7 +84,7 @@ describe 'Application', -> it 'should dispose itself correctly', -> expect(app.disposed).not.to.be.ok - expect(app.dispose).to.be.a 'function' + expect(app).to.respondTo 'dispose' app.dispose() for key in ['dispatcher', 'layout', 'router', 'composer'] @@ -94,7 +94,7 @@ describe 'Application', -> expect(app).to.be.frozen it 'should be extendable', -> - expect(Application.extend).to.be.a 'function' + expect(Application).to.respondTo 'extend' DerivedApplication = Application.extend() derivedApp = new DerivedApplication() diff --git a/test/collection_spec.coffee b/test/collection_spec.coffee index 2bc2a7b9..a8620806 100644 --- a/test/collection_spec.coffee +++ b/test/collection_spec.coffee @@ -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,7 +30,7 @@ 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 diff --git a/test/collection_view_spec.coffee b/test/collection_view_spec.coffee index 04ce4fe9..8ed62b07 100644 --- a/test/collection_view_spec.coffee +++ b/test/collection_view_spec.coffee @@ -657,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 5f53cace..0ebf915a 100644 --- a/test/composer_spec.coffee +++ b/test/composer_spec.coffee @@ -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).to.respondTo 'extend' DerivedComposer = Composer.extend() derivedComposer = new DerivedComposer() diff --git a/test/composition_spec.coffee b/test/composition_spec.coffee index b0504712..c1775de4 100644 --- a/test/composition_spec.coffee +++ b/test/composition_spec.coffee @@ -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).to.respondTo 'extend' DerivedComposition = Composition.extend() derivedComposition = new DerivedComposition() diff --git a/test/controller_spec.coffee b/test/controller_spec.coffee index b38a42a0..32883707 100644 --- a/test/controller_spec.coffee +++ b/test/controller_spec.coffee @@ -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).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 @@ -90,7 +90,7 @@ describe 'Controller', -> 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 09003316..8ffc6dac 100644 --- a/test/dispatcher_spec.coffee +++ b/test/dispatcher_spec.coffee @@ -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).to.respondTo 'extend' DerivedDispatcher = Dispatcher.extend() derivedDispatcher = new DerivedDispatcher() diff --git a/test/event_broker_spec.coffee b/test/event_broker_spec.coffee index 53129a69..29709c85 100644 --- a/test/event_broker_spec.coffee +++ b/test/event_broker_spec.coffee @@ -7,7 +7,7 @@ describe '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 cac0aee6..0623d606 100644 --- a/test/layout_spec.coffee +++ b/test/layout_spec.coffee @@ -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).to.respondTo 'extend' DerivedLayout = Layout.extend() derivedLayout = new DerivedLayout() diff --git a/test/mediator_spec.coffee b/test/mediator_spec.coffee index 6eac10e1..f080b459 100644 --- a/test/mediator_spec.coffee +++ b/test/mediator_spec.coffee @@ -11,10 +11,10 @@ describe 'mediator', -> expect(mediator).to.be.sealed 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 +64,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 f132a3b5..3f92292b 100644 --- a/test/model_spec.coffee +++ b/test/model_spec.coffee @@ -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 d6420d99..657d3dae 100644 --- a/test/router_spec.coffee +++ b/test/router_spec.coffee @@ -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,13 +827,12 @@ 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).to.respondTo 'extend' DerivedRouter = Router.extend() derivedRouter = new DerivedRouter() expect(derivedRouter).to.be.an.instanceof Router + expect(Route).to.respondTo 'extend' DerivedRoute = Route.extend() derivedRoute = new DerivedRoute 'foo', 'foo#bar' expect(derivedRoute).to.be.an.instanceof Route diff --git a/test/utils_spec.coffee b/test/utils_spec.coffee index b5ac5999..b2b8c42e 100644 --- a/test/utils_spec.coffee +++ b/test/utils_spec.coffee @@ -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 d4eb5ecc..4cfc8ee9 100644 --- a/test/view_spec.coffee +++ b/test/view_spec.coffee @@ -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 From 10da8a6ea84f0c9c6ec12e0dd798c1794e3becb1 Mon Sep 17 00:00:00 2001 From: Aleksey Shvayka Date: Sun, 10 Sep 2017 21:24:40 +0300 Subject: [PATCH 07/15] tests: prefer `.empty` over `.length` --- test/collection_spec.coffee | 2 +- test/collection_view_spec.coffee | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/collection_spec.coffee b/test/collection_spec.coffee index a8620806..00ba589d 100644 --- a/test/collection_spec.coffee +++ b/test/collection_spec.coffee @@ -33,7 +33,7 @@ describe 'Collection', -> expect(collection).to.respondTo 'dispose' collection.dispose() - expect(collection.length).to.equal 0 + expect(collection).to.be.empty 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 8ed62b07..73817889 100644 --- a/test/collection_view_spec.coffee +++ b/test/collection_view_spec.coffee @@ -190,7 +190,7 @@ describe 'CollectionView', -> basicSetup() collection.reset() children = getViewChildren() - expect(children.length).to.equal 0 + expect(children).to.be.empty describe 'Sorting', -> From 2ca890f9ffe37da7a0ffede5bccdc4e82b2bf9ff Mon Sep 17 00:00:00 2001 From: Aleksey Shvayka Date: Sun, 10 Sep 2017 21:25:14 +0300 Subject: [PATCH 08/15] tests: prefer `.lengthOf` over `.length` --- test/application_spec.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/application_spec.coffee b/test/application_spec.coffee index eee7ba48..3317196c 100644 --- a/test/application_spec.coffee +++ b/test/application_spec.coffee @@ -58,8 +58,8 @@ describe 'Application', -> routesCalled = yes passedMatch = match - 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 From cd6311bc04113a06aa809ccf96a50c630c67a038 Mon Sep 17 00:00:00 2001 From: Aleksey Shvayka Date: Sun, 10 Sep 2017 21:39:11 +0300 Subject: [PATCH 09/15] make tests more precise --- test/application_spec.coffee | 2 +- test/view_spec.coffee | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/application_spec.coffee b/test/application_spec.coffee index 3317196c..ae8bb0c2 100644 --- a/test/application_spec.coffee +++ b/test/application_spec.coffee @@ -83,7 +83,7 @@ describe 'Application', -> expect(-> app.initialize()).to.throw Error it 'should dispose itself correctly', -> - expect(app.disposed).not.to.be.ok + expect(app.disposed).to.be.false expect(app).to.respondTo 'dispose' app.dispose() diff --git a/test/view_spec.coffee b/test/view_spec.coffee index 4cfc8ee9..2c2cad10 100644 --- a/test/view_spec.coffee +++ b/test/view_spec.coffee @@ -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' From 552176af79e3f822f20a72006964a5ba671f9ecf Mon Sep 17 00:00:00 2001 From: Aleksey Shvayka Date: Sun, 10 Sep 2017 21:39:32 +0300 Subject: [PATCH 10/15] tests: inline loop --- test/mediator_spec.coffee | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/test/mediator_spec.coffee b/test/mediator_spec.coffee index f080b459..0865cd19 100644 --- a/test/mediator_spec.coffee +++ b/test/mediator_spec.coffee @@ -17,17 +17,18 @@ describe 'mediator', -> expect(mediator).to.respondTo 'publish' it 'should have readonly Pub/Sub and Resp/Req methods', -> - methods = [ - 'subscribe', 'subscribeOnce', 'unsubscribe', 'publish', - 'setHandler', 'execute', 'removeHandlers' - ] - - for method in methods - expect(mediator).to.have.ownPropertyDescriptor method, - value: mediator[method] - writable: false - enumerable: true - configurable: false + desc = + writable: false + enumerable: true + configurable: false + + expect(mediator).to.have.ownPropertyDescriptor 'subscribe', desc + expect(mediator).to.have.ownPropertyDescriptor 'subscribeOnce', desc + expect(mediator).to.have.ownPropertyDescriptor 'unsubscribe', desc + expect(mediator).to.have.ownPropertyDescriptor 'publish', desc + expect(mediator).to.have.ownPropertyDescriptor 'setHandler', desc + expect(mediator).to.have.ownPropertyDescriptor 'execute', desc + expect(mediator).to.have.ownPropertyDescriptor 'removeHandlers', desc it 'should publish messages to subscribers', -> spy = sinon.spy() From 8808996c76af925d163eca9333b13c8150741527 Mon Sep 17 00:00:00 2001 From: Aleksey Shvayka Date: Sun, 10 Sep 2017 21:39:43 +0300 Subject: [PATCH 11/15] tests: minor code tweaks --- test/application_spec.coffee | 10 ++++------ test/controller_spec.coffee | 6 ++++-- test/mediator_spec.coffee | 6 ++++-- test/router_spec.coffee | 3 +-- test/sync_machine_spec.coffee | 4 +--- 5 files changed, 14 insertions(+), 15 deletions(-) diff --git a/test/application_spec.coffee b/test/application_spec.coffee index ae8bb0c2..cd80d9cb 100644 --- a/test/application_spec.coffee +++ b/test/application_spec.coffee @@ -19,13 +19,11 @@ describe 'Application', -> 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).to.respondTo 'initialize' @@ -55,8 +53,8 @@ describe 'Application', -> passedMatch = null routesCalled = no routes = (match) -> - routesCalled = yes passedMatch = match + routesCalled = yes expect(app).to.respondTo 'initRouter' expect(app.initRouter).to.have.lengthOf 2 diff --git a/test/controller_spec.coffee b/test/controller_spec.coffee index 32883707..49b5d345 100644 --- a/test/controller_spec.coffee +++ b/test/controller_spec.coffee @@ -79,11 +79,13 @@ 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', -> diff --git a/test/mediator_spec.coffee b/test/mediator_spec.coffee index 0865cd19..fc47d8b6 100644 --- a/test/mediator_spec.coffee +++ b/test/mediator_spec.coffee @@ -6,10 +6,12 @@ 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).to.respondTo 'subscribe' expect(mediator).to.respondTo 'subscribeOnce' diff --git a/test/router_spec.coffee b/test/router_spec.coffee index 657d3dae..c7a9827e 100644 --- a/test/router_spec.coffee +++ b/test/router_spec.coffee @@ -831,10 +831,9 @@ describe 'Router and Route', -> DerivedRouter = Router.extend() derivedRouter = new DerivedRouter() expect(derivedRouter).to.be.an.instanceof Router + derivedRouter.dispose() expect(Route).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 2a43aae1..7c71005b 100644 --- a/test/sync_machine_spec.coffee +++ b/test/sync_machine_spec.coffee @@ -7,9 +7,7 @@ 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' From 285ccc082b3be283c02487811f08cc626ef3dfa3 Mon Sep 17 00:00:00 2001 From: Aleksey Shvayka Date: Sun, 10 Sep 2017 23:15:25 +0300 Subject: [PATCH 12/15] fix tests --- src/chaplin/application.coffee | 2 ++ src/chaplin/lib/utils.coffee | 16 +++++----------- test/application_spec.coffee | 2 +- test/collection_spec.coffee | 2 +- test/collection_view_spec.coffee | 2 +- test/composer_spec.coffee | 2 +- test/composition_spec.coffee | 2 +- test/controller_spec.coffee | 2 +- test/dispatcher_spec.coffee | 2 +- test/layout_spec.coffee | 2 +- test/mediator_spec.coffee | 23 +++++++++++------------ test/router_spec.coffee | 4 ++-- 12 files changed, 28 insertions(+), 33 deletions(-) diff --git a/src/chaplin/application.coffee b/src/chaplin/application.coffee index 06f849f3..5fcf7f78 100644 --- a/src/chaplin/application.coffee +++ b/src/chaplin/application.coffee @@ -128,6 +128,8 @@ export default 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/lib/utils.coffee b/src/chaplin/lib/utils.coffee index 9d488518..366883bb 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,13 +66,11 @@ 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 -> @@ -152,5 +147,4 @@ utils.indexOf = (array, item) -> array.indexOf item utils.isArray = Array.isArray utils.queryParams = utils.querystring -# Seal the utils object. export default Object.seal utils diff --git a/test/application_spec.coffee b/test/application_spec.coffee index cd80d9cb..cd1036f6 100644 --- a/test/application_spec.coffee +++ b/test/application_spec.coffee @@ -92,7 +92,7 @@ describe 'Application', -> expect(app).to.be.frozen it 'should be extendable', -> - expect(Application).to.respondTo 'extend' + 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 00ba589d..f7841fa8 100644 --- a/test/collection_spec.coffee +++ b/test/collection_spec.coffee @@ -33,7 +33,7 @@ describe 'Collection', -> expect(collection).to.respondTo 'dispose' collection.dispose() - expect(collection).to.be.empty + 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 73817889..76bd3e12 100644 --- a/test/collection_view_spec.coffee +++ b/test/collection_view_spec.coffee @@ -190,7 +190,7 @@ describe 'CollectionView', -> basicSetup() collection.reset() children = getViewChildren() - expect(children).to.be.empty + expect(children).to.have.lengthOf 0 describe 'Sorting', -> diff --git a/test/composer_spec.coffee b/test/composer_spec.coffee index 0ebf915a..1afdccc2 100644 --- a/test/composer_spec.coffee +++ b/test/composer_spec.coffee @@ -236,7 +236,7 @@ describe 'Composer', -> # ---------- it 'should be extendable', -> - expect(Composer).to.respondTo 'extend' + 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 c1775de4..c54aaf84 100644 --- a/test/composition_spec.coffee +++ b/test/composition_spec.coffee @@ -46,7 +46,7 @@ describe 'Composition', -> # ---------- it 'should be extendable', -> - expect(Composition).to.respondTo 'extend' + 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 49b5d345..726dbe1f 100644 --- a/test/controller_spec.coffee +++ b/test/controller_spec.coffee @@ -22,7 +22,7 @@ describe 'Controller', -> expect(prototype).to.contain.all.keys EventBroker it 'should be extendable', -> - expect(Controller).to.respondTo 'extend' + expect(Controller).itself.to.respondTo 'extend' DerivedController = Controller.extend() derivedController = new DerivedController() diff --git a/test/dispatcher_spec.coffee b/test/dispatcher_spec.coffee index 8ffc6dac..e235a54a 100644 --- a/test/dispatcher_spec.coffee +++ b/test/dispatcher_spec.coffee @@ -407,7 +407,7 @@ describe 'Dispatcher', -> initialize.restore() it 'should be extendable', -> - expect(Dispatcher).to.respondTo 'extend' + expect(Dispatcher).itself.to.respondTo 'extend' DerivedDispatcher = Dispatcher.extend() derivedDispatcher = new DerivedDispatcher() diff --git a/test/layout_spec.coffee b/test/layout_spec.coffee index 0623d606..c33aef69 100644 --- a/test/layout_spec.coffee +++ b/test/layout_spec.coffee @@ -425,7 +425,7 @@ describe 'Layout', -> expect(spy2).to.not.have.been.called it 'should be extendable', -> - expect(Layout).to.respondTo 'extend' + 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 fc47d8b6..7490e739 100644 --- a/test/mediator_spec.coffee +++ b/test/mediator_spec.coffee @@ -19,18 +19,17 @@ describe 'mediator', -> expect(mediator).to.respondTo 'publish' it 'should have readonly Pub/Sub and Resp/Req methods', -> - desc = - writable: false - enumerable: true - configurable: false - - expect(mediator).to.have.ownPropertyDescriptor 'subscribe', desc - expect(mediator).to.have.ownPropertyDescriptor 'subscribeOnce', desc - expect(mediator).to.have.ownPropertyDescriptor 'unsubscribe', desc - expect(mediator).to.have.ownPropertyDescriptor 'publish', desc - expect(mediator).to.have.ownPropertyDescriptor 'setHandler', desc - expect(mediator).to.have.ownPropertyDescriptor 'execute', desc - expect(mediator).to.have.ownPropertyDescriptor 'removeHandlers', desc + methods = [ + 'subscribe', 'subscribeOnce', 'unsubscribe', 'publish', + 'setHandler', 'execute', 'removeHandlers' + ] + + for method in methods + expect(mediator).to.have.ownPropertyDescriptor method, + value: mediator[method] + writable: false + enumerable: true + configurable: false it 'should publish messages to subscribers', -> spy = sinon.spy() diff --git a/test/router_spec.coffee b/test/router_spec.coffee index c7a9827e..4a33a3a0 100644 --- a/test/router_spec.coffee +++ b/test/router_spec.coffee @@ -827,13 +827,13 @@ describe 'Router and Route', -> describe 'Extendability', -> it 'should be extendable', -> - expect(Router).to.respondTo 'extend' + expect(Router).itself.to.respondTo 'extend' DerivedRouter = Router.extend() derivedRouter = new DerivedRouter() expect(derivedRouter).to.be.an.instanceof Router derivedRouter.dispose() - expect(Route).to.respondTo 'extend' + expect(Route).itself.to.respondTo 'extend' DerivedRoute = Route.extend() derivedRoute = new DerivedRoute 'foo', 'foo#bar' expect(derivedRoute).to.be.an.instanceof Route From c8d679a332f560b292d5eb815313abd206b3acd3 Mon Sep 17 00:00:00 2001 From: Aleksey Shvayka Date: Mon, 11 Sep 2017 03:16:27 +0300 Subject: [PATCH 13/15] simplify `utils.loadModule` --- src/chaplin/lib/utils.coffee | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/chaplin/lib/utils.coffee b/src/chaplin/lib/utils.coffee index 366883bb..e9e0e61d 100644 --- a/src/chaplin/lib/utils.coffee +++ b/src/chaplin/lib/utils.coffee @@ -74,13 +74,11 @@ utils = # 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 From 44e6777cdfe97bd50d315c9fd01d81844241e86c Mon Sep 17 00:00:00 2001 From: Aleksey Shvayka Date: Mon, 11 Sep 2017 03:16:37 +0300 Subject: [PATCH 14/15] update deps --- Gruntfile.coffee | 89 ++++++++++++++++-------------------------------- package.json | 28 +++++++-------- 2 files changed, 43 insertions(+), 74 deletions(-) diff --git a/Gruntfile.coffee b/Gruntfile.coffee index 4af9acf9..355bf1d3 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' @@ -47,7 +22,7 @@ setupJSDOM = -> setupChai = -> chai = require 'chai' chai.use require 'sinon-chai' - chai.should() + require 'chai/register-expect' module.exports = (grunt) -> @@ -59,6 +34,9 @@ module.exports = (grunt) -> # ------- pkg + clean: + dist: 'build' + coffeelint: src: 'src/**/*.coffee' test: 'test/*.coffee' @@ -72,7 +50,6 @@ module.exports = (grunt) -> reporter: 'spec' require: [ 'coffee-script/register' - 'coffee-coverage/register-istanbul' setupJSDOM -> require.cache[require.resolve 'jquery'] = {} 'backbone.nativeview' @@ -90,37 +67,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 +207,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 +285,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..e38cdb90 100644 --- a/package.json +++ b/package.json @@ -10,26 +10,26 @@ "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-clean": "^1.1.0", + "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", + "grunt-rollup": "~5.0.0", + "grunt-transbrute": "~1.0.1", "jquery": "2.2.x", - "jsdom": "~11.1.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": { From 82a2510bd08346e02c28d18ca8b789dfc58ef4ca Mon Sep 17 00:00:00 2001 From: Aleksey Shvayka Date: Mon, 11 Sep 2017 03:19:44 +0300 Subject: [PATCH 15/15] drop clean task --- Gruntfile.coffee | 3 --- package.json | 3 +-- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/Gruntfile.coffee b/Gruntfile.coffee index 355bf1d3..a5c5e46a 100644 --- a/Gruntfile.coffee +++ b/Gruntfile.coffee @@ -34,9 +34,6 @@ module.exports = (grunt) -> # ------- pkg - clean: - dist: 'build' - coffeelint: src: 'src/**/*.coffee' test: 'test/*.coffee' diff --git a/package.json b/package.json index e38cdb90..8bdba123 100644 --- a/package.json +++ b/package.json @@ -14,14 +14,13 @@ "grunt": "~1.0.1", "grunt-cli": "~1.2.0", "grunt-coffeelint": "~0.0.16", - "grunt-contrib-clean": "^1.1.0", "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-rollup": "~5.0.0", "grunt-transbrute": "~1.0.1", - "jquery": "2.2.x", + "jquery": "~2.2.0", "jsdom": "~11.2.0", "jsdom-global": "~3.0.2", "mocha": "~3.5.0",