test: add unit tests for ai-content-planner helpers#23224
Open
test: add unit tests for ai-content-planner helpers#23224
Conversation
Covers the previously untested build-blocks-from-outline, fetch, fields, and normalize-error helpers with cases for happy paths, edge cases (empty input, missing DOM elements), error handling (timeout, user abort, HTTP errors), and fallback behaviour.
Coverage Report for CI Build 7Warning Build has drifted: This PR's base is out of sync with its target branch, so coverage data may include unrelated changes. Coverage increased (+0.8%) to 53.689%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats💛 - Coveralls |
Contributor
There was a problem hiding this comment.
Pull request overview
Adds first-pass unit coverage for the AI content planner helper layer so the JS helpers can be validated independently of the editor/store flows.
Changes:
- Adds direct unit tests for
normalize-errorand hidden-field helpers. - Adds fetch-helper tests covering success, abort, timeout, and structured HTTP error paths.
- Adds block-builder tests covering block creation, ordering, and per-section output shape.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
packages/js/tests/ai-content-planner/helpers/normalize-error.test.js |
Adds normalization tests for null/undefined, partial payloads, and message precedence. |
packages/js/tests/ai-content-planner/helpers/fields.test.js |
Adds DOM-based tests for reading/writing the banner hidden inputs. |
packages/js/tests/ai-content-planner/helpers/fetch.test.js |
Adds apiFetch tests for success, request options, abort/timeout handling, and HTTP-error normalization. |
packages/js/tests/ai-content-planner/helpers/build-blocks-from-outline.test.js |
Adds mocked block-construction tests for outline-to-block conversion. |
| it( "returns all defaults for an empty object payload", () => { | ||
| expect( normalizeError( {} ) ).toEqual( DEFAULT_ERROR ); | ||
| } ); | ||
|
|
Comment on lines
+13
to
+27
| it( "returns an empty array for an empty outline", () => { | ||
| const result = buildBlocksFromOutline( [] ); | ||
| expect( result ).toEqual( [] ); | ||
| expect( createBlock ).not.toHaveBeenCalled(); | ||
| } ); | ||
|
|
||
| it( "creates three blocks per section: heading, content-suggestion, paragraph", () => { | ||
| const outline = [ { heading: "Introduction", contentNotes: [ "Note 1", "Note 2" ] } ]; | ||
|
|
||
| const result = buildBlocksFromOutline( outline ); | ||
|
|
||
| expect( result ).toHaveLength( 3 ); | ||
| expect( createBlock ).toHaveBeenCalledWith( "core/heading", { content: "Introduction", level: 2 } ); | ||
| expect( createBlock ).toHaveBeenCalledWith( "yoast-seo/content-suggestion", { suggestions: [ "Note 1", "Note 2" ] } ); | ||
| expect( createBlock ).toHaveBeenCalledWith( "core/paragraph" ); |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
Summary
This PR can be summarized in the following changelog entry:
Relevant technical choices:
fetch.test.js— timeout vs. user-abort distinction:fetch.jsuses a singleAbortControllerinternally and sets anisTimeoutflag inside the timer callback before callingcontroller.abort(). To exercise both branches correctly, theapiFetchmock listens tooptions.signaland rejects with aDOMException("AbortError")when the signal fires, rather than rejecting immediately. This keeps theisTimeoutflag in the correct state at the moment the error is caught.fetch.test.js— malformed JSON on success path: Whenresponse.json()throws, the error falls through to thecatchblock. Because it is not anAbortError, it is passed tobuildHttpError, which produces a structured{ errorCode: 502, … }fallback rather than re-throwing the rawSyntaxError.fields.test.js— real DOM, no mocks: The fields helpers read and writedocument.getElementByIddirectly. Tests setdocument.body.innerHTMLbefore each case and reset it inafterEach, which is simpler and more faithful than mockingdocument.build-blocks-from-outline.test.js—@wordpress/blocksmock:createBlockis mocked to return{ type, attrs }so tests can assert on the exact block type and attributes without needing a full block registration environment.normalize-error.test.js— no mocks needed:normalizeErroris a pure function with no side effects, so tests cover input shapes directly (plainError, partial object,null/undefined) and verify theerrorMessagevsmessagepriority and per-field defaults.Test instructions
Test instructions for the acceptance test before the PR gets merged
This PR can be acceptance tested by following these steps:
Relevant test scenarios
Test instructions for QA when the code is in the RC
QA can test this PR by following these steps:
Impact check
This PR affects the following parts of the plugin, which may require extra testing:
Other environments
[shopify-seo], added test instructions for Shopify and attached theShopifylabel to this PR.[yoast-doc-extension], added test instructions for Yoast SEO for Google Docs and attached theGoogle Docs Add-onlabel to this PR.Documentation
Quality assurance
grunt build:imagesand committed the results, if my PR introduces or edits images or SVGs.Innovation
innovationlabel.Fixes https://github.com/Yoast/reserved-tasks/issues/1192