Skip to content

Commit

Permalink
refactor: some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
PKulkoRaccoonGang committed Nov 25, 2024
1 parent 2e4cef3 commit 1d9ac29
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
47 changes: 46 additions & 1 deletion src/course-unit/xblock-container-iframe/tests/hooks.test.tsx
Original file line number Diff line number Diff line change
@@ -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(),
Expand Down Expand Up @@ -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 }));

Expand Down Expand Up @@ -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);
});
});
3 changes: 0 additions & 3 deletions src/editors/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down

0 comments on commit 1d9ac29

Please sign in to comment.