Skip to content

Commit

Permalink
feat: Intercept cancel button event to close Advanced Editor
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisChV committed Dec 13, 2024
1 parent fcc1445 commit c0f84ab
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 8 deletions.
50 changes: 50 additions & 0 deletions src/editors/AdvancedEditor.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { getConfig } from '@edx/frontend-platform';

import {
render,
initializeMocks,
waitFor,
} from '../testUtils';
import AdvancedEditor from './AdvancedEditor';

jest.mock('../library-authoring/LibraryBlock', () => ({
LibraryBlock: jest.fn(() => (<div>Advanced Editor Iframe</div>)),
}));

describe('AdvancedEditor', () => {
beforeEach(() => {
initializeMocks();
});

it('should call onClose when receiving "cancel-clicked" message', () => {
const onCloseMock = jest.fn();

render(<AdvancedEditor usageKey="test" onClose={onCloseMock} />);

const messageEvent = new MessageEvent('message', {
data: 'cancel-clicked',
origin: getConfig().STUDIO_BASE_URL,
});

window.dispatchEvent(messageEvent);

waitFor(() => {
expect(onCloseMock).toHaveBeenCalled();
});
});

it('should not call onClose if the message is from an invalid origin', () => {
const onCloseMock = jest.fn();

render(<AdvancedEditor usageKey="test" onClose={onCloseMock} />);

const messageEvent = new MessageEvent('message', {
data: 'cancel-clicked',
origin: 'https://invalid-origin.com',
});

window.dispatchEvent(messageEvent);

expect(onCloseMock).not.toHaveBeenCalled();
});
});
39 changes: 31 additions & 8 deletions src/editors/AdvancedEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { useEffect } from 'react';
import { getConfig } from '@edx/frontend-platform';

import { LibraryBlock } from '../library-authoring/LibraryBlock';
import { EditorModalWrapper } from './containers/EditorContainer';

Expand All @@ -6,13 +9,33 @@ interface AdvancedEditorProps {
onClose: Function | null,
}

const AdvancedEditor = ({ usageKey, onClose }: AdvancedEditorProps) => (
<EditorModalWrapper onClose={onClose as () => void}>
<LibraryBlock
usageKey={usageKey}
view="studio_view"
/>
</EditorModalWrapper>
);
const AdvancedEditor = ({ usageKey, onClose }: AdvancedEditorProps) => {
useEffect(() => {
const handleIframeMessage = (event) => {
if (event.origin !== getConfig().STUDIO_BASE_URL) {
return;

Check warning on line 16 in src/editors/AdvancedEditor.tsx

View check run for this annotation

Codecov / codecov/patch

src/editors/AdvancedEditor.tsx#L16

Added line #L16 was not covered by tests
}

if (event.data === 'cancel-clicked' && onClose) {
onClose();

Check warning on line 20 in src/editors/AdvancedEditor.tsx

View check run for this annotation

Codecov / codecov/patch

src/editors/AdvancedEditor.tsx#L20

Added line #L20 was not covered by tests
}
};

window.addEventListener('message', handleIframeMessage);

return () => {
window.removeEventListener('message', handleIframeMessage);
};
}, []);

return (
<EditorModalWrapper onClose={onClose as () => void}>
<LibraryBlock
usageKey={usageKey}
view="studio_view"
/>
</EditorModalWrapper>
);
};

export default AdvancedEditor;

0 comments on commit c0f84ab

Please sign in to comment.