Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: text editor opening blank with no images #492

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ export const ExplanationWidget = ({
intl,
}) => {
const { editorRef, refReady, setEditorRef } = prepareEditorRef();
const solutionContent = replaceStaticWithAsset({
initialContent: settings?.solutionExplanation || '',
const initialContent = settings?.solutionExplanation || '';
const newContent = replaceStaticWithAsset({
initialContent,
learningContextId,
});
const solutionContent = newContent || initialContent;
if (!refReady) { return null; }
return (
<div className="tinyMceWidget mt-4 text-primary-500">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable react/prop-types */
import React from 'react';
import { shallow } from '@edx/react-unit-test-utils';
import { formatMessage } from '../../../../../../testUtils';
Expand All @@ -24,11 +23,11 @@ jest.mock('../../../../../data/redux', () => ({
}));

jest.mock('../../../../../sharedComponents/TinyMceWidget/hooks', () => ({
...jest.requireActual('../../../../../sharedComponents/TinyMceWidget/hooks'),
prepareEditorRef: jest.fn(() => ({
refReady: true,
setEditorRef: jest.fn().mockName('prepareEditorRef.setEditorRef'),
})),
replaceStaticWithAsset: jest.fn(() => 'This is my solution'),
}));

describe('SolutionWidget', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ export const QuestionWidget = ({
intl,
}) => {
const { editorRef, refReady, setEditorRef } = prepareEditorRef();
const questionContent = replaceStaticWithAsset({
initialContent: question,
const initialContent = question;
const newContent = replaceStaticWithAsset({
initialContent,
learningContextId,
});
const questionContent = newContent || initialContent;
if (!refReady) { return null; }
return (
<div className="tinyMceWidget">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable react/prop-types */
import React from 'react';
import { shallow } from '@edx/react-unit-test-utils';
import { formatMessage } from '../../../../../../testUtils';
Expand Down Expand Up @@ -29,11 +28,11 @@ jest.mock('../../../../../data/redux', () => ({
}));

jest.mock('../../../../../sharedComponents/TinyMceWidget/hooks', () => ({
...jest.requireActual('../../../../../sharedComponents/TinyMceWidget/hooks'),
prepareEditorRef: jest.fn(() => ({
refReady: true,
setEditorRef: jest.fn().mockName('prepareEditorRef.setEditorRef'),
})),
replaceStaticWithAsset: jest.fn(() => 'This is my question'),
}));

describe('QuestionWidget', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,52 @@ exports[`TextEditor snapshots renders as expected with default behavior 1`] = `
</div>
</EditorContainer>
`;

exports[`TextEditor snapshots renders static images with relative paths 1`] = `
<EditorContainer
getContent={
{
"getContent": {
"editorRef": {
"current": {
"value": "something",
},
},
"showRawEditor": false,
},
}
}
onClose={[MockFunction props.onClose]}
returnFunction={null}
>
<div
className="editor-body h-75 overflow-auto"
>
<Toast
onClose={[MockFunction hooks.nullMethod]}
show={false}
>
<FormattedMessage
defaultMessage="Error: Could Not Load Text Content"
description="Error Message Dispayed When HTML content fails to Load"
id="authoring.texteditor.load.error"
/>
</Toast>
<[object Object]
editorContentHtml="eDiTablE Text with <img src="/asset+org+run+type@[email protected]" />"
editorRef={
{
"current": {
"value": "something",
},
}
}
editorType="text"
height="100%"
initializeEditor={[MockFunction args.intializeEditor]}
minHeight={500}
setEditorRef={[MockFunction hooks.prepareEditorRef.setEditorRef]}
/>
</div>
</EditorContainer>
`;
8 changes: 5 additions & 3 deletions src/editors/containers/TextEditor/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ export const TextEditor = ({
intl,
}) => {
const { editorRef, refReady, setEditorRef } = prepareEditorRef();
const editorContent = blockValue ? replaceStaticWithAsset({
initialContent: blockValue.data.data,
const initialContent = blockValue ? blockValue.data.data : '';
const newContent = replaceStaticWithAsset({
initialContent,
learningContextId,
}) : '';
});
const editorContent = newContent || initialContent;

if (!refReady) { return null; }

Expand Down
9 changes: 8 additions & 1 deletion src/editors/containers/TextEditor/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ jest.mock('./hooks', () => ({
}));

jest.mock('../../sharedComponents/TinyMceWidget/hooks', () => ({
...jest.requireActual('../../sharedComponents/TinyMceWidget/hooks'),
prepareEditorRef: jest.fn(() => ({
editorRef: { current: { value: 'something' } },
refReady: true,
setEditorRef: jest.fn().mockName('hooks.prepareEditorRef.setEditorRef'),
})),
replaceStaticWithAsset: jest.fn(() => 'eDiTablE Text'),
}));

jest.mock('react', () => {
Expand Down Expand Up @@ -88,6 +88,13 @@ describe('TextEditor', () => {
test('renders as expected with default behavior', () => {
expect(shallow(<TextEditor {...props} />).snapshot).toMatchSnapshot();
});
test('renders static images with relative paths', () => {
const updatedProps = {
...props,
blockValue: { data: { data: 'eDiTablE Text with <img src="/static/img.jpg" />' } },
};
expect(shallow(<TextEditor {...updatedProps} />).snapshot).toMatchSnapshot();
});
test('not yet loaded, Spinner appears', () => {
expect(shallow(<TextEditor {...props} blockFinished={false} />).snapshot).toMatchSnapshot();
});
Expand Down
4 changes: 3 additions & 1 deletion src/editors/sharedComponents/ImageUploadModal/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export const hooks = {
updateReactState({ settings, ...args });

close();
args.setSelection(null);
},
onClose: ({ clearSelection, close }) => () => {
clearSelection();
Expand Down Expand Up @@ -130,7 +131,7 @@ export const ImageUploadModal = ({
<ImageSettingsModal
{...{
isOpen,
close: module.hooks.onClose({ editorRef, clearSelection, close }),
close: module.hooks.onClose({ clearSelection, close }),
selection,
images,
saveToEditor: module.hooks.createSaveCallback({
Expand All @@ -141,6 +142,7 @@ export const ImageUploadModal = ({
selection,
setSelection,
lmsEndpointUrl,
clearSelection,
}),
returnToSelection: clearSelection,
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ describe('ImageUploadModal', () => {
expect(updateImageDimensionsSpy.mock.results[0].value.foundMatch).toBe(false);
expect(images.current).toEqual([mockImage, newImage]);
expect(close).toBeCalled();
expect(setSelection).toBeCalledWith(null);
},
);
});
Expand Down

This file was deleted.

7 changes: 5 additions & 2 deletions src/editors/sharedComponents/RawEditor/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import { Alert } from '@openedx/paragon';

import CodeEditor from '../CodeEditor';
import { setAssetToStaticUrl } from '../TinyMceWidget/hooks';

function getValue(content) {
if (!content) { return null; }
Expand All @@ -15,7 +16,8 @@ export const RawEditor = ({
content,
lang,
}) => {
const value = getValue(content);
const value = getValue(content) || '';
const staticUpdate = setAssetToStaticUrl({ editorValue: value });

return (
<div>
Expand All @@ -27,8 +29,9 @@ export const RawEditor = ({
{ value ? (
<CodeEditor
innerRef={editorRef}
value={value}
value={staticUpdate}
lang={lang}
data-testid="code-editor"
/>
) : null}

Expand Down
53 changes: 44 additions & 9 deletions src/editors/sharedComponents/RawEditor/index.test.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import React from 'react';
import { shallow } from '@edx/react-unit-test-utils';
import { render, screen } from '@testing-library/react';
import { IntlProvider } from '@edx/frontend-platform/i18n';
import '@testing-library/jest-dom/extend-expect';

import { RawEditor } from '.';

jest.unmock('@openedx/paragon');

const renderComponent = (props) => render(
<IntlProvider locale="en">
<RawEditor {...props} />
</IntlProvider>,
);
describe('RawEditor', () => {
const defaultProps = {
editorRef: {
current: {
value: 'Ref Value',
},
},
content: { data: { data: 'eDiTablE Text' } },
content: { data: { data: 'eDiTablE Text HtmL' } },
lang: 'html',
};
const xmlProps = {
Expand All @@ -19,7 +28,7 @@ describe('RawEditor', () => {
value: 'Ref Value',
},
},
content: { data: { data: 'eDiTablE Text' } },
content: { data: { data: 'eDiTablE Text XMl' } },
lang: 'xml',
};
const noContentProps = {
Expand All @@ -33,13 +42,39 @@ describe('RawEditor', () => {
width: { width: '80%' },
};

test('renders as expected with default behavior', () => {
expect(shallow(<RawEditor {...defaultProps} />).snapshot).toMatchSnapshot();
it('renders as expected with default behavior', () => {
renderComponent(defaultProps);
expect(screen.getByRole('alert')).toBeVisible();

expect(screen.getByText('eDiTablE Text HtmL')).toBeVisible();
});
test('renders as expected with lang equal to xml', () => {
expect(shallow(<RawEditor {...xmlProps} />).snapshot).toMatchSnapshot();

it('updates the assets to static srcs', () => {
const updatedProps = {
...defaultProps,
content: 'pick <img src="/asset-v1:org+run+term+type@[email protected]" /> or <img src="/assets/courseware/v1/hash/asset-v1:org+run+term+type@asset+block/img2.jpeg" />',
};
renderComponent(updatedProps);
expect(screen.getByText('"/static/img.jpeg"')).toBeVisible();

expect(screen.getByText('"/static/img2.jpeg"')).toBeVisible();

expect(screen.queryByText('"/asset-v1:org+run+term+type@[email protected]"')).toBeNull();

expect(screen.queryByText('"/assets/courseware/v1/hash/asset-v1:org+run+term+type@asset+block/img2.jpeg"')).toBeNull();
});
test('renders as expected with content equal to null', () => {
expect(shallow(<RawEditor {...noContentProps} />).snapshot).toMatchSnapshot();

it('renders as expected with lang equal to xml', () => {
renderComponent(xmlProps);
expect(screen.queryByRole('alert')).toBeNull();

expect(screen.getByText('eDiTablE Text XMl')).toBeVisible();
});

it('renders as expected with content equal to null', () => {
renderComponent(noContentProps);
expect(screen.getByRole('alert')).toBeVisible();

expect(screen.queryByTestId('code-editor')).toBeNull();
});
});
Loading
Loading