官术网_书友最值得收藏!

Nesting describe blocks

We can nest describe blocks to break similar tests up into logical contexts. We can invent our own convention for how to name these describe blocks. Whereas the top level is named after the form itself, the second-level describe blocks are named after the form fields.

Here's how we'd like them to end up:

describe('CustomerForm', () => {
describe('first name field', () => {
// ... tests ...
};
describe('last name field', () => {
// ... tests ...
};
describe('phone number field', () => {
// ... tests ...
};
});

With this structure in place, you can simplify the it descriptive text by removing the name of the field. For example, 'renders the first name field as a text box' becomes 'renders as a text box', because it has already been scoped by the 'first name field' describe block. Because of the way Jest displays describe block names before test names in test output, each of these still reads like a plain-English sentence, but without the verbiage. In the example just given, Jest will show us: CustomerForm first name field renders as a text box.

Let's do that now for the first name field. Wrap the six existing tests in a describe block, and then rename the tests, as shown:

describe('first name field', () => {
it('renders as a text box' ... );

it('includes the existing value' ... );

it('renders a label' ... );

it('assigns an id that matches the label id' ... );

it('saves existing value when submitted' ... );

it('saves new value when submitted' ... );
});
主站蜘蛛池模板: 通榆县| 瑞昌市| 普陀区| 图木舒克市| 饶平县| 铜川市| 怀柔区| 绵竹市| 青州市| 龙海市| 新巴尔虎右旗| 北辰区| 子长县| 鄱阳县| 娱乐| 昌都县| 招远市| 商丘市| 西城区| 化州市| 冀州市| 万安县| 虹口区| 含山县| 富宁县| 土默特右旗| 大方县| 乌拉特后旗| 鹰潭市| 颍上县| 开平市| 贡嘎县| 旬邑县| 乌拉特前旗| 望江县| 恩平市| 和田市| 耿马| 安平县| 宽城| 富裕县|