-
Notifications
You must be signed in to change notification settings - Fork 149
Replace connect with useSelector() and useDispatch() 4/5 #2317 #2346
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
base: master
Are you sure you want to change the base?
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #2346 +/- ##
==========================================
+ Coverage 94.40% 94.44% +0.03%
==========================================
Files 1167 1168 +1
Lines 24957 24987 +30
Branches 5303 5422 +119
==========================================
+ Hits 23561 23598 +37
+ Misses 1329 1316 -13
- Partials 67 73 +6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for those updates! The changes to the main app code mostly look good, but the test code still needs a bunch of revisions to match the other changes we've been making. Please see the things I've flagged below but then check them in all the modified tests, because I didn't point out every occurrence of each issue.
import editorRender from '../../../../../modifiedEditorTestRender'; | ||
import ExplanationWidget from './index'; | ||
import { initializeStore } from '../../../../../data/redux'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import editorRender from '../../../../../modifiedEditorTestRender'; | |
import ExplanationWidget from './index'; | |
import { initializeStore } from '../../../../../data/redux'; | |
import { editorRender } from '@src/editors/editorTestRender'; | |
import ExplanationWidget from './index'; |
What is this modifiedEditorTestRender
? It doesn't seem to be necessary. The existing editorRender
works fine.
initializeMocks({ | ||
initializeStore, | ||
initialState, | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
initializeMocks({ | |
initializeStore, | |
initialState, | |
}); | |
initializeMocks(); |
Since you are passing initialState
into editorRender
, you don't have to do anything with the editor store here when initializing the global app mocks. There are two different redux stores. initializeMocks
initializes the global app store, which the editors basically ignore. editorRender
will initialize the editor redux store, which is what's relevant here.
import editorRender from '../../../../../modifiedEditorTestRender'; | ||
import { initializeStore } from '../../../../../data/redux'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import editorRender from '../../../../../modifiedEditorTestRender'; | |
import { initializeStore } from '../../../../../data/redux'; | |
import { editorRender } from '@src/editors/editorTestRender'; |
beforeEach(() => { | ||
initializeMocks({ initialState, initializeStore }); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
beforeEach(() => { | |
initializeMocks({ initialState, initializeStore }); | |
}); |
initializeMocks
is already called below. We shouldn't call it twice. And you don't need to pass in any redux state - the global redux store that it sets up is not used for editor tests.
export const SettingsWidgetInternal = SettingsWidget; // For testing only | ||
export default connect(mapStateToProps, mapDispatchToProps)(SettingsWidget); | ||
export const SettingsWidgetInternal = SettingsWidget; // For testing | ||
export default injectIntl(SettingsWidget); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't add injectIntl
- use useIntl
instead. But I don't think you need either here - it seems to be unused. Maybe this is a merge conflict error?
import { selectors, initializeStore } from '../../../../../../data/redux'; | ||
import editorRender from '../../../../../../modifiedEditorTestRender'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import { selectors, initializeStore } from '../../../../../../data/redux'; | |
import editorRender from '../../../../../../modifiedEditorTestRender'; | |
import { selectors } from '@src/editors/data/redux'; | |
import { editorRender } from '@src/editors/editorTestRender'; |
Please remove modifiedEditorTestRender
and you don't need to import initializeStore
jest.spyOn(app, 'learningContextId').mockReturnValue('learningContextId'); | ||
jest.spyOn(app, 'isLibrary').mockReturnValue(false); | ||
initializeMocks(); | ||
initializeMocks({ initialState, initializeStore }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
initializeMocks({ initialState, initializeStore }); | |
initializeMocks(); |
}) => { | ||
const intl = useIntl(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import { screen } from '@testing-library/react'; | ||
import { initializeMocks } from '@src/testUtils'; | ||
import { RequestKeys } from '@src/editors/data/constants/requests'; | ||
import editorRender from '../../editorTestRender'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import editorRender from '../../editorTestRender'; | |
import { editorRender } from '@src/editors/editorTestRender'; |
import React from 'react'; | ||
import { Provider } from 'react-redux'; | ||
import { render as baseRender } from '../testUtils'; | ||
import { EditorContextProvider } from './EditorContext'; | ||
import { initializeStore } from './data/redux'; // adjust path if needed | ||
|
||
/** | ||
* Custom render function for testing React components with the editor context and Redux store. | ||
* | ||
* Wraps the provided UI in both the EditorContextProvider and Redux Provider, | ||
* ensuring that components under test have access to the necessary context and store. | ||
* | ||
* @param {React.ReactElement} ui - The React element to render. | ||
* @param {object} [options] - Optional parameters. | ||
* @param {object} [options.initialState] - Optional initial state for the store. | ||
* @param {string} [options.learningContextId] - Optional learning context ID. | ||
* @returns {RenderResult} The result of the render, as returned by RTL render. | ||
*/ | ||
const editorRender = ( | ||
ui, | ||
{ | ||
initialState = {}, | ||
learningContextId = 'course-v1:Org+COURSE+RUN', | ||
} = {}, | ||
) => { | ||
const store = initializeStore(initialState); | ||
|
||
return baseRender(ui, { | ||
extraWrapper: ({ children }) => ( | ||
<EditorContextProvider learningContextId={learningContextId}> | ||
<Provider store={store}> | ||
{children} | ||
</Provider> | ||
</EditorContextProvider> | ||
), | ||
}); | ||
}; | ||
|
||
export default editorRender; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please delete this whole file. I'm not sure what it's purpose was but we don't need it, because editorTestRender
works fine.
Description
Replace connect with useSelector() and useDispatch() 4/5 #2317
Supporting information
Link to other information about the change, such as GitHub issues, or Discourse discussions.
Be sure to check they are publicly readable, or if not, repeat the information here.
Testing instructions
Please provide detailed step-by-step instructions for manually testing this change.
Other information
Include anything else that will help reviewers and consumers understand the change.
Best Practices Checklist
We're trying to move away from some deprecated patterns in this codebase. Please
check if your PR meets these recommendations before asking for a review:
.ts
,.tsx
).propTypes
,defaultProps
, andinjectIntl
patterns are not used in any new or modified code.src/testUtils.tsx
(specificallyinitializeMocks
)apiHooks.ts
in this repo for examples.messages.ts
files have adescription
for translators to use.../
. To import from parent folders, use@src
, e.g.import { initializeMocks } from '@src/testUtils';
instead offrom '../../../../testUtils'