Skip to content

Commit

Permalink
Merge pull request #4 from Florian-R/bb-view-hooks
Browse files Browse the repository at this point in the history
Fix failing tests without jQuery
  • Loading branch information
akre54 committed Oct 13, 2015
2 parents a455973 + 71551c1 commit 567c10c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 64 deletions.
4 changes: 3 additions & 1 deletion src/chaplin/views/collection_view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ module.exports = class CollectionView extends View
# Set the listEl property with the actual list container.
listSelector = _.result this, 'listSelector'

@listEl = if listSelector then @$(listSelector)[0] else @el
@listEl = if listSelector then @el.querySelector(listSelector) else @el
@$list = Backbone.$ @listEl if Backbone.$

@initFallback()
Expand Down Expand Up @@ -226,6 +226,7 @@ module.exports = class CollectionView extends View

# Set the $fallback property.
@$fallback = @$ @fallbackSelector
@fallback = @el.querySelector @fallbackSelector

# Listen for visible items changes.
@on 'visibilityChange', @toggleFallback
Expand Down Expand Up @@ -259,6 +260,7 @@ module.exports = class CollectionView extends View

# Set the $loading property.
@$loading = @$ @loadingSelector
@loading = @el.querySelector @loadingSelector

# Listen for sync events on the collection.
@listenTo @collection, 'syncStateChange', @toggleLoadingIndicator
Expand Down
17 changes: 6 additions & 11 deletions src/chaplin/views/layout.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ module.exports = class Layout extends View
skipRouting = @settings.skipRouting
type = typeof skipRouting
return if type is 'function' and not skipRouting(href, el) or
type is 'string' and (if Backbone.$ then Backbone.$(el).is(skipRouting) else el.matches skipRouting)
type is 'string' and utils.matchesSelector.call el, skipRouting

# Handle external links.
external = isAnchor and @isExternalLink el
Expand Down Expand Up @@ -203,18 +203,13 @@ module.exports = class Layout extends View

# Apply the region selector.
instance.container = if region.selector is ''
if Backbone.$
region.instance.$el
else
region.instance.el
region.instance.el
else
if region.instance.noWrap
if Backbone.$
Backbone.$(region.instance.container).find region.selector
root = if region.instance.noWrap
region.instance.container
else
region.instance.container.querySelector region.selector
else
region.instance.$ region.selector
region.instance.el
root.querySelector region.selector

# Disposal
# --------
Expand Down
2 changes: 1 addition & 1 deletion src/chaplin/views/view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ module.exports = class View extends Backbone.View
handler = if typeof value is 'function' then value else this[value]
throw new Error "Method '#{value}' does not exist" unless handler
match = key.match /^(\S+)\s*(.*)$/
eventName = "#{match[1]}.delegateEvents#{@cid}"
eventName = match[1]
selector = match[2]
bound = _.bind handler, this
@delegate eventName, (selector or null), bound
Expand Down
60 changes: 31 additions & 29 deletions test/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ var testType = window.testType || (match ? match[1] : 'backbone');
var useDeps = window.useDeps || (match ? match[2] : true);

var addDeps = function() {
if (useDeps) {
paths.underscore = '../' + componentsFolder + '/lodash/lodash.compat';
if (useDeps === true) {
paths.jquery = '../' + componentsFolder + '/jquery/jquery';
paths.underscore = '../' + componentsFolder + '/lodash/lodash.compat';
} else {
paths.NativeView = '../' + componentsFolder + '/Backbone.NativeView/backbone.nativeview';
paths.NativeView = '../' + componentsFolder + '/backbone.nativeview/backbone.nativeview';
}
};
if (testType === 'backbone') {
Expand All @@ -29,33 +29,17 @@ if (testType === 'backbone') {
paths.backbone = '../' + componentsFolder + '/exoskeleton/exoskeleton';
}


var config = {
baseUrl: 'temp/',
paths: paths,
// For easier development, disable browser caching
urlArgs: 'bust=' + (new Date()).getTime()
};

if (testType === 'backbone' || testType === 'deps') {
config.shim = {
backbone: {
deps: ['underscore', 'jquery'],
exports: 'Backbone'
},
underscore: {
exports: '_'
}
};
}

requirejs.config(config);
if (testType === 'exos') {
define('jquery', function(){});
define('underscore', ['backbone'], function(Backbone){
var _ = Backbone.utils;
_.bind = function(fn, ctx) { return fn.bind(ctx); }
return _;
});
}
mocha.setup({ui: 'bdd', ignoreLeaks: true});
// Wonderful hack to send a message to grunt from inside a mocha test.
Expand Down Expand Up @@ -107,14 +91,32 @@ window.addEventListener('DOMContentLoaded', function() {
}
};

require(specs, function() {
if (useDeps) {
run();
} else {
require(['backbone', 'NativeView'], function(Backbone, NativeView) {
Backbone.View = NativeView;
run();
if (useDeps === true) {
require(specs, run)
} else {
define('underscore', function(){});
require(['backbone', 'NativeView'], function(Backbone, NativeView) {
requirejs.undef('underscore')
define('underscore', function(){
var _ = Backbone.utils
_.bind = function(fn, ctx) { return fn.bind(ctx); }
_.isObject = function(obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
};
_.clone = function(obj) {
if (!_.isObject(obj)) return obj;
return Array.isArray(obj) ? obj.slice() : _.extend({}, obj);
};
_.isEmpty = function(obj) {
if (obj == null) return true;
if (obj.length !== undefined) return obj.length === 0;
return Object.keys(obj).length === 0;
};
return _
});
}
});
Backbone.View = NativeView;
require(specs, run)
});
}
}, false);
17 changes: 9 additions & 8 deletions test/spec/collection_view_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ define [
'chaplin/views/view'
'chaplin/views/collection_view'
'chaplin/lib/sync_machine'
], (_, jQuery, Model, Collection, View, CollectionView, SyncMachine) ->
'chaplin/lib/utils'
], (_, jQuery, Model, Collection, View, CollectionView, SyncMachine, utils) ->
'use strict'

jQuery = null unless jQuery?.fn
Expand Down Expand Up @@ -86,9 +87,9 @@ define [
collectionView.$list.children collectionView.itemSelector
else
if collectionView.itemSelector
(item for item in collectionView.list.children when Backbone.utils.matchesSelector item, collectionView.itemSelector)
(item for item in collectionView.listEl.children when utils.matchesSelector.call item, collectionView.itemSelector)
else
collectionView.list.children
collectionView.listEl.children

getAllChildren = ->
if jQuery
Expand Down Expand Up @@ -176,7 +177,7 @@ define [
expect(collectionView.$list).to.be.a jQuery
expect(collectionView.$list.length).to.be 1
else
expect(collectionView.list).to.be.a Element
expect(collectionView.listEl).to.be.a Element

collectionView.renderAllItems()
viewsMatchCollection()
Expand Down Expand Up @@ -735,10 +736,10 @@ define [
children = getViewChildren()
expect(children.length).to.be collection.length
else
list = collectionView.list
list = collectionView.listEl
expect(list).to.be.true

list2 = collectionView.find(collectionView.listSelector)
list2 = collectionView.el.querySelector(collectionView.listSelector)
expect(list).to.be list2

children = getViewChildren()
Expand Down Expand Up @@ -788,7 +789,7 @@ define [
else
{fallback} = collectionView
expect(fallback).to.be.true
fallback2 = collectionView.find(collectionView.fallbackSelector)
fallback2 = collectionView.el.querySelector(collectionView.fallbackSelector)
expect(fallback).to.be fallback2

it 'should show the fallback element properly', ->
Expand Down Expand Up @@ -857,7 +858,7 @@ define [
else
{loading} = collectionView
expect(loading).to.be.true
loading2 = collectionView.find(collectionView.loadingSelector)
loading2 = collectionView.el.querySelector(collectionView.loadingSelector)
expect(loading).to.be loading2

it 'should show the loading indicator properly', ->
Expand Down
20 changes: 6 additions & 14 deletions test/spec/layout_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,8 @@ define [
instance2 = new Test2View {region: 'test2'}
instance3 = new Test2View {region: 'test0'}

if $
expect(instance2.container.attr('id')).to.be 'test2'
expect(instance3.container).to.be instance1.$el
else
expect(instance2.container.id).to.be 'test2'
expect(instance3.container).to.be instance1.el
expect(instance2.container.id).to.be 'test2'
expect(instance3.container).to.be instance1.el

instance1.dispose()
instance2.dispose()
Expand All @@ -375,10 +371,8 @@ define [
instance1 = new Test1View()
instance2 = new Test2View()
instance3 = new Test3View {region: 'test2'}
if $
expect(instance3.container.attr('id')).to.be 'test5'
else
expect(instance3.container.id).to.be 'test5'

expect(instance3.container.id).to.be 'test5'

instance1.dispose()
instance2.dispose()
Expand Down Expand Up @@ -407,10 +401,8 @@ define [
instance2 = new Test2View()
instance2.stale = true
instance3 = new Test3View {region: 'test2'}
if $
expect(instance3.container.attr('id')).to.be 'test2'
else
expect(instance3.container.id).to.be 'test2'

expect(instance3.container.id).to.be 'test2'

instance1.dispose()
instance2.dispose()
Expand Down

0 comments on commit 567c10c

Please sign in to comment.