Skip to content

Replace connect with useSelector() and useDispatch() 2/5 #2314 #2333

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
@@ -1,6 +1,5 @@
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { useSelector } from 'react-redux';
import { useIntl, FormattedMessage } from '@edx/frontend-platform/i18n';
import { getConfig } from '@edx/frontend-platform';

Expand All @@ -9,27 +8,30 @@ import messages from './messages';
import TinyMceWidget from '../../../../../sharedComponents/TinyMceWidget';
import { prepareEditorRef, replaceStaticWithAsset } from '../../../../../sharedComponents/TinyMceWidget/hooks';

const ExplanationWidget = ({
// redux
settings,
learningContextId,
images,
isLibrary,
blockId,
}) => {
const ExplanationWidget = () => {
const intl = useIntl();
const { editorRef, refReady, setEditorRef } = prepareEditorRef();

const settings = useSelector(selectors.problem.settings);
const learningContextId = useSelector(selectors.app.learningContextId);
const images = useSelector(selectors.app.images);
const isLibrary = useSelector(selectors.app.isLibrary);
const blockId = useSelector(selectors.app.blockId);

const initialContent = settings?.solutionExplanation || '';
const newContent = replaceStaticWithAsset({
initialContent,
learningContextId,
});
const solutionContent = newContent || initialContent;

let staticRootUrl;
if (isLibrary) {
staticRootUrl = `${getConfig().STUDIO_BASE_URL }/library_assets/blocks/${ blockId }/`;
staticRootUrl = `${getConfig().STUDIO_BASE_URL}/library_assets/blocks/${blockId}/`;
}

if (!refReady) { return null; }

return (
<div className="tinyMceWidget mt-4 text-primary-500">
<div className="h4 mb-3">
Expand Down Expand Up @@ -57,22 +59,4 @@ const ExplanationWidget = ({
);
};

ExplanationWidget.propTypes = {
// redux
// eslint-disable-next-line
settings: PropTypes.any.isRequired,
learningContextId: PropTypes.string.isRequired,
images: PropTypes.shape({}).isRequired,
isLibrary: PropTypes.bool.isRequired,
blockId: PropTypes.string.isRequired,
};
export const mapStateToProps = (state) => ({
settings: selectors.problem.settings(state),
learningContextId: selectors.app.learningContextId(state),
images: selectors.app.images(state),
isLibrary: selectors.app.isLibrary(state),
blockId: selectors.app.blockId(state),
});

export const ExplanationWidgetInternal = ExplanationWidget; // For testing only
export default connect(mapStateToProps)(ExplanationWidget);
export default ExplanationWidget;
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
import React from 'react';
import { render, screen, initializeMocks } from '@src/testUtils';
import ExplanationWidget from '.';

jest.mock('../../../../../data/redux', () => ({
__esModule: true,
default: jest.fn(),
selectors: {
problem: {
settings: jest.fn(state => ({ question: state })),
},
app: {
learningContextId: jest.fn(state => ({ learningContextId: state })),
images: jest.fn(state => ({ images: state })),
isLibrary: jest.fn(state => ({ isLibrary: state })),
blockId: jest.fn(state => ({ blockId: state })),
},
},
thunkActions: {
video: {
importTranscript: jest.fn(),
},
},
}));
import { screen, initializeMocks } from '@src/testUtils';
import { editorRender } from '../../../../../editorTestRender';
import ExplanationWidget from './index';

jest.mock('../../../../../sharedComponents/TinyMceWidget/hooks', () => ({
...jest.requireActual('../../../../../sharedComponents/TinyMceWidget/hooks'),
Expand All @@ -36,19 +16,24 @@ jest.mock('../../../../../sharedComponents/TinyMceWidget', () => ({
default: () => <div>TinyMceWidget</div>,
}));

describe('SolutionWidget', () => {
const props = {
const initialState = {
problem: {
settings: { solutionExplanation: 'This is my solution' },
},
app: {
learningContextId: 'course+org+run',
images: {},
isLibrary: false,
blockId: 'block-v1:Org+TS100+24+type@html+block@12345',
};
},
};

describe('SolutionWidget', () => {
beforeEach(() => {
initializeMocks();
});
test('renders correct default', () => {
render(<ExplanationWidget {...props} />);
editorRender(<ExplanationWidget />, { initialState });
expect(screen.getByText('Explanation')).toBeInTheDocument();
expect(screen.getByText('Provide an explanation for the correct answer')).toBeInTheDocument();
expect(screen.getByText('TinyMceWidget')).toBeInTheDocument();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { FormattedMessage, useIntl } from '@edx/frontend-platform/i18n';
import { useSelector } from 'react-redux';
import { useIntl, FormattedMessage } from '@edx/frontend-platform/i18n';
import { getConfig } from '@edx/frontend-platform';

import { selectors } from '../../../../../data/redux';
import messages from './messages';
import TinyMceWidget from '../../../../../sharedComponents/TinyMceWidget';
import { prepareEditorRef, replaceStaticWithAsset } from '../../../../../sharedComponents/TinyMceWidget/hooks';
import {
prepareEditorRef,
replaceStaticWithAsset,
} from '../../../../../sharedComponents/TinyMceWidget/hooks';

const QuestionWidget = ({
// redux
question,
learningContextId,
images,
isLibrary,
blockId,
}) => {
const QuestionWidget = () => {
const intl = useIntl();
const question = useSelector(selectors.problem.question);
const learningContextId = useSelector(selectors.app.learningContextId);
const images = useSelector(selectors.app.images);
const isLibrary = useSelector(selectors.app.isLibrary);
const blockId = useSelector(selectors.app.blockId);

const { editorRef, refReady, setEditorRef } = prepareEditorRef();

const initialContent = question;
const newContent = replaceStaticWithAsset({
initialContent,
learningContextId,
});
const questionContent = newContent || initialContent;

let staticRootUrl;
if (isLibrary) {
staticRootUrl = `${getConfig().STUDIO_BASE_URL }/library_assets/blocks/${ blockId }/`;
staticRootUrl = `${getConfig().STUDIO_BASE_URL}/library_assets/blocks/${blockId}/`;
}

if (!refReady) { return null; }

return (
<div className="tinyMceWidget">
<div className="h4 mb-3">
Expand All @@ -54,21 +59,4 @@ const QuestionWidget = ({
);
};

QuestionWidget.propTypes = {
// redux
question: PropTypes.string.isRequired,
learningContextId: PropTypes.string.isRequired,
images: PropTypes.shape({}).isRequired,
isLibrary: PropTypes.bool.isRequired,
blockId: PropTypes.string.isRequired,
};
export const mapStateToProps = (state) => ({
question: selectors.problem.question(state),
learningContextId: selectors.app.learningContextId(state),
images: selectors.app.images(state),
isLibrary: selectors.app.isLibrary(state),
blockId: selectors.app.blockId(state),
});

export const QuestionWidgetInternal = QuestionWidget; // For testing only
export default connect(mapStateToProps)(QuestionWidget);
export default QuestionWidget;
Original file line number Diff line number Diff line change
@@ -1,33 +1,7 @@
import React from 'react';
import { render, screen, initializeMocks } from '@src/testUtils';
import { formatMessage } from '@src/editors/testUtils';
import { QuestionWidgetInternal as QuestionWidget } from '.';

jest.mock('@src/editors/data/redux', () => ({
__esModule: true,
default: jest.fn(),
actions: {
problem: {
updateQuestion: jest.fn().mockName('actions.problem.updateQuestion'),
},
},
selectors: {
app: {
learningContextId: jest.fn(state => ({ learningContextId: state })),
images: jest.fn(state => ({ images: state })),
isLibrary: jest.fn(state => ({ isLibrary: state })),
blockId: jest.fn(state => ({ blockId: state })),
},
problem: {
question: jest.fn(state => ({ question: state })),
},
},
thunkActions: {
video: {
importTranscript: jest.fn(),
},
},
}));
import { screen, initializeMocks } from '@src/testUtils';
import { editorRender } from '../../../../../editorTestRender';
import QuestionWidget from '.';

jest.mock('@src/editors/sharedComponents/TinyMceWidget/hooks', () => ({
...jest.requireActual('../../../../../sharedComponents/TinyMceWidget/hooks'),
Expand All @@ -39,23 +13,25 @@ jest.mock('@src/editors/sharedComponents/TinyMceWidget/hooks', () => ({

jest.mock('@src/editors/sharedComponents/TinyMceWidget', () => ('TinyMceWidget'));

describe('QuestionWidget', () => {
const props = {
const initialState = {
problem: {
question: 'This is my question',
updateQuestion: jest.fn(),
},
app: {
learningContextId: 'course+org+run',
images: {},
isLibrary: false,
blockId: '',
// injected
intl: { formatMessage },
};
},
};

describe('QuestionWidget', () => {
describe('render', () => {
beforeEach(() => {
initializeMocks();
});
test('renders correct default', () => {
render(<QuestionWidget {...props} />);
editorRender(<QuestionWidget />, { initialState });
expect(screen.getByText('Question')).toBeInTheDocument();
});
});
Expand Down
Loading