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

renderMix: fix nestedMix string or object value, add tests #551

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 4 additions & 0 deletions lib/bemhtml/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ BEMHTML.prototype.renderMix = function(entity, mix, jsParams, addJSInitClass) {
if (!nestedMix)
continue;

// Transform nestedMix to the single-item array if it's not array
if (!Array.isArray(nestedMix))
nestedMix = [ nestedMix ];

for (var j = 0; j < nestedMix.length; j++) {
var nestedItem = nestedMix[j];
if (!nestedItem) continue;
Expand Down
139 changes: 137 additions & 2 deletions test/bemjson-mix-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ var fixtures = require('./fixtures')('bemhtml');
var test = fixtures.test;

describe('BEMJSON mix', function() {
it('should support mix in json', function() {
it('should support string mix in json', function() {
test(function() {},
{
block: 'b1',
mix: 'b2'
},
'<div class="b1 b2"></div>');
});

it('should support object mix in json', function() {
test(function() {},
{
block: 'b1',
Expand All @@ -11,6 +20,28 @@ describe('BEMJSON mix', function() {
'<div class="b1 b2"></div>');
});

it('should support array with string mix in json', function() {
test(function() {},
{
block: 'b1',
mix: [
'b2'
]
},
'<div class="b1 b2"></div>');
});

it('should support array with object mix in json', function() {
test(function() {},
{
block: 'b1',
mix: [
{ block: 'b2' }
]
},
'<div class="b1 b2"></div>');
});

it('should not propagate parent elem to JS params', function() {
test(function() {},
{
Expand Down Expand Up @@ -240,7 +271,7 @@ describe('BEMJSON mix', function() {
});

describe('nested mixes', function() {
it('should support nested mix', function() {
it('should support nested mix in json', function() {
test(function() {},
{
block: 'b1',
Expand All @@ -260,6 +291,110 @@ describe('BEMJSON mix', function() {
'<div class="b1 b2__e b3 b3_test_opa b4 b5"></div>');
});

it('should support string mix ' +
'with nested string mix in bemhtml', function() {
test(function() {
block('b2')(
addMods()('b3')
);
}, {
block: 'b1',
mix: 'b2'
}, '<div class="b1 b2 b3"></div>');
});

it('should support string mix ' +
'with nested object mix in bemhtml', function() {
test(function() {
block('b2')(
addMods()({ block: 'b3' })
);
}, {
block: 'b1',
mix: 'b2'
}, '<div class="b1 b2 b3"></div>');
});

it('should support string mix ' +
'with nested array of strings mix in bemhtml', function() {
test(function() {
block('b2')(
addMods()([ 'b3' ])
);
}, {
block: 'b1',
mix: 'b2'
}, '<div class="b1 b2 b3"></div>');
});

it('should support string mix ' +
'with nested array of objects mix in bemhtml', function() {
test(function() {
block('b2')(
addMods()([ { block: 'b3' } ])
);
}, {
block: 'b1',
mix: 'b2'
}, '<div class="b1 b2 b3"></div>');
});

it('should support object mix ' +
'with nested string mix in bemhtml', function() {
test(function() {
block('b2')(
addMods()('b3')
);
}, {
block: 'b1',
mix: {
block: 'b2'
}
}, '<div class="b1 b2 b3"></div>');
});

it('should support object mix ' +
'with nested object mix in bemhtml', function() {
test(function() {
block('b2')(
addMods()({ block: 'b3' })
);
}, {
block: 'b1',
mix: {
block: 'b2'
}
}, '<div class="b1 b2 b3"></div>');
});

it('should support object mix ' +
'with nested array of strings mix in bemhtml', function() {
test(function() {
block('b2')(
addMods()([ 'b3' ])
);
}, {
block: 'b1',
mix: {
block: 'b2'
}
}, '<div class="b1 b2 b3"></div>');
});

it('should support object mix ' +
'with nested array of objects mix in bemhtml', function() {
test(function() {
block('b2')(
addMods()([ { block: 'b3' } ])
);
}, {
block: 'b1',
mix: {
block: 'b2'
}
}, '<div class="b1 b2 b3"></div>');
});

it('should support nested mix with js params', function() {
test(function() {},
{
Expand Down