From 3c42388c48185119d8f2eaa895c1662a3065b26c Mon Sep 17 00:00:00 2001 From: Tim Fischbach Date: Thu, 21 May 2026 17:23:45 +0200 Subject: [PATCH 1/2] Fake parent window in useInlineEditingPageObjects Inline editing only ever runs inside the editor's preview iframe in production, so `window.parent !== window` is always true and the postMessage listener in inline-editing's MessageHandler is always attached. Have the helper set that up by default so inline-editing specs no longer need to remember `fakeParentWindow()` per test. --- .../spec/frontend/features/contentElementSelection-spec.js | 2 -- .../spec/frontend/features/editorCommentBadges-spec.js | 2 -- .../frontend/features/insertContentElementMessage-spec.js | 2 -- .../package/spec/frontend/features/marginIndicator-spec.js | 5 ----- .../spec/frontend/features/moveContentElementMessage-spec.js | 2 -- .../package/spec/frontend/features/paddingIndicator-spec.js | 5 ----- .../spec/frontend/features/scrollPointMessages-spec.js | 2 -- .../package/spec/frontend/features/selectedMessage-spec.js | 2 -- .../features/useContentElementConfigurationUpdate-spec.js | 2 -- .../frontend/features/useWidgetConfigurationUpdate-spec.js | 2 -- .../EditableText/features/commentHighlights-spec.js | 3 --- .../EditableText/features/commentSelection-spec.js | 3 --- .../package/spec/support/pageObjects/inlineEditing.js | 2 ++ 13 files changed, 2 insertions(+), 32 deletions(-) diff --git a/entry_types/scrolled/package/spec/frontend/features/contentElementSelection-spec.js b/entry_types/scrolled/package/spec/frontend/features/contentElementSelection-spec.js index 335eec1cd7..a7a590b2d0 100644 --- a/entry_types/scrolled/package/spec/frontend/features/contentElementSelection-spec.js +++ b/entry_types/scrolled/package/spec/frontend/features/contentElementSelection-spec.js @@ -3,7 +3,6 @@ import {frontend} from 'frontend'; import {features} from 'pageflow/frontend'; import {useInlineEditingPageObjects, renderEntry} from 'support/pageObjects'; -import {fakeParentWindow} from 'support'; import {changeLocationHash} from 'support/changeLocationHash'; import '@testing-library/jest-dom/extend-expect' import {act, fireEvent, waitFor} from '@testing-library/react'; @@ -12,7 +11,6 @@ describe('content element selection', () => { useInlineEditingPageObjects(); beforeEach(() => { - fakeParentWindow() window.parent.postMessage = jest.fn(); window.location.hash = '#initial'; }); diff --git a/entry_types/scrolled/package/spec/frontend/features/editorCommentBadges-spec.js b/entry_types/scrolled/package/spec/frontend/features/editorCommentBadges-spec.js index 44abfd4fab..80d1bae442 100644 --- a/entry_types/scrolled/package/spec/frontend/features/editorCommentBadges-spec.js +++ b/entry_types/scrolled/package/spec/frontend/features/editorCommentBadges-spec.js @@ -3,7 +3,6 @@ import {act, waitFor} from '@testing-library/react'; import {features} from 'pageflow/frontend'; import {useInlineEditingPageObjects, renderEntry} from 'support/pageObjects'; -import {fakeParentWindow} from 'support'; import badgeStyles from 'review/Badge.module.css'; @@ -11,7 +10,6 @@ describe('editor comment badges', () => { useInlineEditingPageObjects(); beforeEach(() => { - fakeParentWindow(); window.parent.postMessage = jest.fn(); features.enable('frontend', ['commenting']); }); diff --git a/entry_types/scrolled/package/spec/frontend/features/insertContentElementMessage-spec.js b/entry_types/scrolled/package/spec/frontend/features/insertContentElementMessage-spec.js index 803c870a85..648305eefc 100644 --- a/entry_types/scrolled/package/spec/frontend/features/insertContentElementMessage-spec.js +++ b/entry_types/scrolled/package/spec/frontend/features/insertContentElementMessage-spec.js @@ -1,12 +1,10 @@ import {useInlineEditingPageObjects, renderEntry} from 'support/pageObjects'; -import {fakeParentWindow} from 'support'; import '@testing-library/jest-dom/extend-expect' describe('INSERT_CONTENT_ELEMENT message', () => { useInlineEditingPageObjects(); beforeEach(() => { - fakeParentWindow() window.parent.postMessage = jest.fn(); }); diff --git a/entry_types/scrolled/package/spec/frontend/features/marginIndicator-spec.js b/entry_types/scrolled/package/spec/frontend/features/marginIndicator-spec.js index a945cb35ce..3e70c3b6fb 100644 --- a/entry_types/scrolled/package/spec/frontend/features/marginIndicator-spec.js +++ b/entry_types/scrolled/package/spec/frontend/features/marginIndicator-spec.js @@ -1,14 +1,9 @@ import {useInlineEditingPageObjects, renderEntry} from 'support/pageObjects'; -import {fakeParentWindow} from 'support'; import '@testing-library/jest-dom/extend-expect'; describe('MarginIndicator', () => { useInlineEditingPageObjects(); - beforeEach(() => { - fakeParentWindow(); - }); - it('displays scale translation for top margin when element is selected', () => { const {getContentElementByTestId} = renderEntry({ seed: { diff --git a/entry_types/scrolled/package/spec/frontend/features/moveContentElementMessage-spec.js b/entry_types/scrolled/package/spec/frontend/features/moveContentElementMessage-spec.js index 1f40326fe0..dcbd8ce181 100644 --- a/entry_types/scrolled/package/spec/frontend/features/moveContentElementMessage-spec.js +++ b/entry_types/scrolled/package/spec/frontend/features/moveContentElementMessage-spec.js @@ -1,12 +1,10 @@ import {useInlineEditingPageObjects, renderEntry} from 'support/pageObjects'; -import {fakeParentWindow} from 'support'; import '@testing-library/jest-dom/extend-expect' describe('MOVE_CONTENT_ELEMENT message', () => { useInlineEditingPageObjects(); beforeEach(() => { - fakeParentWindow() window.parent.postMessage = jest.fn(); }); diff --git a/entry_types/scrolled/package/spec/frontend/features/paddingIndicator-spec.js b/entry_types/scrolled/package/spec/frontend/features/paddingIndicator-spec.js index 75ce0cb454..d48ac557bf 100644 --- a/entry_types/scrolled/package/spec/frontend/features/paddingIndicator-spec.js +++ b/entry_types/scrolled/package/spec/frontend/features/paddingIndicator-spec.js @@ -1,5 +1,4 @@ import {useInlineEditingPageObjects, renderEntry} from 'support/pageObjects'; -import {fakeParentWindow} from 'support'; import {useMotifAreaState} from 'frontend/useMotifAreaState'; import '@testing-library/jest-dom/extend-expect'; @@ -8,10 +7,6 @@ jest.mock('frontend/useMotifAreaState'); describe('PaddingIndicator', () => { useInlineEditingPageObjects(); - beforeEach(() => { - fakeParentWindow(); - }); - it('displays scale translation for top padding when section is selected', () => { const {getSectionByPermaId} = renderEntry({ seed: { diff --git a/entry_types/scrolled/package/spec/frontend/features/scrollPointMessages-spec.js b/entry_types/scrolled/package/spec/frontend/features/scrollPointMessages-spec.js index a0a6e08d5a..29627a2870 100644 --- a/entry_types/scrolled/package/spec/frontend/features/scrollPointMessages-spec.js +++ b/entry_types/scrolled/package/spec/frontend/features/scrollPointMessages-spec.js @@ -1,5 +1,4 @@ import {renderEntry, useInlineEditingPageObjects} from 'support/pageObjects'; -import {fakeParentWindow} from 'support'; import {asyncHandlingOf} from 'support/asyncHandlingOf'; @@ -7,7 +6,6 @@ describe('scroll point messages', () => { useInlineEditingPageObjects(); beforeEach(() => { - fakeParentWindow(); window.parent.postMessage = jest.fn(); window.scrollTo = jest.fn(); }); diff --git a/entry_types/scrolled/package/spec/frontend/features/selectedMessage-spec.js b/entry_types/scrolled/package/spec/frontend/features/selectedMessage-spec.js index 4892a5d42b..3b636c79bb 100644 --- a/entry_types/scrolled/package/spec/frontend/features/selectedMessage-spec.js +++ b/entry_types/scrolled/package/spec/frontend/features/selectedMessage-spec.js @@ -3,7 +3,6 @@ import {frontend, WidgetSelectionRect} from 'frontend'; import {features} from 'pageflow/frontend'; import {useInlineEditingPageObjects, renderEntry} from 'support/pageObjects'; -import {fakeParentWindow} from 'support'; import {act, fireEvent, waitFor} from '@testing-library/react'; import '@testing-library/jest-dom/extend-expect' @@ -11,7 +10,6 @@ describe('SELECTED message', () => { useInlineEditingPageObjects(); beforeEach(() => { - fakeParentWindow() window.parent.postMessage = jest.fn(); }); diff --git a/entry_types/scrolled/package/spec/frontend/features/useContentElementConfigurationUpdate-spec.js b/entry_types/scrolled/package/spec/frontend/features/useContentElementConfigurationUpdate-spec.js index d84bc64e92..ed97c5d811 100644 --- a/entry_types/scrolled/package/spec/frontend/features/useContentElementConfigurationUpdate-spec.js +++ b/entry_types/scrolled/package/spec/frontend/features/useContentElementConfigurationUpdate-spec.js @@ -3,14 +3,12 @@ import React, {useEffect} from 'react'; import {frontend, useContentElementConfigurationUpdate} from 'frontend'; import {renderEntry, useInlineEditingPageObjects} from 'support/pageObjects'; -import {fakeParentWindow} from 'support'; import '@testing-library/jest-dom/extend-expect' describe('useContentElementConfigurationUpdate', () => { useInlineEditingPageObjects(); beforeEach(() => { - fakeParentWindow() window.parent.postMessage = jest.fn(); }); diff --git a/entry_types/scrolled/package/spec/frontend/features/useWidgetConfigurationUpdate-spec.js b/entry_types/scrolled/package/spec/frontend/features/useWidgetConfigurationUpdate-spec.js index 84d82ab6c0..83ee83d10a 100644 --- a/entry_types/scrolled/package/spec/frontend/features/useWidgetConfigurationUpdate-spec.js +++ b/entry_types/scrolled/package/spec/frontend/features/useWidgetConfigurationUpdate-spec.js @@ -3,14 +3,12 @@ import React, {useEffect} from 'react'; import {frontend, useWidgetConfigurationUpdate} from 'frontend'; import {renderEntry, useInlineEditingPageObjects} from 'support/pageObjects'; -import {fakeParentWindow} from 'support'; import '@testing-library/jest-dom/extend-expect' describe('useWidgetConfigurationUpdate', () => { useInlineEditingPageObjects(); beforeEach(() => { - fakeParentWindow() window.parent.postMessage = jest.fn(); }); diff --git a/entry_types/scrolled/package/spec/frontend/inlineEditing/EditableText/features/commentHighlights-spec.js b/entry_types/scrolled/package/spec/frontend/inlineEditing/EditableText/features/commentHighlights-spec.js index 8419614e0c..bff7c586e6 100644 --- a/entry_types/scrolled/package/spec/frontend/inlineEditing/EditableText/features/commentHighlights-spec.js +++ b/entry_types/scrolled/package/spec/frontend/inlineEditing/EditableText/features/commentHighlights-spec.js @@ -3,7 +3,6 @@ import React from 'react'; import {features} from 'pageflow/frontend'; import {EditableText} from 'frontend'; import {renderEntry, useInlineEditingPageObjects} from 'support/pageObjects/inlineEditing'; -import {fakeParentWindow} from 'support'; import {act, waitFor} from '@testing-library/react'; import '@testing-library/jest-dom/extend-expect'; @@ -50,8 +49,6 @@ describe('inline editing EditableText comment highlights', () => { }); it('highlights pending new thread range from editor state', async () => { - fakeParentWindow(); - const entry = renderEntry({ contentElement: { ui: , diff --git a/entry_types/scrolled/package/spec/frontend/inlineEditing/EditableText/features/commentSelection-spec.js b/entry_types/scrolled/package/spec/frontend/inlineEditing/EditableText/features/commentSelection-spec.js index 5e479f6d48..5bbb1c29fa 100644 --- a/entry_types/scrolled/package/spec/frontend/inlineEditing/EditableText/features/commentSelection-spec.js +++ b/entry_types/scrolled/package/spec/frontend/inlineEditing/EditableText/features/commentSelection-spec.js @@ -3,7 +3,6 @@ import React from 'react'; import {features} from 'pageflow/frontend'; import {EditableText} from 'frontend'; import {renderEntry, useInlineEditingPageObjects} from 'support/pageObjects/inlineEditing'; -import {fakeParentWindow} from 'support'; import {act} from '@testing-library/react'; import '@testing-library/jest-dom/extend-expect'; @@ -22,7 +21,6 @@ describe('inline editing EditableText comment selection messages', () => { }); it('posts SELECTED contentElementComments with highlightedThreadId on badge click', () => { - fakeParentWindow(); window.parent.postMessage = jest.fn(); const value = [ @@ -57,7 +55,6 @@ describe('inline editing EditableText comment selection messages', () => { }); it('runs badge click logic and scrolls into view on SELECT_COMMENT_THREAD message', async () => { - fakeParentWindow(); window.parent.postMessage = jest.fn(); const scrollIntoView = jest.fn(); Element.prototype.scrollIntoView = scrollIntoView; diff --git a/entry_types/scrolled/package/spec/support/pageObjects/inlineEditing.js b/entry_types/scrolled/package/spec/support/pageObjects/inlineEditing.js index 30f42585c3..43a13e18c8 100644 --- a/entry_types/scrolled/package/spec/support/pageObjects/inlineEditing.js +++ b/entry_types/scrolled/package/spec/support/pageObjects/inlineEditing.js @@ -5,6 +5,7 @@ import {loadInlineEditingComponents} from 'frontend/inlineEditing'; import {clearExtensions} from 'frontend/extensionRegistry'; import badgeStyles from 'review/Badge.module.css'; +import {fakeParentWindow} from '../fakeWindows'; import { renderEntry as baseRenderEntry, usePageObjects @@ -25,6 +26,7 @@ export function renderEntry({commenting, ...options} = {}) { export function useInlineEditingPageObjects() { beforeAll(async () => { + fakeParentWindow(); await loadInlineEditingComponents(); }); From a1eee2f42309ef46286cd30757d68e7bafbd6eca Mon Sep 17 00:00:00 2001 From: Tim Fischbach Date: Fri, 22 May 2026 08:53:07 +0200 Subject: [PATCH 2/2] Replace fakeParentWindow with useFakeParentWindow hook Bundle the fake parent window setup AND its postMessage stub into a single setup hook so specs that need outgoing messages mocked can opt in with one line. `useInlineEditingPageObjects` calls it instead of the bare `fakeParentWindow()`; specs that previously paired `fakeParentWindow()` with `window.parent.postMessage = jest.fn()` drop both calls and use the hook directly. The shallow-equal transient state test in inEditorPreview-spec used to re-stub `window.parent.postMessage` mid-test before re-rendering. With the hook stubbing it once per beforeEach, switch to a `mockClear()` before the rerender and assert that no further posts happen, instead of asserting a total call count. --- .../features/changeSectionMessage-spec.js | 8 ++------ .../features/contentElementSelection-spec.js | 1 - .../features/editorCommentBadges-spec.js | 1 - .../features/insertContentElementMessage-spec.js | 3 --- .../features/moveContentElementMessage-spec.js | 3 --- .../features/scrollPointMessages-spec.js | 1 - .../frontend/features/selectedMessage-spec.js | 3 --- .../useContentElementConfigurationUpdate-spec.js | 3 --- .../useWidgetConfigurationUpdate-spec.js | 3 --- .../features/commentSelection-spec.js | 2 -- .../useSelectLinkDestination-spec.js | 16 +++------------- .../inEditorPreview-spec.js | 9 +++++---- .../scrolled/package/spec/support/fakeWindows.js | 13 +++++++++++++ .../spec/support/pageObjects/inlineEditing.js | 5 +++-- 14 files changed, 26 insertions(+), 45 deletions(-) diff --git a/entry_types/scrolled/package/spec/frontend/features/changeSectionMessage-spec.js b/entry_types/scrolled/package/spec/frontend/features/changeSectionMessage-spec.js index 6fad4768f2..1a156e5ac7 100644 --- a/entry_types/scrolled/package/spec/frontend/features/changeSectionMessage-spec.js +++ b/entry_types/scrolled/package/spec/frontend/features/changeSectionMessage-spec.js @@ -1,7 +1,7 @@ import 'widgets/excursionSheet'; import {renderEntry, usePageObjects} from 'support/pageObjects'; -import {fakeParentWindow} from 'support'; +import {useFakeParentWindow} from 'support/fakeWindows'; import {changeLocationHash} from 'support/changeLocationHash'; import {useFakeTranslations} from 'pageflow/testHelpers'; import 'support/viewTimelineStub'; @@ -11,16 +11,12 @@ import userEvent from '@testing-library/user-event'; describe('CHANGE_SECTION message', () => { usePageObjects(); + useFakeParentWindow(); useFakeTranslations({ 'pageflow_scrolled.public.close': 'Close' }); - beforeEach(() => { - fakeParentWindow() - window.parent.postMessage = jest.fn(); - }); - it('is posted when section becomes active', () => { const {getSectionByPermaId} = renderEntry({ seed: { diff --git a/entry_types/scrolled/package/spec/frontend/features/contentElementSelection-spec.js b/entry_types/scrolled/package/spec/frontend/features/contentElementSelection-spec.js index a7a590b2d0..85b1f21023 100644 --- a/entry_types/scrolled/package/spec/frontend/features/contentElementSelection-spec.js +++ b/entry_types/scrolled/package/spec/frontend/features/contentElementSelection-spec.js @@ -11,7 +11,6 @@ describe('content element selection', () => { useInlineEditingPageObjects(); beforeEach(() => { - window.parent.postMessage = jest.fn(); window.location.hash = '#initial'; }); diff --git a/entry_types/scrolled/package/spec/frontend/features/editorCommentBadges-spec.js b/entry_types/scrolled/package/spec/frontend/features/editorCommentBadges-spec.js index 80d1bae442..20bc4741ba 100644 --- a/entry_types/scrolled/package/spec/frontend/features/editorCommentBadges-spec.js +++ b/entry_types/scrolled/package/spec/frontend/features/editorCommentBadges-spec.js @@ -10,7 +10,6 @@ describe('editor comment badges', () => { useInlineEditingPageObjects(); beforeEach(() => { - window.parent.postMessage = jest.fn(); features.enable('frontend', ['commenting']); }); diff --git a/entry_types/scrolled/package/spec/frontend/features/insertContentElementMessage-spec.js b/entry_types/scrolled/package/spec/frontend/features/insertContentElementMessage-spec.js index 648305eefc..2a39169f91 100644 --- a/entry_types/scrolled/package/spec/frontend/features/insertContentElementMessage-spec.js +++ b/entry_types/scrolled/package/spec/frontend/features/insertContentElementMessage-spec.js @@ -4,9 +4,6 @@ import '@testing-library/jest-dom/extend-expect' describe('INSERT_CONTENT_ELEMENT message', () => { useInlineEditingPageObjects(); - beforeEach(() => { - window.parent.postMessage = jest.fn(); - }); it('is posted when selection rect insert button is clicked', () => { const {getContentElementByTestId} = renderEntry({ diff --git a/entry_types/scrolled/package/spec/frontend/features/moveContentElementMessage-spec.js b/entry_types/scrolled/package/spec/frontend/features/moveContentElementMessage-spec.js index dcbd8ce181..e171d3a928 100644 --- a/entry_types/scrolled/package/spec/frontend/features/moveContentElementMessage-spec.js +++ b/entry_types/scrolled/package/spec/frontend/features/moveContentElementMessage-spec.js @@ -4,9 +4,6 @@ import '@testing-library/jest-dom/extend-expect' describe('MOVE_CONTENT_ELEMENT message', () => { useInlineEditingPageObjects(); - beforeEach(() => { - window.parent.postMessage = jest.fn(); - }); it('is posted when content element is dragged before other content element', () => { const {getContentElementByTestId} = renderEntry({ diff --git a/entry_types/scrolled/package/spec/frontend/features/scrollPointMessages-spec.js b/entry_types/scrolled/package/spec/frontend/features/scrollPointMessages-spec.js index 29627a2870..10c74d6af1 100644 --- a/entry_types/scrolled/package/spec/frontend/features/scrollPointMessages-spec.js +++ b/entry_types/scrolled/package/spec/frontend/features/scrollPointMessages-spec.js @@ -6,7 +6,6 @@ describe('scroll point messages', () => { useInlineEditingPageObjects(); beforeEach(() => { - window.parent.postMessage = jest.fn(); window.scrollTo = jest.fn(); }); diff --git a/entry_types/scrolled/package/spec/frontend/features/selectedMessage-spec.js b/entry_types/scrolled/package/spec/frontend/features/selectedMessage-spec.js index 3b636c79bb..44171d4a46 100644 --- a/entry_types/scrolled/package/spec/frontend/features/selectedMessage-spec.js +++ b/entry_types/scrolled/package/spec/frontend/features/selectedMessage-spec.js @@ -9,9 +9,6 @@ import '@testing-library/jest-dom/extend-expect' describe('SELECTED message', () => { useInlineEditingPageObjects(); - beforeEach(() => { - window.parent.postMessage = jest.fn(); - }); it('is posted when content element is clicked', () => { frontend.contentElementTypes.register('text', { diff --git a/entry_types/scrolled/package/spec/frontend/features/useContentElementConfigurationUpdate-spec.js b/entry_types/scrolled/package/spec/frontend/features/useContentElementConfigurationUpdate-spec.js index ed97c5d811..e4034ad6f4 100644 --- a/entry_types/scrolled/package/spec/frontend/features/useContentElementConfigurationUpdate-spec.js +++ b/entry_types/scrolled/package/spec/frontend/features/useContentElementConfigurationUpdate-spec.js @@ -8,9 +8,6 @@ import '@testing-library/jest-dom/extend-expect' describe('useContentElementConfigurationUpdate', () => { useInlineEditingPageObjects(); - beforeEach(() => { - window.parent.postMessage = jest.fn(); - }); it('lets content elements use hook to update their own configuration', () => { frontend.contentElementTypes.register('text', { diff --git a/entry_types/scrolled/package/spec/frontend/features/useWidgetConfigurationUpdate-spec.js b/entry_types/scrolled/package/spec/frontend/features/useWidgetConfigurationUpdate-spec.js index 83ee83d10a..90462063cc 100644 --- a/entry_types/scrolled/package/spec/frontend/features/useWidgetConfigurationUpdate-spec.js +++ b/entry_types/scrolled/package/spec/frontend/features/useWidgetConfigurationUpdate-spec.js @@ -8,9 +8,6 @@ import '@testing-library/jest-dom/extend-expect' describe('useWidgetConfigurationUpdate', () => { useInlineEditingPageObjects(); - beforeEach(() => { - window.parent.postMessage = jest.fn(); - }); it('lets widgets use hook to update their own configuration', () => { frontend.widgetTypes.register('customNavigation', { diff --git a/entry_types/scrolled/package/spec/frontend/inlineEditing/EditableText/features/commentSelection-spec.js b/entry_types/scrolled/package/spec/frontend/inlineEditing/EditableText/features/commentSelection-spec.js index 5bbb1c29fa..7bc9bf1360 100644 --- a/entry_types/scrolled/package/spec/frontend/inlineEditing/EditableText/features/commentSelection-spec.js +++ b/entry_types/scrolled/package/spec/frontend/inlineEditing/EditableText/features/commentSelection-spec.js @@ -21,7 +21,6 @@ describe('inline editing EditableText comment selection messages', () => { }); it('posts SELECTED contentElementComments with highlightedThreadId on badge click', () => { - window.parent.postMessage = jest.fn(); const value = [ {type: 'paragraph', children: [{text: 'First paragraph'}]} @@ -55,7 +54,6 @@ describe('inline editing EditableText comment selection messages', () => { }); it('runs badge click logic and scrolls into view on SELECT_COMMENT_THREAD message', async () => { - window.parent.postMessage = jest.fn(); const scrollIntoView = jest.fn(); Element.prototype.scrollIntoView = scrollIntoView; diff --git a/entry_types/scrolled/package/spec/frontend/inlineEditing/useSelectLinkDestination-spec.js b/entry_types/scrolled/package/spec/frontend/inlineEditing/useSelectLinkDestination-spec.js index 4cfde36e75..50ff367a3b 100644 --- a/entry_types/scrolled/package/spec/frontend/inlineEditing/useSelectLinkDestination-spec.js +++ b/entry_types/scrolled/package/spec/frontend/inlineEditing/useSelectLinkDestination-spec.js @@ -1,13 +1,12 @@ import {useSelectLinkDestination} from 'frontend/inlineEditing/useSelectLinkDestination'; import {renderHook} from '@testing-library/react-hooks'; -import {fakeParentWindow} from 'support'; +import {useFakeParentWindow} from 'support/fakeWindows'; describe('useSelectLinkDestination', () => { - it('returns function that posts SELECT_LINK_DESTINATION message', () => { - fakeParentWindow(); - window.parent.postMessage = jest.fn(); + useFakeParentWindow(); + it('returns function that posts SELECT_LINK_DESTINATION message', () => { const {result} = renderHook(() => useSelectLinkDestination()); const selectLinkDestination = result.current; selectLinkDestination().catch(() => {}); @@ -19,9 +18,6 @@ describe('useSelectLinkDestination', () => { }); it('returns function returns promise which resolves on LINK_DESTINATION_SELECTED message', () => { - fakeParentWindow(); - window.parent.postMessage = jest.fn(); - const {result} = renderHook(() => useSelectLinkDestination()); const selectLinkDestination = result.current; const promise = selectLinkDestination(); @@ -35,9 +31,6 @@ describe('useSelectLinkDestination', () => { }); it('rejects promise on when function is called again', () => { - fakeParentWindow(); - window.parent.postMessage = jest.fn(); - const {result} = renderHook(() => useSelectLinkDestination()); const selectLinkDestination = result.current; const promise = selectLinkDestination(); @@ -47,9 +40,6 @@ describe('useSelectLinkDestination', () => { }); it('ignores other messages send to window', () => { - fakeParentWindow(); - window.parent.postMessage = jest.fn(); - const {result} = renderHook(() => useSelectLinkDestination()); const selectLinkDestination = result.current; const promise = selectLinkDestination(); diff --git a/entry_types/scrolled/package/spec/frontend/useContentElementEditorState/inEditorPreview-spec.js b/entry_types/scrolled/package/spec/frontend/useContentElementEditorState/inEditorPreview-spec.js index d959146e21..1b55ce5a42 100644 --- a/entry_types/scrolled/package/spec/frontend/useContentElementEditorState/inEditorPreview-spec.js +++ b/entry_types/scrolled/package/spec/frontend/useContentElementEditorState/inEditorPreview-spec.js @@ -1,5 +1,6 @@ import {frontend, Entry, useContentElementEditorState} from 'pageflow-scrolled/frontend'; import {renderInEntry} from 'support'; +import {useFakeParentWindow} from 'support/fakeWindows'; import {useEditorSelection} from 'frontend/inlineEditing/EditorState'; import React, {useEffect} from 'react'; @@ -10,6 +11,7 @@ import {loadInlineEditingComponents} from 'frontend/inlineEditing'; describe('useContentElementEditorState in editor preview', () => { beforeAll(loadInlineEditingComponents); + useFakeParentWindow(); it('lets content elements determine whether they are selected', () => { frontend.contentElementTypes.register('test', { @@ -98,7 +100,6 @@ describe('useContentElementEditorState in editor preview', () => { }); it('lets content elements publish transient state via post message', () => { - window.parent.postMessage = jest.fn(); frontend.contentElementTypes.register('test', { component: function Test() { const {setTransientState} = useContentElementEditorState(); @@ -116,7 +117,7 @@ describe('useContentElementEditorState in editor preview', () => { } }); - expect(window.postMessage).toHaveBeenCalledWith({ + expect(window.parent.postMessage).toHaveBeenCalledWith({ type: 'UPDATE_TRANSIENT_CONTENT_ELEMENT_STATE', payload: { id: 5, @@ -126,7 +127,6 @@ describe('useContentElementEditorState in editor preview', () => { }); it('does not send message if transient state is shallow equal to previous transient state', () => { - window.parent.postMessage = jest.fn(); frontend.contentElementTypes.register('test', { component: function Test() { const {setTransientState} = useContentElementEditorState(); @@ -144,8 +144,9 @@ describe('useContentElementEditorState in editor preview', () => { } }); + window.parent.postMessage.mockClear(); rerender(); - expect(window.postMessage).toHaveBeenCalledTimes(1); + expect(window.parent.postMessage).not.toHaveBeenCalled(); }); }); diff --git a/entry_types/scrolled/package/spec/support/fakeWindows.js b/entry_types/scrolled/package/spec/support/fakeWindows.js index 64b95789f3..981502c861 100644 --- a/entry_types/scrolled/package/spec/support/fakeWindows.js +++ b/entry_types/scrolled/package/spec/support/fakeWindows.js @@ -4,6 +4,19 @@ export function fakeParentWindow() { Object.defineProperty(window, 'parent', {value: new JSDOM('').window }); }; +export function useFakeParentWindow() { + beforeAll(() => { + Object.defineProperty(window, 'parent', { + value: new JSDOM('').window, + configurable: true + }); + }); + + beforeEach(() => { + window.parent.postMessage = jest.fn(); + }); +} + export function createIframeWindow() { const dom = new JSDOM(''); dom.reconfigure({windowTop: window, url: window.location.origin}); diff --git a/entry_types/scrolled/package/spec/support/pageObjects/inlineEditing.js b/entry_types/scrolled/package/spec/support/pageObjects/inlineEditing.js index 43a13e18c8..d1b97e59d6 100644 --- a/entry_types/scrolled/package/spec/support/pageObjects/inlineEditing.js +++ b/entry_types/scrolled/package/spec/support/pageObjects/inlineEditing.js @@ -5,7 +5,7 @@ import {loadInlineEditingComponents} from 'frontend/inlineEditing'; import {clearExtensions} from 'frontend/extensionRegistry'; import badgeStyles from 'review/Badge.module.css'; -import {fakeParentWindow} from '../fakeWindows'; +import {useFakeParentWindow} from '../fakeWindows'; import { renderEntry as baseRenderEntry, usePageObjects @@ -25,8 +25,9 @@ export function renderEntry({commenting, ...options} = {}) { } export function useInlineEditingPageObjects() { + useFakeParentWindow(); + beforeAll(async () => { - fakeParentWindow(); await loadInlineEditingComponents(); });