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

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' ... );
});
主站蜘蛛池模板: 大名县| 绥江县| 唐山市| 黎川县| 广水市| 肇源县| 永安市| 滁州市| 玉山县| 漳州市| 安吉县| 武乡县| 龙陵县| 宣化县| 邓州市| 濮阳市| 玉环县| 宕昌县| 寿光市| 察雅县| 闽侯县| 疏勒县| 慈利县| 察隅县| 汪清县| 龙泉市| 巴楚县| 玉门市| 太湖县| 龙海市| 汝城县| 萨迦县| 长丰县| 太仓市| 广饶县| 凤阳县| 溧水县| 宁陕县| 康乐县| 佛山市| 商水县|