Skip to content

Commit eca6667

Browse files
committed
test: fix useIframeBehaviour hook tests
1 parent 2d9c07c commit eca6667

2 files changed

Lines changed: 214 additions & 169 deletions

File tree

src/course-unit/xblock-container-iframe/hooks/tests/hooks.test.tsx

Lines changed: 1 addition & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import React from 'react';
22
import { act, renderHook } from '@testing-library/react';
33
import { QueryClientProvider, QueryClient } from '@tanstack/react-query';
4-
import { useKeyedState } from '@edx/react-unit-test-utils';
54
import { initializeMockApp } from '@edx/frontend-platform';
6-
import { logError } from '@edx/frontend-platform/logging';
75
import { IntlProvider } from '@edx/frontend-platform/i18n';
86
import { Provider } from 'react-redux';
97

108
import { messageTypes } from '../../../constants';
119
import { mockBroadcastChannel } from '../../../../generic/data/api.mock';
1210
import initializeStore from '../../../../store';
13-
import { useLoadBearingHook, useIFrameBehavior, useMessageHandlers } from '..';
14-
import { iframeMessageTypes, iframeStateKeys } from '../../../../constants';
11+
import { useMessageHandlers } from '..';
1512

1613
jest.useFakeTimers();
1714

@@ -25,171 +22,6 @@ jest.mock('@edx/frontend-platform/logging', () => ({
2522

2623
mockBroadcastChannel();
2724

28-
describe('useIFrameBehavior', () => {
29-
const id = 'test-id';
30-
const iframeUrl = 'http://example.com';
31-
const setIframeHeight = jest.fn();
32-
const setHasLoaded = jest.fn();
33-
const setShowError = jest.fn();
34-
const setWindowTopOffset = jest.fn();
35-
36-
beforeEach(() => {
37-
(useKeyedState as jest.Mock).mockImplementation((key, initialValue) => {
38-
switch (key) {
39-
case iframeStateKeys.iframeHeight:
40-
return [0, setIframeHeight];
41-
case iframeStateKeys.hasLoaded:
42-
return [false, setHasLoaded];
43-
case iframeStateKeys.showError:
44-
return [false, setShowError];
45-
case iframeStateKeys.windowTopOffset:
46-
return [null, setWindowTopOffset];
47-
default:
48-
return [initialValue, jest.fn()];
49-
}
50-
});
51-
52-
window.scrollTo = jest.fn((x: number | ScrollToOptions, y?: number): void => {
53-
const scrollY = typeof x === 'number' ? y : (x as ScrollToOptions).top || 0;
54-
Object.defineProperty(window, 'scrollY', { value: scrollY, writable: true });
55-
}) as typeof window.scrollTo;
56-
});
57-
58-
it('initializes state correctly', () => {
59-
const { result } = renderHook(() => useIFrameBehavior({ id, iframeUrl }));
60-
61-
expect(result.current.iframeHeight).toBe(0);
62-
expect(result.current.showError).toBe(false);
63-
expect(result.current.hasLoaded).toBe(false);
64-
});
65-
66-
it('scrolls to previous position on video fullscreen exit', () => {
67-
const mockWindowTopOffset = 100;
68-
69-
(useKeyedState as jest.Mock).mockImplementation((key) => {
70-
if (key === iframeStateKeys.windowTopOffset) {
71-
return [mockWindowTopOffset, setWindowTopOffset];
72-
}
73-
return [null, jest.fn()];
74-
});
75-
76-
renderHook(() => useIFrameBehavior({ id, iframeUrl }));
77-
78-
const message = {
79-
data: {
80-
type: iframeMessageTypes.videoFullScreen,
81-
payload: { open: false },
82-
},
83-
};
84-
85-
act(() => {
86-
window.dispatchEvent(new MessageEvent('message', message));
87-
});
88-
89-
expect(window.scrollTo).toHaveBeenCalledWith(0, mockWindowTopOffset);
90-
});
91-
92-
it('handles resize message correctly', () => {
93-
renderHook(() => useIFrameBehavior({ id, iframeUrl }));
94-
95-
const message = {
96-
data: {
97-
type: messageTypes.resize,
98-
payload: { height: 500 },
99-
},
100-
};
101-
102-
act(() => {
103-
window.dispatchEvent(new MessageEvent('message', message));
104-
});
105-
106-
expect(setIframeHeight).toHaveBeenCalledWith(500);
107-
expect(setHasLoaded).toHaveBeenCalledWith(true);
108-
});
109-
110-
it('handles videoFullScreen message correctly', () => {
111-
renderHook(() => useIFrameBehavior({ id, iframeUrl }));
112-
113-
const message = {
114-
data: {
115-
type: messageTypes.videoFullScreen,
116-
payload: { open: true },
117-
},
118-
};
119-
120-
act(() => {
121-
window.dispatchEvent(new MessageEvent('message', message));
122-
});
123-
124-
expect(setWindowTopOffset).toHaveBeenCalledWith(window.scrollY);
125-
});
126-
127-
it('handles offset message correctly', () => {
128-
document.body.innerHTML = '<div id="unit-iframe" style="position: absolute; top: 50px;"></div>';
129-
renderHook(() => useIFrameBehavior({ id, iframeUrl }));
130-
131-
const message = {
132-
data: { offset: 100 },
133-
};
134-
135-
act(() => {
136-
window.dispatchEvent(new MessageEvent('message', message));
137-
});
138-
139-
expect(window.scrollY).toBe(100 + (document.getElementById('unit-iframe') as HTMLElement).offsetTop);
140-
});
141-
142-
it('handles iframe load error correctly', () => {
143-
const { result } = renderHook(() => useIFrameBehavior({ id, iframeUrl }));
144-
145-
act(() => {
146-
result.current.handleIFrameLoad();
147-
});
148-
149-
expect(setShowError).toHaveBeenCalledWith(true);
150-
expect(logError).toHaveBeenCalledWith('Unit iframe failed to load. Server possibly returned 4xx or 5xx response.', {
151-
iframeUrl,
152-
});
153-
});
154-
155-
it('resets state when iframeUrl changes', () => {
156-
// eslint-disable-next-line @typescript-eslint/no-shadow
157-
const { rerender } = renderHook(({ id, iframeUrl }) => useIFrameBehavior({ id, iframeUrl }), {
158-
initialProps: { id, iframeUrl },
159-
});
160-
161-
rerender({ id, iframeUrl: 'http://new-url.com' });
162-
163-
expect(setIframeHeight).toHaveBeenCalledWith(0);
164-
expect(setHasLoaded).toHaveBeenCalledWith(false);
165-
});
166-
});
167-
168-
describe('useLoadBearingHook', () => {
169-
const setValue = jest.fn();
170-
171-
beforeEach(() => {
172-
jest.spyOn(React, 'useState').mockReturnValue([0, setValue]);
173-
});
174-
175-
afterEach(() => {
176-
jest.restoreAllMocks();
177-
});
178-
179-
it('updates state when id changes', () => {
180-
const { rerender } = renderHook(({ id }) => useLoadBearingHook(id), {
181-
initialProps: { id: 'initial-id' },
182-
});
183-
184-
setValue.mockClear();
185-
186-
rerender({ id: 'new-id' });
187-
188-
expect(setValue).toHaveBeenCalledWith(expect.any(Function));
189-
expect(setValue.mock.calls);
190-
});
191-
});
192-
19325
describe('useMessageHandlers', () => {
19426
let handlers;
19527
let result;

0 commit comments

Comments
 (0)