-
Notifications
You must be signed in to change notification settings - Fork 31
Add better return types for the API functions #218
Conversation
Codecov Report
@@ Coverage Diff @@
## master #218 +/- ##
=========================================
+ Coverage 94.02% 95.3% +1.28%
=========================================
Files 22 22
Lines 318 341 +23
Branches 76 81 +5
=========================================
+ Hits 299 325 +26
+ Misses 18 15 -3
Partials 1 1
Continue to review full report at Codecov.
|
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.
This seems useful for detecting errors but I'd like to not keep investing in all of this code that ignores errors. I suggested an approach that would throw all errors but if you think we should tackle that in a follow-up instead just let me know.
4680113
to
8f929f0
Compare
Yes, it is time to resolve #182 now. |
I like this approach. I think ultimately we should go with that approach but there may be some complexities in how we deal with rendering / dismissing notices. How about taking a baby step towards that approach like this? if (isErrorResponse(response)) {
log.error(`TODO: handle this error response: ${response.error}`);
} else {
dispatch(versionActions.loadVersionInfo({ version: response }));
} |
💯 👍 I'll update this patch with that, and I'll try to think more about the other patch with the |
8f929f0
to
fa26555
Compare
@kumar303 so I think this is good for another look. I had to introduce a |
3f4522d
to
72b1345
Compare
1d71bd8
to
8b2e149
Compare
@kumar303 this should be ready for another look! It's full of thunks 🌟 |
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.
This looks good. I just have some minor cleanup requests.
I miss sinon.spy(store, 'dispatch')
a little. I tried wrapping up the jest methods in a helper function but couldn't figure out how to mirror the function parameters of jest.spyOn
. TypeScript!
My failed attempt 😞
diff --git a/src/pages/Browse/index.spec.tsx b/src/pages/Browse/index.spec.tsx
index 48c2876..42b0871 100644
--- a/src/pages/Browse/index.spec.tsx
+++ b/src/pages/Browse/index.spec.tsx
@@ -6,6 +6,7 @@ import {
createFakeThunk,
fakeVersion,
shallowUntilTarget,
+ spyOn,
} from '../../test-helpers';
import configureStore from '../../configureStore';
import {
@@ -92,9 +93,7 @@ describe(__filename, () => {
const version = fakeVersion;
const store = configureStore();
- const dispatch = jest
- .spyOn(store, 'dispatch')
- .mockImplementation(jest.fn());
+ const dispatch = spyOn(store, 'dispatch');
const fakeThunk = createFakeThunk();
render({
diff --git a/src/test-helpers.tsx b/src/test-helpers.tsx
index 2084dca..e3051ab 100644
--- a/src/test-helpers.tsx
+++ b/src/test-helpers.tsx
@@ -290,3 +290,7 @@ export const getFakeLogger = () => {
info: jest.fn(),
};
};
+
+export const spyOn = (...args: Parameters<typeof jest.spyOn>) => {
+ return jest.spyOn(...args).mockImplementation(jest.fn());
+};
async componentDidUpdate(prevProps: Props) { | ||
const { apiState, dispatch, profile } = this.props; | ||
componentDidUpdate(prevProps: Props) { | ||
const { apiState, profile } = this.props; | ||
|
||
if (!profile && prevProps.apiState.authToken !== apiState.authToken) { |
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.
These kind of if statements make me sad. We should look into simplifying it with useEffect
soon.
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.
Agreed. Should we file an issue? This patch gets too big.
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.
Definitely. Not high priority: #246
I added this function. I tweaked it a bit to make it TS-compliant. |
@kumar303 thanks a lot for the last review, I hope this is good to go now. |
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 again for thunking all the things. This looks great
d955f2b
to
3054212
Compare
Fixes #181
This PR allows us to write code like this now: https://github.com/mozilla/addons-code-manager/pull/218/files#diff-eaeb6d1753d7786e20e90d2089ffe11e. In other words, we now have precise return types for our API calls.
The PR also adds new thunks and a bunch of test cases.