Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

more tests for leave()

  • Loading branch information...
commit a324d8173fb6510a243b133c2712d170f3bcf0ac 1 parent 4eb2052
@nickrivadeneira nickrivadeneira authored
View
51 lib/assets/backbone-support/composite_view.js
@@ -7,15 +7,15 @@ _.extend(Support.CompositeView.prototype, Backbone.View.prototype, {
leave: function() {
this.unbind();
this.remove();
- // this._leaveChildren();
- // this._removeFromParent();
- }
+ this._leaveChildren();
+ this._removeFromParent();
+ },
- //renderChild: function(view) {
- // view.render();
- // this.children.push(view);
- // view.parent = this;
- //},
+ renderChild: function(view) {
+ view.render();
+ this.children.push(view);
+ view.parent = this;
+ },
//appendChild: function(view) {
// this.renderChild(view);
@@ -27,25 +27,22 @@ _.extend(Support.CompositeView.prototype, Backbone.View.prototype, {
// $(container).html('').append(view.el);
//},
- //_leaveChildren: function() {
- // var clonedChildren = this.children.slice(0);
- // _.each(clonedChildren, function(view) {
- // if (view.leave) {
- // view.leave();
- // }
- // });
- //},
-
- //_removeFromParent: function() {
- // if (this.parent) {
- // this.parent.removeChild(this);
- // }
- //},
-
- //removeChild: function(view) {
- // var index = this.children.indexOf(view);
- // this.children.splice(index, 1);
- //}
+ _leaveChildren: function() {
+ _(this.children).chain().clone().each(function(view) {
+ if (view.leave)
+ view.leave();
+ });
+ },
+
+ _removeFromParent: function() {
+ if (this.parent)
+ this.parent._removeChild(this);
+ },
+
+ _removeChild: function(view) {
+ var index = this.children.indexOf(view);
+ this.children.splice(index, 1);
+ }
});
Support.CompositeView.extend = Backbone.View.extend;
View
99 spec/javascripts/composite_view_spec.js
@@ -1,4 +1,4 @@
-describe("CompositeView", function() {
+describe("Support.CompositeView", function() {
var orangeView = Support.CompositeView.extend({
render: function() {
var text = this.make("span", {}, "Orange!");
@@ -6,41 +6,106 @@ describe("CompositeView", function() {
}
});
+ var blankView = Support.CompositeView.extend({
+ render: function() {
+ }
+ });
+
+ var normalView = Backbone.View.extend({
+ render: function() {
+ var text = this.make("span", {}, "Normal!");
+ $(this.el).append(text);
+ }
+ });
+
beforeEach(function() {
Helpers.setup();
- $("body").append("<div id='test'></div>");
+ Helpers.append("test1");
+ Helpers.append("test2");
});
afterEach(function() {
Helpers.teardown();
});
- it("removes elements and events when leave() is called", function() {
- var view = new orangeView();
- var spy = sinon.spy(view, "unbind");
+ describe("#renderChild", function() {
+ it("renders children views", function() {
+ var view = new blankView();
+ view.renderChild(new orangeView({el: "#test1"}));
+ view.renderChild(new orangeView({el: "#test2"}));
+
+ expect($("#test1").text()).toEqual("Orange!");
+ expect($("#test2").text()).toEqual("Orange!");
+ });
+ });
+
+ describe("#leave", function() {
+ it("removes elements and events when leave() is called", function() {
+ var view = new orangeView();
+ var spy = sinon.spy(view, "unbind");
+
+ runs(function() {
+ view.render();
+ $("#test").append(view.el);
+ });
+
+ Helpers.sleep();
+
+ runs(function() {
+ expect($("#test").text()).toEqual("Orange!");
+ });
+
+ Helpers.sleep();
+
+ runs(function() {
+ view.leave();
+ });
- runs(function() {
- view.render();
- $("#test").append(view.el);
+ Helpers.sleep();
+
+ runs(function() {
+ expect($("#test").text()).toEqual("");
+ expect(spy.called).toBeTruthy();
+ });
});
- Helpers.sleep();
+ it("removes children views on leave", function() {
+ var view = new blankView();
+ view.renderChild(new orangeView({el: "#test1"}));
+ view.renderChild(new orangeView({el: "#test2"}));
+
+ view.leave();
- runs(function() {
- expect($("#test").text()).toEqual("Orange!");
+ expect($("#test1").size()).toEqual(0);
+ expect($("#test2").size()).toEqual(0);
});
- Helpers.sleep();
+ it("doesn't fail on normal backbone views that may be children", function() {
+ var view = new blankView();
+ view.renderChild(new orangeView({el: "#test1"}));
+ view.renderChild(new normalView({el: "#test2"}));
- runs(function() {
view.leave();
+
+ expect($("#test1").size()).toEqual(0);
+ expect($("#test2").size()).toEqual(1);
});
- Helpers.sleep();
+ it("removes self from parent if invoked on a child view", function() {
+ var view = new blankView();
+ var childView = new orangeView({el: "#test1"});
+ view.renderChild(childView)
+ view.renderChild(new orangeView({el: "#test2"}));
+
+ expect($("#test1").size()).toEqual(1);
+ expect($("#test2").size()).toEqual(1);
+ expect(view.children.length).toEqual(2);
+
+ childView.leave();
- runs(function() {
- expect($("#test").text()).toEqual("");
- expect(spy.called).toBeTruthy();
+ expect($("#test1").size()).toEqual(0);
+ expect($("#test2").size()).toEqual(1);
+ expect(view.children.length).toEqual(1);
});
});
});
View
8 spec/javascripts/helpers/helpers.js
@@ -5,11 +5,15 @@ var Helpers = {
setup: function() {
this.teardown();
- $("body").append("<div id='test'></div>");
+ this.append("test");
},
teardown: function() {
window.location.hash = "#"
- $("#test").remove();
+ $(".test").remove();
+ },
+
+ append: function(id) {
+ $("body").append("<div class='test' id='" + id + "'></div>");
}
};
View
3  spec/javascripts/swapping_router_spec.js
@@ -46,13 +46,12 @@ describe("SwappingRouter", function() {
beforeEach(function() {
Helpers.setup();
- $("body").append("<div id='test'></div>");
if(!historyStarted) {
historyStarted = true;
Backbone.history.start();
}
- router.el = $("#test");
+ router.el = "#test";
});
afterEach(function() {
Please sign in to comment.
Something went wrong with that request. Please try again.