Skip to content

test: add unit tests for ai-content-planner helpers#23224

Open
vraja-pro wants to merge 4 commits intotrunkfrom
1192-content-planner-js-unit-tests-for-helpers
Open

test: add unit tests for ai-content-planner helpers#23224
vraja-pro wants to merge 4 commits intotrunkfrom
1192-content-planner-js-unit-tests-for-helpers

Conversation

@vraja-pro
Copy link
Copy Markdown
Contributor

@vraja-pro vraja-pro commented May 4, 2026

Context

  • We want to add tests to the helper functions of the content planner feature: 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.

Summary

This PR can be summarized in the following changelog entry:

  • Adds unit tests for the content planner JS helper functions.

Relevant technical choices:

  • fetch.test.js — timeout vs. user-abort distinction: fetch.js uses a single AbortController internally and sets an isTimeout flag inside the timer callback before calling controller.abort(). To exercise both branches correctly, the apiFetch mock listens to options.signal and rejects with a DOMException("AbortError") when the signal fires, rather than rejecting immediately. This keeps the isTimeout flag in the correct state at the moment the error is caught.
  • fetch.test.js — malformed JSON on success path: When response.json() throws, the error falls through to the catch block. Because it is not an AbortError, it is passed to buildHttpError, which produces a structured { errorCode: 502, … } fallback rather than re-throwing the raw SyntaxError.
  • fields.test.js — real DOM, no mocks: The fields helpers read and write document.getElementById directly. Tests set document.body.innerHTML before each case and reset it in afterEach, which is simpler and more faithful than mocking document.
  • build-blocks-from-outline.test.js@wordpress/blocks mock: createBlock is 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: normalizeError is a pure function with no side effects, so tests cover input shapes directly (plain Error, partial object, null/undefined) and verify the errorMessage vs message priority 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:

  • Excute the following command and check helpers get 100% test coverage.
cd packages/js && yarn jest tests/ai-content-planner --coverage --collectCoverageFrom="src/ai-content-planner/helpers/**/*.js"

Relevant test scenarios

  • Changes should be tested with the browser console open
  • Changes should be tested on different posts/pages/taxonomies/custom post types/custom taxonomies
  • Changes should be tested on different editors (Default Block/Gutenberg/Classic/Elementor/other)
  • Changes should be tested on different browsers
  • Changes should be tested on multisite

Test instructions for QA when the code is in the RC

  • QA should use the same steps as above.

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

  • This PR also affects Shopify. I have added a changelog entry starting with [shopify-seo], added test instructions for Shopify and attached the Shopify label to this PR.
  • This PR also affects Yoast SEO for Google Docs. I have added a changelog entry starting with [yoast-doc-extension], added test instructions for Yoast SEO for Google Docs and attached the Google Docs Add-on label to this PR.

Documentation

  • I have written documentation for this change. For example, comments in the Relevant technical choices, comments in the code, documentation on Confluence / shared Google Drive / Yoast developer portal, or other.

Quality assurance

  • I have tested this code to the best of my abilities.
  • During testing, I had activated all plugins that Yoast SEO provides integrations for.
  • I have added unit tests to verify the code works as intended.
  • If any part of the code is behind a feature flag, my test instructions also cover cases where the feature flag is switched off.
  • I have written this PR in accordance with my team's definition of done.
  • I have checked that the base branch is correctly set.
  • I have run grunt build:images and committed the results, if my PR introduces or edits images or SVGs.

Innovation

  • No innovation project is applicable for this PR.
  • This PR falls under an innovation project. I have attached the innovation label.
  • I have added my hours to the WBSO document.

Fixes https://github.com/Yoast/reserved-tasks/issues/1192

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.
@vraja-pro vraja-pro added the changelog: non-user-facing Needs to be included in the 'Non-userfacing' category in the changelog label May 4, 2026
@coveralls
Copy link
Copy Markdown

coveralls commented May 4, 2026

Coverage Report for CI Build 7

Warning

Build has drifted: This PR's base is out of sync with its target branch, so coverage data may include unrelated changes.
Quick fix: rebase this PR. Learn more →

Coverage increased (+0.8%) to 53.689%

Details

  • Coverage increased (+0.8%) from the base build.
  • Patch coverage: No coverable lines changed in this PR.
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 66130
Covered Lines: 35349
Line Coverage: 53.45%
Relevant Branches: 16972
Covered Branches: 9268
Branch Coverage: 54.61%
Branches in Coverage %: Yes
Coverage Strength: 45487.87 hits per line

💛 - Coveralls

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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-error and 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" );
Comment thread packages/js/tests/ai-content-planner/helpers/fetch.test.js Outdated
@FAMarfuaty FAMarfuaty added this to the 27.7 milestone May 5, 2026
vraja-pro and others added 3 commits May 5, 2026 10:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog: non-user-facing Needs to be included in the 'Non-userfacing' category in the changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants