Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure $el contains only the el #3426

Merged
merged 1 commit into from
Aug 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/region.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ const Region = MarionetteObject.extend({
if (!_.isObject(this.el)) {
this.$el = this.getEl(this.el);
this.el = this.$el[0];
// Make sure the $el contains only the el
this.$el = this.Dom.getEl(this.el);
}

if (!this.$el || this.$el.length === 0) {
Expand Down Expand Up @@ -241,7 +243,7 @@ const Region = MarionetteObject.extend({
// Override this method to change how the new view is appended to the `$el` that the
// region is managing
attachHtml(view) {
this.Dom.appendContents(this.el, view.el, {_$contents: view.$el});
this.Dom.appendContents(this.el, view.el, {_$el: this.$el, _$contents: view.$el});
},

// Destroy the current view, if there is one. If there is no current view, it does
Expand Down
55 changes: 36 additions & 19 deletions test/unit/common/build-region.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,29 +115,46 @@ describe('Region', function() {
expect(this.region.el).to.equal(this.fooSelector);
});

describe('with `parentEl` also defined including the selector', function() {
beforeEach(function() {
this.setFixtures('<div id="parent"><div id="child">text</div></div>');
this.parentEl = $('#parent');
this.definition = _.defaults({parentEl: this.parentEl, el: '#child' }, this.definition);
this.region = this.view.addRegion(_.uniqueId('region_'),this.definition);
});

it('returns the jQuery(el)', function() {
expect(this.region.getEl(this.region.el).text()).to.equal($(this.region.el).text());
describe('with `parentEl` also defined', function() {
describe('including the selector', function() {
beforeEach(function() {
this.setFixtures('<div id="parent"><div id="child">text</div></div>');
this.parentEl = $('#parent');
this.definition = _.defaults({parentEl: this.parentEl, el: '#child' }, this.definition);
this.region = this.view.addRegion(_.uniqueId('region_'),this.definition);
});

it('returns the jQuery(el)', function() {
expect(this.region.getEl(this.region.el).text()).to.equal($(this.region.el).text());
});
});
});

describe('with `parentEl` also defined excluding the selector', function() {
beforeEach(function() {
this.setFixtures('<div id="parent"></div><div id="not-child">text</div>');
this.parentEl = $('#parent');
this.definition = _.defaults({parentEl: this.parentEl, el: '#not-child' }, this.definition);
this.region = this.view.addRegion(_.uniqueId('region_'),this.definition);
describe('excluding the selector', function() {
beforeEach(function() {
this.setFixtures('<div id="parent"></div><div id="not-child">text</div>');
this.parentEl = $('#parent');
this.definition = _.defaults({parentEl: this.parentEl, el: '#not-child' }, this.definition);
this.region = this.view.addRegion(_.uniqueId('region_'),this.definition);
});

it('returns the jQuery(el)', function() {
expect(this.region.getEl(this.region.el).text()).to.not.equal($(this.region.el).text());
});
});

it('returns the jQuery(el)', function() {
expect(this.region.getEl(this.region.el).text()).to.not.equal($(this.region.el).text());
describe('including multiple instances of the selector', function() {
beforeEach(function() {
this.setFixtures('<div id="parent"><div class="child">text</div><div class="child">text</div></div>');
this.parentEl = $('#parent');
this.definition = _.defaults({parentEl: this.parentEl, el: '.child' }, this.definition);
this.region = this.view.addRegion(_.uniqueId('region_'),this.definition);
});

it('should ensure a jQuery(el) of length 1', function() {
// calls _ensureElement
this.region.empty();
expect(this.region.$el.length).to.equal(1);
});
});
});
});
Expand Down