Skip to content

Commit

Permalink
Merge pull request #161 from bem/jsfix
Browse files Browse the repository at this point in the history
Fix bug with `ctx.js()`
  • Loading branch information
mishanga committed Aug 21, 2015
2 parents bccaa4f + b9e11f4 commit 710faad
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
10 changes: 4 additions & 6 deletions lib/bh.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,12 +435,10 @@ function BH() {
* @returns {Boolean|Object|Ctx}
*/
js: function(js, force) {
if (js === undefined) {
return this.ctx.js;
}
this.ctx.js = force ?
(js === true ? {} : js) :
js ? this.extend(this.ctx.js, js) : this.ctx.js;
var ctx = this.ctx;
if (js === undefined) return ctx.js;
if (force || ctx.js === undefined) ctx.js = js;
else if (ctx.js !== false) this.extend(ctx.js, js);
return this;
},
/**
Expand Down
19 changes: 18 additions & 1 deletion test/test.js.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ describe('ctx.js()', function() {
bh.apply({ block: 'button' }).should.equal('<div class="button"></div>');
});

it('should not override user declarations', function() {
bh.match('button', function(ctx) {
ctx.js(true);
});
bh.apply({ block: 'button', js: false }).should.equal('<div class="button"></div>');
});

it('should extend user js', function() {
bh.match('button', function(ctx) {
ctx.js({ a: 2 });
Expand All @@ -36,7 +43,7 @@ describe('ctx.js()', function() {
.should.equal('<div class="button i-bem" onclick=\'return {"button":{"x":1,"a":2}}\'></div>');
});

it('should not override later declarations', function() {
it('should not override later declarations #1', function() {
bh.match('button', function(ctx) {
ctx.js(false);
});
Expand All @@ -46,6 +53,16 @@ describe('ctx.js()', function() {
bh.apply({ block: 'button' }).should.equal('<div class="button i-bem" onclick=\'return {"button":{}}\'></div>');
});

it('should not override later declarations #2', function() {
bh.match('button', function(ctx) {
ctx.js(true);
});
bh.match('button', function(ctx) {
ctx.js(false);
});
bh.apply({ block: 'button' }).should.equal('<div class="button"></div>');
});

it('should override later declarations with force flag', function() {
bh.match('button', function(ctx) {
ctx.js(true, true);
Expand Down

0 comments on commit 710faad

Please sign in to comment.