"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "vendor/backbone/test/view.js" between
lodash-4.0.0.tar.gz and lodash-4.17.21.tar.gz

About: lodash is a modern JavaScript utility library delivering modularity, performance, & extras. Tagged version.

view.js  (lodash-4.0.0):view.js  (lodash-4.17.21)
(function() { (function(QUnit) {
var view; var view;
QUnit.module('Backbone.View', { QUnit.module('Backbone.View', {
beforeEach: function(assert) { beforeEach: function() {
$('#qunit-fixture').append( $('#qunit-fixture').append(
'<div id="testElement"><h1>Test</h1></div>' '<div id="testElement"><h1>Test</h1></div>'
); );
view = new Backbone.View({ view = new Backbone.View({
id: 'test-view', id: 'test-view',
className: 'test-view', className: 'test-view',
other: 'non-special-option' other: 'non-special-option'
}); });
},
afterEach: function() {
$('#testElement').remove();
$('#test-view').remove();
} }
}); });
QUnit.test('constructor', function(assert) { QUnit.test('constructor', function(assert) {
assert.expect(3); assert.expect(3);
assert.equal(view.el.id, 'test-view'); assert.equal(view.el.id, 'test-view');
assert.equal(view.el.className, 'test-view'); assert.equal(view.el.className, 'test-view');
assert.equal(view.el.other, void 0); assert.equal(view.el.other, void 0);
}); });
QUnit.test('$', function(assert) { QUnit.test('$', function(assert) {
assert.expect(2); assert.expect(2);
var view = new Backbone.View; var myView = new Backbone.View;
view.setElement('<p><a><b>test</b></a></p>'); myView.setElement('<p><a><b>test</b></a></p>');
var result = view.$('a b'); var result = myView.$('a b');
assert.strictEqual(result[0].innerHTML, 'test'); assert.strictEqual(result[0].innerHTML, 'test');
assert.ok(result.length === +result.length); assert.ok(result.length === +result.length);
}); });
QUnit.test('$el', function(assert) { QUnit.test('$el', function(assert) {
assert.expect(3); assert.expect(3);
var view = new Backbone.View; var myView = new Backbone.View;
view.setElement('<p><a><b>test</b></a></p>'); myView.setElement('<p><a><b>test</b></a></p>');
assert.strictEqual(view.el.nodeType, 1); assert.strictEqual(myView.el.nodeType, 1);
assert.ok(view.$el instanceof Backbone.$); assert.ok(myView.$el instanceof Backbone.$);
assert.strictEqual(view.$el[0], view.el); assert.strictEqual(myView.$el[0], myView.el);
}); });
QUnit.test('initialize', function(assert) { QUnit.test('initialize', function(assert) {
assert.expect(1); assert.expect(1);
var View = Backbone.View.extend({ var View = Backbone.View.extend({
initialize: function() { initialize: function() {
this.one = 1; this.one = 1;
} }
}); });
assert.strictEqual(new View().one, 1); assert.strictEqual(new View().one, 1);
}); });
QUnit.test('preinitialize', function(assert) {
assert.expect(1);
var View = Backbone.View.extend({
preinitialize: function() {
this.one = 1;
}
});
assert.strictEqual(new View().one, 1);
});
QUnit.test('preinitialize occurs before the view is set up', function(assert)
{
assert.expect(2);
var View = Backbone.View.extend({
preinitialize: function() {
assert.equal(this.el, undefined);
}
});
var _view = new View({});
assert.notEqual(_view.el, undefined);
});
QUnit.test('render', function(assert) { QUnit.test('render', function(assert) {
assert.expect(1); assert.expect(1);
var view = new Backbone.View; var myView = new Backbone.View;
assert.equal(view.render(), view, '#render returns the view instance'); assert.equal(myView.render(), myView, '#render returns the view instance');
}); });
QUnit.test('delegateEvents', function(assert) { QUnit.test('delegateEvents', function(assert) {
assert.expect(6); assert.expect(6);
var counter1 = 0, counter2 = 0; var counter1 = 0, counter2 = 0;
var view = new Backbone.View({el: '#testElement'}); var myView = new Backbone.View({el: '#testElement'});
view.increment = function(){ counter1++; }; myView.increment = function() { counter1++; };
view.$el.on('click', function(){ counter2++; }); myView.$el.on('click', function() { counter2++; });
var events = {'click h1': 'increment'}; var events = {'click h1': 'increment'};
view.delegateEvents(events); myView.delegateEvents(events);
view.$('h1').trigger('click'); myView.$('h1').trigger('click');
assert.equal(counter1, 1); assert.equal(counter1, 1);
assert.equal(counter2, 1); assert.equal(counter2, 1);
view.$('h1').trigger('click'); myView.$('h1').trigger('click');
assert.equal(counter1, 2); assert.equal(counter1, 2);
assert.equal(counter2, 2); assert.equal(counter2, 2);
view.delegateEvents(events); myView.delegateEvents(events);
view.$('h1').trigger('click'); myView.$('h1').trigger('click');
assert.equal(counter1, 3); assert.equal(counter1, 3);
assert.equal(counter2, 3); assert.equal(counter2, 3);
}); });
QUnit.test('delegate', function(assert) { QUnit.test('delegate', function(assert) {
assert.expect(3); assert.expect(3);
var view = new Backbone.View({el: '#testElement'}); var myView = new Backbone.View({el: '#testElement'});
view.delegate('click', 'h1', function() { myView.delegate('click', 'h1', function() {
assert.ok(true); assert.ok(true);
}); });
view.delegate('click', function() { myView.delegate('click', function() {
assert.ok(true); assert.ok(true);
}); });
view.$('h1').trigger('click'); myView.$('h1').trigger('click');
assert.equal(view.delegate(), view, '#delegate returns the view instance'); assert.equal(myView.delegate(), myView, '#delegate returns the view instance ');
}); });
QUnit.test('delegateEvents allows functions for callbacks', function(assert) { QUnit.test('delegateEvents allows functions for callbacks', function(assert) {
assert.expect(3); assert.expect(3);
var view = new Backbone.View({el: '<p></p>'}); var myView = new Backbone.View({el: '<p></p>'});
view.counter = 0; myView.counter = 0;
var events = { var events = {
click: function() { click: function() {
this.counter++; this.counter++;
} }
}; };
view.delegateEvents(events); myView.delegateEvents(events);
view.$el.trigger('click'); myView.$el.trigger('click');
assert.equal(view.counter, 1); assert.equal(myView.counter, 1);
view.$el.trigger('click'); myView.$el.trigger('click');
assert.equal(view.counter, 2); assert.equal(myView.counter, 2);
view.delegateEvents(events); myView.delegateEvents(events);
view.$el.trigger('click'); myView.$el.trigger('click');
assert.equal(view.counter, 3); assert.equal(myView.counter, 3);
}); });
QUnit.test('delegateEvents ignore undefined methods', function(assert) { QUnit.test('delegateEvents ignore undefined methods', function(assert) {
assert.expect(0); assert.expect(0);
var view = new Backbone.View({el: '<p></p>'}); var myView = new Backbone.View({el: '<p></p>'});
view.delegateEvents({'click': 'undefinedMethod'}); myView.delegateEvents({click: 'undefinedMethod'});
view.$el.trigger('click'); myView.$el.trigger('click');
}); });
QUnit.test('undelegateEvents', function(assert) { QUnit.test('undelegateEvents', function(assert) {
assert.expect(7); assert.expect(7);
var counter1 = 0, counter2 = 0; var counter1 = 0, counter2 = 0;
var view = new Backbone.View({el: '#testElement'}); var myView = new Backbone.View({el: '#testElement'});
view.increment = function(){ counter1++; }; myView.increment = function() { counter1++; };
view.$el.on('click', function(){ counter2++; }); myView.$el.on('click', function() { counter2++; });
var events = {'click h1': 'increment'}; var events = {'click h1': 'increment'};
view.delegateEvents(events); myView.delegateEvents(events);
view.$('h1').trigger('click'); myView.$('h1').trigger('click');
assert.equal(counter1, 1); assert.equal(counter1, 1);
assert.equal(counter2, 1); assert.equal(counter2, 1);
view.undelegateEvents(); myView.undelegateEvents();
view.$('h1').trigger('click'); myView.$('h1').trigger('click');
assert.equal(counter1, 1); assert.equal(counter1, 1);
assert.equal(counter2, 2); assert.equal(counter2, 2);
view.delegateEvents(events); myView.delegateEvents(events);
view.$('h1').trigger('click'); myView.$('h1').trigger('click');
assert.equal(counter1, 2); assert.equal(counter1, 2);
assert.equal(counter2, 3); assert.equal(counter2, 3);
assert.equal(view.undelegateEvents(), view, '#undelegateEvents returns the v iew instance'); assert.equal(myView.undelegateEvents(), myView, '#undelegateEvents returns t he view instance');
}); });
QUnit.test('undelegate', function(assert) { QUnit.test('undelegate', function(assert) {
assert.expect(1); assert.expect(1);
view = new Backbone.View({el: '#testElement'}); var myView = new Backbone.View({el: '#testElement'});
view.delegate('click', function() { assert.ok(false); }); myView.delegate('click', function() { assert.ok(false); });
view.delegate('click', 'h1', function() { assert.ok(false); }); myView.delegate('click', 'h1', function() { assert.ok(false); });
view.undelegate('click'); myView.undelegate('click');
view.$('h1').trigger('click'); myView.$('h1').trigger('click');
view.$el.trigger('click'); myView.$el.trigger('click');
assert.equal(view.undelegate(), view, '#undelegate returns the view instance '); assert.equal(myView.undelegate(), myView, '#undelegate returns the view inst ance');
}); });
QUnit.test('undelegate with passed handler', function(assert) { QUnit.test('undelegate with passed handler', function(assert) {
assert.expect(1); assert.expect(1);
view = new Backbone.View({el: '#testElement'}); var myView = new Backbone.View({el: '#testElement'});
var listener = function() { assert.ok(false); }; var listener = function() { assert.ok(false); };
view.delegate('click', listener); myView.delegate('click', listener);
view.delegate('click', function() { assert.ok(true); }); myView.delegate('click', function() { assert.ok(true); });
view.undelegate('click', listener); myView.undelegate('click', listener);
view.$el.trigger('click'); myView.$el.trigger('click');
}); });
QUnit.test('undelegate with selector', function(assert) { QUnit.test('undelegate with selector', function(assert) {
assert.expect(2); assert.expect(2);
view = new Backbone.View({el: '#testElement'}); var myView = new Backbone.View({el: '#testElement'});
view.delegate('click', function() { assert.ok(true); }); myView.delegate('click', function() { assert.ok(true); });
view.delegate('click', 'h1', function() { assert.ok(false); }); myView.delegate('click', 'h1', function() { assert.ok(false); });
view.undelegate('click', 'h1'); myView.undelegate('click', 'h1');
view.$('h1').trigger('click'); myView.$('h1').trigger('click');
view.$el.trigger('click'); myView.$el.trigger('click');
}); });
QUnit.test('undelegate with handler and selector', function(assert) { QUnit.test('undelegate with handler and selector', function(assert) {
assert.expect(2); assert.expect(2);
view = new Backbone.View({el: '#testElement'}); var myView = new Backbone.View({el: '#testElement'});
view.delegate('click', function() { assert.ok(true); }); myView.delegate('click', function() { assert.ok(true); });
var handler = function(){ assert.ok(false); }; var handler = function() { assert.ok(false); };
view.delegate('click', 'h1', handler); myView.delegate('click', 'h1', handler);
view.undelegate('click', 'h1', handler); myView.undelegate('click', 'h1', handler);
view.$('h1').trigger('click'); myView.$('h1').trigger('click');
view.$el.trigger('click'); myView.$el.trigger('click');
}); });
QUnit.test('tagName can be provided as a string', function(assert) { QUnit.test('tagName can be provided as a string', function(assert) {
assert.expect(1); assert.expect(1);
var View = Backbone.View.extend({ var View = Backbone.View.extend({
tagName: 'span' tagName: 'span'
}); });
assert.equal(new View().el.tagName, 'SPAN'); assert.equal(new View().el.tagName, 'SPAN');
}); });
skipping to change at line 304 skipping to change at line 331
assert.expect(4); assert.expect(4);
var View = Backbone.View.extend({ var View = Backbone.View.extend({
className: 'backboneClass', className: 'backboneClass',
id: 'backboneId', id: 'backboneId',
attributes: { attributes: {
'class': 'attributeClass', 'class': 'attributeClass',
'id': 'attributeId' 'id': 'attributeId'
} }
}); });
var view = new View; var myView = new View;
assert.strictEqual(view.el.className, 'backboneClass'); assert.strictEqual(myView.el.className, 'backboneClass');
assert.strictEqual(view.el.id, 'backboneId'); assert.strictEqual(myView.el.id, 'backboneId');
assert.strictEqual(view.$el.attr('class'), 'backboneClass'); assert.strictEqual(myView.$el.attr('class'), 'backboneClass');
assert.strictEqual(view.$el.attr('id'), 'backboneId'); assert.strictEqual(myView.$el.attr('id'), 'backboneId');
}); });
QUnit.test('multiple views per element', function(assert) { QUnit.test('multiple views per element', function(assert) {
assert.expect(3); assert.expect(3);
var count = 0; var count = 0;
var $el = $('<p></p>'); var $el = $('<p></p>');
var View = Backbone.View.extend({ var View = Backbone.View.extend({
el: $el, el: $el,
events: { events: {
skipping to change at line 347 skipping to change at line 374
QUnit.test('custom events', function(assert) { QUnit.test('custom events', function(assert) {
assert.expect(2); assert.expect(2);
var View = Backbone.View.extend({ var View = Backbone.View.extend({
el: $('body'), el: $('body'),
events: { events: {
fake$event: function() { assert.ok(true); } fake$event: function() { assert.ok(true); }
} }
}); });
var view = new View; var myView = new View;
$('body').trigger('fake$event').trigger('fake$event'); $('body').trigger('fake$event').trigger('fake$event');
$('body').off('fake$event'); $('body').off('fake$event');
$('body').trigger('fake$event'); $('body').trigger('fake$event');
}); });
QUnit.test('#1048 - setElement uses provided object.', function(assert) { QUnit.test('#1048 - setElement uses provided object.', function(assert) {
assert.expect(2); assert.expect(2);
var $el = $('body'); var $el = $('body');
var view = new Backbone.View({el: $el}); var myView = new Backbone.View({el: $el});
assert.ok(view.$el === $el); assert.ok(myView.$el === $el);
view.setElement($el = $($el)); myView.setElement($el = $($el));
assert.ok(view.$el === $el); assert.ok(myView.$el === $el);
}); });
QUnit.test('#986 - Undelegate before changing element.', function(assert) { QUnit.test('#986 - Undelegate before changing element.', function(assert) {
assert.expect(1); assert.expect(1);
var button1 = $('<button></button>'); var button1 = $('<button></button>');
var button2 = $('<button></button>'); var button2 = $('<button></button>');
var View = Backbone.View.extend({ var View = Backbone.View.extend({
events: { events: {
click: function(e) { click: function(e) {
assert.ok(view.el === e.target); assert.ok(myView.el === e.target);
} }
} }
}); });
var view = new View({el: button1}); var myView = new View({el: button1});
view.setElement(button2); myView.setElement(button2);
button1.trigger('click'); button1.trigger('click');
button2.trigger('click'); button2.trigger('click');
}); });
QUnit.test('#1172 - Clone attributes object', function(assert) { QUnit.test('#1172 - Clone attributes object', function(assert) {
assert.expect(2); assert.expect(2);
var View = Backbone.View.extend({ var View = Backbone.View.extend({
attributes: {foo: 'bar'} attributes: {foo: 'bar'}
}); });
skipping to change at line 402 skipping to change at line 429
assert.strictEqual(view1.el.id, 'foo'); assert.strictEqual(view1.el.id, 'foo');
var view2 = new View(); var view2 = new View();
assert.ok(!view2.el.id); assert.ok(!view2.el.id);
}); });
QUnit.test('views stopListening', function(assert) { QUnit.test('views stopListening', function(assert) {
assert.expect(0); assert.expect(0);
var View = Backbone.View.extend({ var View = Backbone.View.extend({
initialize: function() { initialize: function() {
this.listenTo(this.model, 'all x', function(){ assert.ok(false); }); this.listenTo(this.model, 'all x', function() { assert.ok(false); });
this.listenTo(this.collection, 'all x', function(){ assert.ok(false); }) this.listenTo(this.collection, 'all x', function() { assert.ok(false); }
; );
} }
}); });
var view = new View({ var myView = new View({
model: new Backbone.Model, model: new Backbone.Model,
collection: new Backbone.Collection collection: new Backbone.Collection
}); });
view.stopListening(); myView.stopListening();
view.model.trigger('x'); myView.model.trigger('x');
view.collection.trigger('x'); myView.collection.trigger('x');
}); });
QUnit.test('Provide function for el.', function(assert) { QUnit.test('Provide function for el.', function(assert) {
assert.expect(2); assert.expect(2);
var View = Backbone.View.extend({ var View = Backbone.View.extend({
el: function() { el: function() {
return '<p><a></a></p>'; return '<p><a></a></p>';
} }
}); });
var view = new View; var myView = new View;
assert.ok(view.$el.is('p')); assert.ok(myView.$el.is('p'));
assert.ok(view.$el.has('a')); assert.ok(myView.$el.has('a'));
}); });
QUnit.test('events passed in options', function(assert) { QUnit.test('events passed in options', function(assert) {
assert.expect(1); assert.expect(1);
var counter = 0; var counter = 0;
var View = Backbone.View.extend({ var View = Backbone.View.extend({
el: '#testElement', el: '#testElement',
increment: function() { increment: function() {
counter++; counter++;
} }
}); });
var view = new View({ var myView = new View({
events: { events: {
'click h1': 'increment' 'click h1': 'increment'
} }
}); });
view.$('h1').trigger('click').trigger('click'); myView.$('h1').trigger('click').trigger('click');
assert.equal(counter, 2); assert.equal(counter, 2);
}); });
QUnit.test('remove', function(assert) { QUnit.test('remove', function(assert) {
assert.expect(2); assert.expect(2);
var view = new Backbone.View; var myView = new Backbone.View;
document.body.appendChild(view.el); document.body.appendChild(view.el);
view.delegate('click', function() { assert.ok(false); }); myView.delegate('click', function() { assert.ok(false); });
view.listenTo(view, 'all x', function() { assert.ok(false); }); myView.listenTo(myView, 'all x', function() { assert.ok(false); });
assert.equal(view.remove(), view, '#remove returns the view instance'); assert.equal(myView.remove(), myView, '#remove returns the view instance');
view.$el.trigger('click'); myView.$el.trigger('click');
view.trigger('x'); myView.trigger('x');
// In IE8 and below, parentNode still exists but is not document.body. // In IE8 and below, parentNode still exists but is not document.body.
assert.notEqual(view.el.parentNode, document.body); assert.notEqual(myView.el.parentNode, document.body);
}); });
QUnit.test('setElement', function(assert) { QUnit.test('setElement', function(assert) {
assert.expect(3); assert.expect(3);
var view = new Backbone.View({ var myView = new Backbone.View({
events: { events: {
click: function() { assert.ok(false); } click: function() { assert.ok(false); }
} }
}); });
view.events = { myView.events = {
click: function() { assert.ok(true); } click: function() { assert.ok(true); }
}; };
var oldEl = view.el; var oldEl = myView.el;
var $oldEl = view.$el; var $oldEl = myView.$el;
view.setElement(document.createElement('div')); myView.setElement(document.createElement('div'));
$oldEl.click(); $oldEl.click();
view.$el.click(); myView.$el.click();
assert.notEqual(oldEl, view.el); assert.notEqual(oldEl, myView.el);
assert.notEqual($oldEl, view.$el); assert.notEqual($oldEl, myView.$el);
}); });
})(); })(QUnit);
 End of changes. 56 change blocks. 
116 lines changed or deleted 144 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)