diff --git a/src/course-unit/xblock-container-iframe/tests/hooks.test.tsx b/src/course-unit/xblock-container-iframe/tests/hooks.test.tsx index 5afd3fea70..13b5467622 100644 --- a/src/course-unit/xblock-container-iframe/tests/hooks.test.tsx +++ b/src/course-unit/xblock-container-iframe/tests/hooks.test.tsx @@ -1,9 +1,10 @@ +import React from 'react'; import { renderHook, act } from '@testing-library/react-hooks'; import { useKeyedState } from '@edx/react-unit-test-utils'; import { logError } from '@edx/frontend-platform/logging'; import { stateKeys, messageTypes } from '../../constants'; -import { useIFrameBehavior } from '../hooks'; +import { useIFrameBehavior, useLoadBearingHook } from '../hooks'; jest.mock('@edx/react-unit-test-utils', () => ({ useKeyedState: jest.fn(), @@ -51,6 +52,32 @@ describe('useIFrameBehavior', () => { expect(result.current.hasLoaded).toBe(false); }); + it('scrolls to previous position on video fullscreen exit', () => { + const mockWindowTopOffset = 100; + + (useKeyedState as jest.Mock).mockImplementation((key) => { + if (key === stateKeys.windowTopOffset) { + return [mockWindowTopOffset, setWindowTopOffset]; + } + return [null, jest.fn()]; + }); + + renderHook(() => useIFrameBehavior({ id, iframeUrl })); + + const message = { + data: { + type: messageTypes.videoFullScreen, + payload: { open: false }, + }, + }; + + act(() => { + window.dispatchEvent(new MessageEvent('message', message)); + }); + + expect(window.scrollTo).toHaveBeenCalledWith(0, mockWindowTopOffset); + }); + it('handles resize message correctly', () => { renderHook(() => useIFrameBehavior({ id, iframeUrl })); @@ -126,3 +153,21 @@ describe('useIFrameBehavior', () => { expect(setHasLoaded).toHaveBeenCalledWith(false); }); }); + +describe('useLoadBearingHook', () => { + it('updates state when id changes', () => { + const setValue = jest.fn(); + jest.spyOn(React, 'useState').mockReturnValue([0, setValue]); + + const { rerender } = renderHook(({ id }) => useLoadBearingHook(id), { + initialProps: { id: 'initial-id' }, + }); + + setValue.mockClear(); + + rerender({ id: 'new-id' }); + + expect(setValue).toHaveBeenCalledWith(expect.any(Function)); + expect(setValue.mock.calls); + }); +}); diff --git a/src/editors/hooks.ts b/src/editors/hooks.ts index a7d0aa856b..d2a7403d87 100644 --- a/src/editors/hooks.ts +++ b/src/editors/hooks.ts @@ -13,9 +13,6 @@ export const initializeApp = ({ dispatch, data }) => useEffect( ); export const navigateTo = (destination: string | URL) => { - // TODO: once the "Remove backend redirects (use SPA functionality)" PR (#1372) is merged, - // the editor will utilize SPA functionality, allowing navigation back - // to the course unit page without a full page reload. window.location.assign(destination); };