Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

Commit

Permalink
Merge pull request #881 from hellyeahllc/add-missing-tests
Browse files Browse the repository at this point in the history
Add missing tests
  • Loading branch information
paulmillr committed Feb 17, 2016
2 parents 460f0d2 + ebb80cc commit 77d2d7b
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 18 deletions.
1 change: 0 additions & 1 deletion Gruntfile.coffee
Expand Up @@ -64,7 +64,6 @@ module.exports = (grunt) ->
require: [
'coffee-script/register'
'jsdom-assign'
'jquery'
]
src: 'test/*.coffee'

Expand Down
8 changes: 4 additions & 4 deletions src/chaplin/dispatcher.coffee
Expand Up @@ -4,7 +4,7 @@ _ = require 'underscore'
Backbone = require 'backbone'

EventBroker = require './lib/event_broker'
{loadModule} = require './lib/utils'
utils = require './lib/utils'
mediator = require './mediator'

module.exports = class Dispatcher
Expand Down Expand Up @@ -78,11 +78,11 @@ module.exports = class Dispatcher
# The default implementation uses require() from a AMD module loader
# like RequireJS to fetch the constructor.
loadController: (name, handler) ->
return handler(name) if name is Object name
return handler name if name is Object name

fileName = name + @settings.controllerSuffix
moduleName = @settings.controllerPath + fileName
loadModule moduleName, handler
utils.loadModule moduleName, handler

# Handler for the controller lazy-loading.
controllerLoaded: (route, params, options, Controller) ->
Expand Down Expand Up @@ -146,7 +146,7 @@ module.exports = class Dispatcher

# Execute action in controller context.
promise = controller.beforeAction params, route, options
if promise and promise.then
if typeof promise?.then is 'function'
promise.then executeAction
else
executeAction()
Expand Down
5 changes: 2 additions & 3 deletions src/chaplin/lib/router.coffee
Expand Up @@ -72,8 +72,7 @@ module.exports = class Router # This class does not extend Backbone.Router.
match: (pattern, target, options = {}) =>
if arguments.length is 2 and target and typeof target is 'object'
# Handles cases like `match 'url', controller: 'c', action: 'a'`.
options = target
{controller, action} = options
{controller, action} = options = target
unless controller and action
throw new Error 'Router#match must receive either target or ' +
'options.controller & options.action'
Expand Down Expand Up @@ -178,7 +177,7 @@ module.exports = class Router # This class does not extend Backbone.Router.
changeURL: (controller, params, route, options) ->
return unless route.path? and options?.changeURL

url = route.path + if route.query then "?#{route.query}" else ""
url = route.path + if route.query then "?#{route.query}" else ''

navigateOptions =
# Do not trigger or replace per default.
Expand Down
7 changes: 3 additions & 4 deletions src/chaplin/lib/utils.coffee
Expand Up @@ -78,7 +78,7 @@ utils =

# Determines module system and returns module loader function.
loadModule: do ->
{require} = window
{define, require} = window

if typeof define is 'function' and define.amd
(moduleName, handler) ->
Expand All @@ -87,8 +87,7 @@ utils =
enqueue = setImmediate ? setTimeout

(moduleName, handler) ->
enqueue ->
handler require moduleName
enqueue -> handler require moduleName


# Query parameters Helpers
Expand Down Expand Up @@ -121,7 +120,7 @@ utils =
string = string.slice 1 + string.indexOf '?'
string.split('&').reduce (params, pair) ->
parts = pair.split('=').map decodeURIComponent
{key, value} = reviver parts...
{key, value} = reviver(parts...) or {}

if value? then params[key] =
if params.hasOwnProperty key
Expand Down
28 changes: 27 additions & 1 deletion test/dispatcher_spec.coffee
Expand Up @@ -8,7 +8,7 @@ chai.use require 'sinon-chai'
chai.should()

{expect} = require 'chai'
{Composer, Controller, Dispatcher, mediator} = require '../src/chaplin'
{Composer, Controller, Dispatcher, utils, mediator} = require '../src/chaplin'

describe 'Dispatcher', ->
# Initialize shared variables
Expand Down Expand Up @@ -49,6 +49,7 @@ describe 'Dispatcher', ->
#console.debug 'Test2Controller#dispose'
super

{loadController} = Dispatcher.prototype
loadStub = sinon.stub Dispatcher.prototype, 'loadController'

makeLoadController = (name, controller) ->
Expand Down Expand Up @@ -93,6 +94,31 @@ describe 'Dispatcher', ->

# The Tests

it 'should load controller by calling utils.loadModule', ->
stub = sinon.stub utils, 'loadModule'
handler = ->

dispatcher.loadController = loadController
dispatcher.loadController 'a', handler

stub.should.have.been.calledOnce
stub.should.have.been.calledWith 'controllers/a_controller', handler
stub.restore()

it 'should load controller by calling handler if name is object', ->
stub = sinon.stub utils, 'loadModule'
spy = sinon.spy()
name = ->

dispatcher.loadController = loadController
dispatcher.loadController name, spy

spy.should.have.been.calledOnce
spy.should.have.been.calledWith name

stub.should.not.have.been.called
stub.restore()

it 'should dispatch routes to controller actions', ->
proto = Test1Controller.prototype
initialize = sinon.spy proto, 'initialize'
Expand Down
24 changes: 19 additions & 5 deletions test/utils_spec.coffee
Expand Up @@ -111,6 +111,7 @@ describe 'utils', ->
# expect(url).to.be.null

describe 'queryParams', ->
falsy = ['', 0, NaN, false, null, undefined]
{stringify, parse} = utils.querystring

queryParams = p1: 'With space', p2_empty: '', 'p 3': ['999', 'a&b']
Expand Down Expand Up @@ -139,6 +140,17 @@ describe 'utils', ->
expect(stringify queryParams, replacer).to.equal(
'p1=With%20space_&p2_empty=_&p%203=999%2Ca%26b_')

it 'should skip pair if replacer returns falsy value', ->
for value in falsy
expect(stringify queryParams, -> value).to.equal ''

it 'should deserialize query parameters from query string into object', ->
expect(parse queryString).to.deep.equal queryParams

it 'should take a full url and only return params object', ->
url = "http://foo.com/app/path?#{queryString}"
expect(parse url).to.deep.equal queryParams

it 'should deserialize with reviver when provided', ->
reviver = (key, value) ->
if ',' in value
Expand All @@ -151,12 +163,14 @@ describe 'utils', ->
b: ['c', 'd']
e: '4'

it 'should deserialize query parameters from query string into object', ->
expect(parse queryString).to.deep.equal queryParams
it 'should skip pair if reviver returns falsy value', ->
for value in falsy
expect(parse queryString, -> value).to.deep.equal {}

it 'should take a full url and only return params object', ->
url = "http://foo.com/app/path?#{queryString}"
expect(parse url).to.deep.equal queryParams
it 'should skip pair if reviver returns value == null', ->
expect(parse 'a&').to.deep.equal {}
expect(parse 'b=1', -> {value: null}).to.deep.equal {}
expect(parse 'c=2', -> []).to.deep.equal {}

it 'should have old methods', ->
expect(utils.queryParams.stringify).to.be.a 'function'
Expand Down

0 comments on commit 77d2d7b

Please sign in to comment.