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

More tests for coverage #405

Merged
merged 1 commit into from
Jan 25, 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
10 changes: 2 additions & 8 deletions lib/bemhtml/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ BEMHTML.prototype.render = function render(context,

var isBEM = bem;
if (isBEM === undefined) {
if (ctx.bem === undefined)
isBEM = entity.block || entity.elem;
else
isBEM = ctx.bem;
isBEM = entity.block || entity.elem;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of bem in line 82 is result of bem() mode execution. And bem() returns this.ctx.bem by default.

}
isBEM = !!isBEM;

Expand All @@ -93,7 +90,7 @@ BEMHTML.prototype.render = function render(context,

var addJSInitClass = jsParams && (
this._elemJsInstances ?
(entity.block || entity.elem) :
entity.block :
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of entity.block is always available.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure it's useless case but it works now:

BEMHTML.apply({ elem: 'e1' }); // '<div class="__e1"></div>'

(entity.block && !entity.elem)
);

Expand Down Expand Up @@ -284,9 +281,6 @@ BEMHTML.prototype.renderMix = function renderMix(entity,
context.elem = oldElem;
context.block = oldBlock;

if (!nestedMix)
continue;

for (var j = 0; j < nestedMix.length; j++) {
var nestedItem = nestedMix[j];
if (!nestedItem) continue;
Expand Down
6 changes: 1 addition & 5 deletions lib/bemxjst/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var ClassBuilder = require('./class-builder').ClassBuilder;
var utils = require('./utils');

function BEMXJST(options) {
this.options = options || {};
this.options = options;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


this.entities = null;
this.defaultEnt = null;
Expand Down Expand Up @@ -117,10 +117,6 @@ BEMXJST.prototype.compile = function compile(code) {

BEMXJST.prototype.recompileInput = function recompileInput(code) {
var args = BEMXJST.prototype.locals;
// Reuse function if it already has right arguments
if (typeof code === 'function' && code.length === args.length)
return code;

var out = code.toString();

// Strip the function
Expand Down
55 changes: 55 additions & 0 deletions test/api-compile-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,60 @@ describe('API compile', function() {
template.apply({ block: 'b1' });
});
});

it('should throw bem-xjst error on template with no block', function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without block

assert.throws(function() {
bemxjst.compile(function() {
attrs()(function() { return { a: 1 }; });
});
});
});
});

describe('Runtime lint mode', function() {
function captureStream(stream) {
var oldWrite = stream.write;
var buf = '';

stream.write = function(chunk) {
buf += chunk.toString();
oldWrite.apply(stream, arguments);
};

return {
unhook: function unhook() { stream.write = oldWrite; },
captured: function() { return buf; }
};
}

var hook;
beforeEach(function() { hook = captureStream(process.stderr); });
afterEach(function() { hook.unhook(); });

it('should work with runtimeLint option', function() {
var template = bemxjst.compile(function() {
block('b__e').content()('test');
}, { runtimeLint: true });

template.apply({ block: 'b__e' });

var stderr = hook.captured();
assert.equal(
stderr.substr(0, 17),
'\nBEM-XJST WARNING'
);
});
});

describe('context option', function() {
it('should work with context option', function() {
assert.doesNotThrow(function() {
var template = bemxjst.compile(function() {
block('b').content()('test');
}, { context: 'this' });

template.apply({ block: 'b' });
});
});
});
});
6 changes: 6 additions & 0 deletions test/bemhtml-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ describe('BEMHTML engine tests', function() {
.apply({ block: 'b', tag: 'br' })
.should.equal('<br class="b">');
});

it('should close short tags with xhtml: true', function() {
compile(function() {}, { xhtml: true })
.apply({ block: 'b', tag: 'br' })
.should.equal('<br class="b"/>');
});
});

describe('omitOptionalEndTags option', function() {
Expand Down
12 changes: 10 additions & 2 deletions test/bemjson-js-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ describe('BEMJSON js', function() {
'Nancy&#39;","b":"&#39;Tom&amp;Jerry&#39;"}}\'></div>');
});

it('should render js data attribute in mixed', function() {
compile(function() {})
.apply({ block: 'b', mix: [ { block: 'a', js: { data: 1 } } ] })
.should
.equal('<div class="b a i-bem" ' +
'data-bem=\'{"a":{"data":1}}\'></div>');
});

describe('elemJsInstances option', function() {
it('should not render i-bem for elems be default', function() {
compile('')
Expand All @@ -95,11 +103,11 @@ describe('BEMJSON js', function() {

tmpls.apply({
block: 'a',
js: true,
js: { data: 1 },
mix: { block: 'b', elem: 'e', js: true }
})
.should.equal('<div class="a b__e i-bem"' +
' data-bem=\'{"a":{},"b__e":{}}\'></div>');
' data-bem=\'{"a":{"data":1},"b__e":{}}\'></div>');

tmpls.apply({
block: 'b',
Expand Down
9 changes: 8 additions & 1 deletion test/bemjson-mods-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,18 @@ describe('BEMJSON mods', function() {
}, '<div class="b1 b1__e1"></div>');
});

it('should not inherit mods from namesake parent block', function () {
it('should not inherit mods from namesake parent block', function() {
test(function() {}, {
block: 'b1',
mods: { a: 1 },
content: { block: 'b1' }
}, '<div class="b1 b1_a_1"><div class="b1"></div></div>');
});

it('should not render epmty modName mods', function() {
test(function() {}, {
block: 'b',
mods: { a: 1, '': 2 }
}, '<div class="b b_a_1"></div>');
});
});
6 changes: 6 additions & 0 deletions test/bemjson-tag-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ describe('BEMJSON tag', function() {
test(function() {
}, { tag: false, content: 'ok' }, 'ok');
});

it('should render empty string ' +
'if block with no content and no tag', function() {
test(function() {
}, { block: 'test', tag: false }, '');
});
});