From bfcd0c7496279137c28ebd00a304fa2a514df47b Mon Sep 17 00:00:00 2001 From: LB Date: Sun, 9 Mar 2025 10:07:17 +1000 Subject: [PATCH] Clean up stubs & adapter - Ensure Jest only adapter items are in adapter.js - Ensure Jest AND Storybook items are in stubs.js - Add mock for someElement.scrollIntoView in the adapter --- CHANGELOG.txt | 1 + client/tests/adapter.js | 40 ++++++++++++++++++++++++++++++++++++++++ client/tests/stubs.js | 31 ------------------------------- docs/releases/6.5.md | 1 + 4 files changed, 42 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 54beac7f7a..65a838edb4 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -26,6 +26,7 @@ Changelog * Maintenance: Validate against invalid characters in Lexeme values (Matt Westcott) * Maintenance: Split up `wagtail.models` module into submodules (Matt Westcott) * Maintenance: Update `ruff` to 0.9.6 (Sage Abdullah) + * Maintenance: Fix up `stubs` & `adapter` contents to better support Jest testing (LB (Ben) Johnston) 6.4.1 (21.02.2025) diff --git a/client/tests/adapter.js b/client/tests/adapter.js index 8e2dfe7f69..1724dbba26 100644 --- a/client/tests/adapter.js +++ b/client/tests/adapter.js @@ -1,6 +1,46 @@ +/** + * Jest adapter and overrides to support Jest testing with Enzyme + * and JSDom. + * This will only be loaded in Jest tests, not in Storybook. + */ + import Enzyme from 'enzyme'; import Adapter from 'enzyme-adapter-react-16'; Enzyme.configure({ adapter: new Adapter(), }); + +/** Mock window.scrollTo as not provided via JSDom */ +window.scrollTo = jest.fn(); + +/** Mock scrollIntoView on elements, this is not provided by JSDom */ +Element.prototype.scrollIntoView = jest.fn(); + +/** Mock console.warn to filter out warnings from React due to Draftail legacy Component API usage. + * Draftail/Draft-js is unlikely to support these and the warnings are not useful for unit test output. + */ +/* eslint-disable no-console */ +const consoleWarnOriginal = console.warn; +console.warn = function filterWarnings(...args) { + /* eslint-enable no-console */ + + const [warning, component] = args; + + const legacyReactWarnings = [ + 'Warning: componentWillMount has been renamed, and is not recommended for use.', + 'Warning: componentWillReceiveProps has been renamed, and is not recommended for use.', + 'Warning: componentWillUpdate has been renamed, and is not recommended for use.', + ]; + + const ignoredComponents = ['DraftEditor', 'PluginEditor']; + + if ( + legacyReactWarnings.some((_) => warning.includes(_)) && + ignoredComponents.includes(component) + ) { + return; + } + + consoleWarnOriginal.apply(console, args); +}; diff --git a/client/tests/stubs.js b/client/tests/stubs.js index e0d2b31564..e84835eee7 100644 --- a/client/tests/stubs.js +++ b/client/tests/stubs.js @@ -56,34 +56,3 @@ global.DOCUMENT_CHOOSER_MODAL_ONLOAD_HANDLERS = { type: 'document' }; class PageChooserModal {} global.PageChooserModal = PageChooserModal; - -/** Mock window.scrollTo as not provided via JSDom */ -window.scrollTo = () => {}; - -/** Mock console.warn to filter out warnings from React due to Draftail legacy Component API usage. - * Draftail/Draft-js is unlikely to support these and the warnings are not useful for unit test output. - */ -/* eslint-disable no-console */ -const consoleWarnOriginal = console.warn; -console.warn = function filterWarnings(...args) { - /* eslint-enable no-console */ - - const [warning, component] = args; - - const legacyReactWarnings = [ - 'Warning: componentWillMount has been renamed, and is not recommended for use.', - 'Warning: componentWillReceiveProps has been renamed, and is not recommended for use.', - 'Warning: componentWillUpdate has been renamed, and is not recommended for use.', - ]; - - const ignoredComponents = ['DraftEditor', 'PluginEditor']; - - if ( - legacyReactWarnings.some((_) => warning.includes(_)) && - ignoredComponents.includes(component) - ) { - return; - } - - consoleWarnOriginal.apply(console, args); -}; diff --git a/docs/releases/6.5.md b/docs/releases/6.5.md index b7567edde4..e5178ee06a 100644 --- a/docs/releases/6.5.md +++ b/docs/releases/6.5.md @@ -44,6 +44,7 @@ depth: 1 * Validate against invalid characters in Lexeme values (Matt Westcott) * Split up `wagtail.models` module into submodules (Matt Westcott) * Update `ruff` to 0.9.6 (Sage Abdullah) + * Fix up `stubs` & `adapter` contents to better support Jest testing (LB (Ben) Johnston) ## Upgrade considerations - changes affecting all projects