Skip to content
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
2 changes: 2 additions & 0 deletions docs/tests.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@

Для моков используется библиотека [sinon](http://sinonjs.org/).

Для помощи в написании тестов существует модуль [spec__utils](utils.ru.md)

### Варианты оформления тестов

Условно можно выделить два случая:
Expand Down
37 changes: 37 additions & 0 deletions docs/utils.ru.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Утилиты для написания тестов
============================

Проект `bem-pr` содержит набор утилит для упрощенного написания тестов клиентского кода BEM-блоков, эти утилиты доступны в модуле `spec__utils`.
Пример теста, использующего утилиты:

```js
modules.define('spec', ['spec__utils'], function(provide, utils) {

describe('test-block', function() {

var bemjson = {
block : 'test-block',
mods : { someMod : 'someVal' }
};

it('should init successfully', function() {
var block = utils.buildBlock('test-block', bemjson);

block.getMod('someMod').should.equal('someVal');

utils.destruct(block); // Настоятельно рекомендуем очищать блок после теста
});

});
});
```

## Доступные функции

### buildBlock(name, bemjson)

Инициализирует и возвращает BEM-блок `name` на основе переданного `bemjson`.

### destruct(block)

Удаляет блок и DOM-дерева. Рекомендуется вызывать эту функцию после каждого теста для всех созданных блоков.
7 changes: 7 additions & 0 deletions spec.blocks/spec/__utils/spec__utils.deps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[{
mustDeps: [
{ block : 'i-bem', elem : 'dom' },
{ block : 'BEMHTML' },
{ block : 'jquery' }
]
}]
31 changes: 31 additions & 0 deletions spec.blocks/spec/__utils/spec__utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @module spec__utils
*/
modules.define('spec__utils', ['i-bem__dom', 'BEMHTML', 'jquery'], function(provide, BEMDOM, BEMHTML, $) {
Copy link
Owner

Choose a reason for hiding this comment

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

Давай это унесем в какой-нибудь i-bem__dom-specs или хотя бы spec__i-bem-dom. Для элемента абстрактного spec, который про i-bem вообще ничего знать не должен, тут слишком много i-bem'а :)


provide({
/**
* Builds a block by name and bemjson
*
* @function buildBlock
* @param {string} name
* @param {object} bemjson
* @return {object} bem block
*/
buildBlock : function(name, bemjson) {
Copy link
Owner

Choose a reason for hiding this comment

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

я бы добавил еще опциональный 3-й аргумент {jQuery|String} [parent=body]. У нас есть кейсы, когда добавлять блок в body нельзя

bemjson = bemjson || { block : name };
return BEMDOM.init($(BEMHTML.apply(bemjson)).appendTo('body')).bem(name);

Choose a reason for hiding this comment

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

Почему не bemjson.block?

},

/**
* Clean up block
*
* @function destruct
* @param {object} block bem block
*/
destruct : function(block) {
Copy link
Owner

Choose a reason for hiding this comment

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

Наверное, utils.destructBlock?

Copy link
Author

Choose a reason for hiding this comment

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

Изначально было сделано по аналогии с BEMDOM.destruct(), но если уж менять название, но я бы сделал destroyBlock

Copy link
Owner

Choose a reason for hiding this comment

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

но если уж менять название, но я бы сделал destroyBlock

Ok

BEMDOM.destruct(block.domElem);
}
});

});
3 changes: 2 additions & 1 deletion spec.blocks/spec/spec.deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
'sinon',
'sinon-chai',
'simulate-dom-event',
'jquery'
'jquery',
{ elem : 'utils' }
]
}]