From e684618d9f11d5a79b195e2d964e439830258dee Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Tue, 4 Apr 2023 15:38:56 +1200 Subject: [PATCH] Add section about integration testing to testing docs (#49454) * Add section about integration testing to testing docs * Changes from code review --- docs/contributors/code/testing-overview.md | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/docs/contributors/code/testing-overview.md b/docs/contributors/code/testing-overview.md index b646756669563d..42704be233b9b9 100644 --- a/docs/contributors/code/testing-overview.md +++ b/docs/contributors/code/testing-overview.md @@ -290,7 +290,30 @@ test('fires onChange when a new value is typed', async () => { }) ``` +### Integration testing for block UI +Integration testing is defined as a type of testing where different parts are tested as a group. In this case, the parts that we want to test are the different components that are required to be rendered for a specific block or editor logic. In the end, they are very similar to unit tests as they are run with the same command using the Jest library. The main difference is that for the integration tests the blocks are run within a [`special instance of the block editor`](https://github.com/WordPress/gutenberg/blob/trunk/test/integration/helpers/integration-test-editor.js#L60). + +The advantage of this approach is that the bulk of a block editor's functionality (block toolbar and inspector panel interactions, etc.) can be tested without having to fire up the full e2e test framework. This means the tests can run much faster and more reliably. It is suggested that as much of a block's UI functionality as possible is covered with integration tests, with e2e tests used for interactions that require a full browser environment, eg. file uploads, drag and drop, etc. + +[`The Cover block`](https://github.com/WordPress/gutenberg/blob/trunk/packages/block-library/src/cover/test/edit.js) is an example of a block that uses this level of testing to provide coverage for a large percentage of the editor interactions. + +To set up a jest file for integration tests: + +```js +import { + initializeEditor +} from 'test/integration/helpers/integration-test-editor'; + +async function setup( attributes ) { + const testBlock = { name: 'core/cover', attributes }; + return initializeEditor( testBlock ); +} +``` + +The `initializeEditor` function returns the output of the `@testing-library/react` `render` method. It will also accept an array of block metadata objects, allowing you to set up the editor with multiple blocks. + +The integration test editor module also exports a `selectBlock` which can be used to select the block to be tested by the aria-label on the block wrapper, eg. "Block: Cover". ### Snapshot testing