diff --git a/lib/filter.js b/lib/filter.js index 4d3eecb..b1ee22e 100644 --- a/lib/filter.js +++ b/lib/filter.js @@ -40,7 +40,7 @@ Filter.prototype = { * @private */ _byBlock: function(item) { - return this._shouldPass(item, 'blocks', 'block'); + return this._shouldPass(item.entity, 'blocks', 'block'); }, /** diff --git a/lib/util.js b/lib/util.js index 501f04b..ffa1df9 100644 --- a/lib/util.js +++ b/lib/util.js @@ -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'); @@ -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); }); }; diff --git a/test/lib/filter.js b/test/lib/filter.js index 140e599..6ec0c45 100644 --- a/test/lib/filter.js +++ b/test/lib/filter.js @@ -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' }; @@ -28,7 +66,9 @@ describe('Filter', function() { elements: ['bar'] }), item = { - block: 'foo', + entity: { + block: 'foo' + }, elem: 'bar' }; @@ -41,7 +81,9 @@ describe('Filter', function() { modifiers: ['bar'] }), item = { - block: 'foo', + entity: { + block: 'foo' + }, modName: 'baz' }; @@ -54,7 +96,9 @@ describe('Filter', function() { modifiers: ['bar'] }), item = { - block: 'foo', + entity: { + block: 'foo' + }, modName: 'bar' }; @@ -67,7 +111,9 @@ describe('Filter', function() { techs: ['bar'] }), item = { - block: 'foo', + entity: { + block: 'foo' + }, tech: 'baz' }; @@ -80,7 +126,9 @@ describe('Filter', function() { techs: ['bar'] }), item = { - block: 'foo', + entity: { + block: 'foo' + }, tech: 'bar' }; @@ -94,7 +142,9 @@ describe('Filter', function() { modifiers: ['fizz'] }), item = { - block: 'foo', + entity: { + block: 'foo' + }, elem: 'bar', modName: 'buzz' }; @@ -109,7 +159,9 @@ describe('Filter', function() { modifiers: ['fizz'] }), item = { - block: 'foo', + entity: { + block: 'foo' + }, elem: 'bar', modName: 'fizz' }; @@ -125,7 +177,9 @@ describe('Filter', function() { techs: ['buzz'] }), item = { - block: 'foo', + entity: { + block: 'foo' + }, elem: 'bar', modName: 'fizz', tech: 'BAZ' @@ -142,7 +196,9 @@ describe('Filter', function() { techs: ['buzz'] }), item = { - block: 'foo', + entity: { + block: 'foo' + }, elem: 'bar', modName: 'fizz', tech: 'buzz' @@ -158,7 +214,9 @@ describe('Filter', function() { techs: ['buzz'] }), item = { - block: 'foo', + entity: { + block: 'foo' + }, elem: 'bar', tech: 'BAZ' }; @@ -173,7 +231,9 @@ describe('Filter', function() { techs: ['buzz'] }), item = { - block: 'foo', + entity: { + block: 'foo' + }, elem: 'bar', tech: 'buzz' }; @@ -188,7 +248,9 @@ describe('Filter', function() { techs: ['buzz'] }), item = { - block: 'foo', + entity: { + block: 'foo' + }, modName: 'fizz', tech: 'BAZ' }; @@ -203,7 +265,9 @@ describe('Filter', function() { techs: ['buzz'] }), item = { - block: 'foo', + entity: { + block: 'foo' + }, modName: 'fizz', tech: 'buzz' }; diff --git a/test/lib/util.js b/test/lib/util.js index c3ecf72..e3f48fd 100644 --- a/test/lib/util.js +++ b/test/lib/util.js @@ -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(); - }); }); });