Skip to content
This repository was archived by the owner on Nov 18, 2024. It is now read-only.

Add better return types for the API functions #218

Merged
merged 4 commits into from
Feb 21, 2019
Merged

Conversation

willdurand
Copy link
Member

@willdurand willdurand commented Feb 15, 2019

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.

@codecov-io
Copy link

codecov-io commented Feb 15, 2019

Codecov Report

Merging #218 into master will increase coverage by 1.28%.
The diff coverage is 94.59%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
src/test-helpers.tsx 100% <100%> (ø) ⬆️
src/reducers/versions.tsx 100% <100%> (ø) ⬆️
src/api/index.tsx 100% <100%> (ø) ⬆️
src/pages/Browse/index.tsx 100% <100%> (ø) ⬆️
src/components/App/index.tsx 100% <100%> (+29.41%) ⬆️
src/reducers/users.tsx 85.18% <77.77%> (-3.71%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update be2ca27...3054212. Read the comment docs.

Copy link
Contributor

@kumar303 kumar303 left a 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.

@willdurand
Copy link
Member Author

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.

Yes, it is time to resolve #182 now.

@willdurand
Copy link
Member Author

Yes, it is time to resolve #182 now.

@kumar303 one idea built on top of this PR is this: 8fbbb9d

screen shot 2019-02-18 at 14 22 50

We dispatch all (known) errors and store them into a centralized notifications reducer, as "error" notifications. We could use the same reducer for other notifications, e.g., success messages.

@kumar303
Copy link
Contributor

@kumar303 one idea built on top of this PR is this: 8fbbb9d
...
We dispatch all (known) errors and store them into a centralized notifications reducer, as "error" notifications.

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 }));
    }

@willdurand
Copy link
Member Author

How about taking a baby step towards that approach like this?

💯 👍

I'll update this patch with that, and I'll try to think more about the other patch with the notifications reducer.

@willdurand
Copy link
Member Author

@kumar303 so I think this is good for another look. I had to introduce a updateWithProps() helper because Enzyme does not support async componentDidUpdate()... (it does support async componentDidMount() though). I added tests for everything I touched.

@willdurand willdurand requested a review from kumar303 February 19, 2019 22:33
@willdurand willdurand force-pushed the api-return-types branch 4 times, most recently from 3f4522d to 72b1345 Compare February 20, 2019 18:26
@willdurand willdurand changed the title Add better return types for the API functions [WIP] Add better return types for the API functions Feb 20, 2019
@willdurand willdurand force-pushed the api-return-types branch 2 times, most recently from 1d71bd8 to 8b2e149 Compare February 20, 2019 21:11
@willdurand willdurand requested a review from kumar303 February 20, 2019 21:21
@willdurand willdurand changed the title [WIP] Add better return types for the API functions Add better return types for the API functions Feb 20, 2019
@willdurand
Copy link
Member Author

@kumar303 this should be ready for another look! It's full of thunks 🌟

Copy link
Contributor

@kumar303 kumar303 left a 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) {
Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Contributor

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

@willdurand
Copy link
Member Author

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 😞

I added this function. I tweaked it a bit to make it TS-compliant.

@willdurand willdurand requested review from kumar303 and removed request for bobsilverberg February 21, 2019 10:29
@willdurand
Copy link
Member Author

@kumar303 thanks a lot for the last review, I hope this is good to go now.

Copy link
Contributor

@kumar303 kumar303 left a 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 :shipit:

@willdurand willdurand merged commit c935598 into master Feb 21, 2019
@willdurand willdurand deleted the api-return-types branch February 21, 2019 19:22
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants