Skip to content

Commit c0f84ab

Browse files
committed
feat: Intercept cancel button event to close Advanced Editor
1 parent fcc1445 commit c0f84ab

File tree

2 files changed

+81
-8
lines changed

2 files changed

+81
-8
lines changed

src/editors/AdvancedEditor.test.tsx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { getConfig } from '@edx/frontend-platform';
2+
3+
import {
4+
render,
5+
initializeMocks,
6+
waitFor,
7+
} from '../testUtils';
8+
import AdvancedEditor from './AdvancedEditor';
9+
10+
jest.mock('../library-authoring/LibraryBlock', () => ({
11+
LibraryBlock: jest.fn(() => (<div>Advanced Editor Iframe</div>)),
12+
}));
13+
14+
describe('AdvancedEditor', () => {
15+
beforeEach(() => {
16+
initializeMocks();
17+
});
18+
19+
it('should call onClose when receiving "cancel-clicked" message', () => {
20+
const onCloseMock = jest.fn();
21+
22+
render(<AdvancedEditor usageKey="test" onClose={onCloseMock} />);
23+
24+
const messageEvent = new MessageEvent('message', {
25+
data: 'cancel-clicked',
26+
origin: getConfig().STUDIO_BASE_URL,
27+
});
28+
29+
window.dispatchEvent(messageEvent);
30+
31+
waitFor(() => {
32+
expect(onCloseMock).toHaveBeenCalled();
33+
});
34+
});
35+
36+
it('should not call onClose if the message is from an invalid origin', () => {
37+
const onCloseMock = jest.fn();
38+
39+
render(<AdvancedEditor usageKey="test" onClose={onCloseMock} />);
40+
41+
const messageEvent = new MessageEvent('message', {
42+
data: 'cancel-clicked',
43+
origin: 'https://invalid-origin.com',
44+
});
45+
46+
window.dispatchEvent(messageEvent);
47+
48+
expect(onCloseMock).not.toHaveBeenCalled();
49+
});
50+
});

src/editors/AdvancedEditor.tsx

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { useEffect } from 'react';
2+
import { getConfig } from '@edx/frontend-platform';
3+
14
import { LibraryBlock } from '../library-authoring/LibraryBlock';
25
import { EditorModalWrapper } from './containers/EditorContainer';
36

@@ -6,13 +9,33 @@ interface AdvancedEditorProps {
69
onClose: Function | null,
710
}
811

9-
const AdvancedEditor = ({ usageKey, onClose }: AdvancedEditorProps) => (
10-
<EditorModalWrapper onClose={onClose as () => void}>
11-
<LibraryBlock
12-
usageKey={usageKey}
13-
view="studio_view"
14-
/>
15-
</EditorModalWrapper>
16-
);
12+
const AdvancedEditor = ({ usageKey, onClose }: AdvancedEditorProps) => {
13+
useEffect(() => {
14+
const handleIframeMessage = (event) => {
15+
if (event.origin !== getConfig().STUDIO_BASE_URL) {
16+
return;
17+
}
18+
19+
if (event.data === 'cancel-clicked' && onClose) {
20+
onClose();
21+
}
22+
};
23+
24+
window.addEventListener('message', handleIframeMessage);
25+
26+
return () => {
27+
window.removeEventListener('message', handleIframeMessage);
28+
};
29+
}, []);
30+
31+
return (
32+
<EditorModalWrapper onClose={onClose as () => void}>
33+
<LibraryBlock
34+
usageKey={usageKey}
35+
view="studio_view"
36+
/>
37+
</EditorModalWrapper>
38+
);
39+
};
1740

1841
export default AdvancedEditor;

0 commit comments

Comments
 (0)