Skip to content

Commit

Permalink
Support latest bem-walker format
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKashlakov committed Jul 4, 2016
1 parent 6ab90b6 commit bf333b9
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 53 deletions.
2 changes: 1 addition & 1 deletion lib/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Filter.prototype = {
* @private
*/
_byBlock: function(item) {
return this._shouldPass(item, 'blocks', 'block');
return this._shouldPass(item.entity, 'blocks', 'block');
},

/**
Expand Down
13 changes: 2 additions & 11 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

var _ = require('lodash'),
Filter = require('./filter'),
var Filter = require('./filter'),
Criteria = require('./criteria/criteria'),
CriteriaCollection = require('./criteria/criteria-collection');

Expand Down Expand Up @@ -95,16 +94,8 @@ exports.getFiltersForConditions = function(conditions) {
return new Filter(criteria);
}),
apply = function(item) {
// Prepare BEM entity for check
var prepareBemEntity = function(item) {
var bemEntity = _.omit(item, _.isObject);
return _.reduce(_.values(_.pick(item, _.isObject)), function(result, currentItem) {
return _.merge(result, currentItem);
}, bemEntity);
};

return filters.some(function(filter) {
return filter.apply(prepareBemEntity(item));
return filter.apply(item);
});
};

Expand Down
94 changes: 79 additions & 15 deletions test/lib/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,56 @@ var Filter = require('../../lib/filter');

describe('Filter', function() {
describe('apply', function() {
testBySingleCriteria_('blocks', 'block');
testBySingleCriteria_('elements', 'elem');
testBySingleCriteria_('modifiers', 'modName');
testBySingleCriteria_('techs', 'tech');

it('should pass block item if no info about blocks provided in config', function() {
var filter = new Filter({}),
item = {
entity: {
block: 'foo'
}
};
assert.equal(filter.apply(item), true);
});

it('should pass block item if block matches blocks to search', function() {
var opts = {
blocks: 'foo'
};
var filter = new Filter(opts),
item = {
entity: {
block: 'foo'
}
};
assert.equal(filter.apply(item), true);
});

it('should not pass block item if block not match blocks to search', function() {
var opts = {
blocks: ['foo']
};
var filter = new Filter(opts),
item = {
entity: {
block: 'bar'
}
};

assert.equal(filter.apply(item), false);
});

it('should not pass if matches block, but not matches elem', function() {
var filter = new Filter({
blocks: ['foo'],
elements: ['bar']
}),
item = {
block: 'foo',
entity: {
block: 'foo'
},
elem: 'baz'
};

Expand All @@ -28,7 +66,9 @@ describe('Filter', function() {
elements: ['bar']
}),
item = {
block: 'foo',
entity: {
block: 'foo'
},
elem: 'bar'
};

Expand All @@ -41,7 +81,9 @@ describe('Filter', function() {
modifiers: ['bar']
}),
item = {
block: 'foo',
entity: {
block: 'foo'
},
modName: 'baz'
};

Expand All @@ -54,7 +96,9 @@ describe('Filter', function() {
modifiers: ['bar']
}),
item = {
block: 'foo',
entity: {
block: 'foo'
},
modName: 'bar'
};

Expand All @@ -67,7 +111,9 @@ describe('Filter', function() {
techs: ['bar']
}),
item = {
block: 'foo',
entity: {
block: 'foo'
},
tech: 'baz'
};

Expand All @@ -80,7 +126,9 @@ describe('Filter', function() {
techs: ['bar']
}),
item = {
block: 'foo',
entity: {
block: 'foo'
},
tech: 'bar'
};

Expand All @@ -94,7 +142,9 @@ describe('Filter', function() {
modifiers: ['fizz']
}),
item = {
block: 'foo',
entity: {
block: 'foo'
},
elem: 'bar',
modName: 'buzz'
};
Expand All @@ -109,7 +159,9 @@ describe('Filter', function() {
modifiers: ['fizz']
}),
item = {
block: 'foo',
entity: {
block: 'foo'
},
elem: 'bar',
modName: 'fizz'
};
Expand All @@ -125,7 +177,9 @@ describe('Filter', function() {
techs: ['buzz']
}),
item = {
block: 'foo',
entity: {
block: 'foo'
},
elem: 'bar',
modName: 'fizz',
tech: 'BAZ'
Expand All @@ -142,7 +196,9 @@ describe('Filter', function() {
techs: ['buzz']
}),
item = {
block: 'foo',
entity: {
block: 'foo'
},
elem: 'bar',
modName: 'fizz',
tech: 'buzz'
Expand All @@ -158,7 +214,9 @@ describe('Filter', function() {
techs: ['buzz']
}),
item = {
block: 'foo',
entity: {
block: 'foo'
},
elem: 'bar',
tech: 'BAZ'
};
Expand All @@ -173,7 +231,9 @@ describe('Filter', function() {
techs: ['buzz']
}),
item = {
block: 'foo',
entity: {
block: 'foo'
},
elem: 'bar',
tech: 'buzz'
};
Expand All @@ -188,7 +248,9 @@ describe('Filter', function() {
techs: ['buzz']
}),
item = {
block: 'foo',
entity: {
block: 'foo'
},
modName: 'fizz',
tech: 'BAZ'
};
Expand All @@ -203,7 +265,9 @@ describe('Filter', function() {
techs: ['buzz']
}),
item = {
block: 'foo',
entity: {
block: 'foo'
},
modName: 'fizz',
tech: 'buzz'
};
Expand Down
26 changes: 0 additions & 26 deletions test/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,31 +161,5 @@ describe('util', function() {
it('should return result with apply function', function() {
assert.instanceOf(util.getFiltersForConditions().apply, Function);
});

it('should prepare BEM entity before apply filter', function() {
var Filter = require('../../lib/filter');
sinon.stub(Filter.prototype, 'apply', function() {
return true;
});
var apply = util.getFiltersForConditions({
blocks: ['foo'],
levels: [],
modifiers: [],
elements: [],
techs: []
}).apply;
apply({
entity: {
block: 'foo'
},
tech: 'js'
});
assert.equal(Filter.prototype.apply.calledOnce, true);
assert.equal(Filter.prototype.apply.calledWith({
block: 'foo',
tech: 'js'
}), true);
Filter.prototype.apply.restore();
});
});
});

0 comments on commit bf333b9

Please sign in to comment.