-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
feat(js): Add useApi hook and implement in withApi #28257
feat(js): Add useApi hook and implement in withApi #28257
Conversation
} | ||
}; | ||
return WithApi; | ||
}; |
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.
Probably easier to read this by just looking at the file
wrapper.unmount(); | ||
|
||
expect(apiInstance?.clear).toHaveBeenCalled(); | ||
}); |
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.
I took a couple tests away because the useApi tests covers them
static/app/utils/useApi.tsx
Outdated
* component is unmounted. | ||
* | ||
* This may be useful in situations where your component needs to finish up | ||
* some where the client was passed into some type of action creator and the |
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.
* some where the client was passed into some type of action creator and the | |
* somewhere the client was passed into some type of action creator and the |
type Props = { | ||
/** | ||
* Test passthrough API clients | ||
*/ | ||
api?: Client; | ||
/** | ||
* Test persistInFlight | ||
*/ | ||
persistInFlight?: boolean; | ||
}; | ||
|
||
const MyComponent: React.FC<Props> = ({api, persistInFlight}) => { | ||
apiInstance = useApi({api, persistInFlight}); | ||
return <div />; | ||
}; |
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.
not sure.. but should we move this out of describe
?
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.
I'm indifferent. I think we usually have stuff in describe
blocks
}; | ||
|
||
const MyComponent: React.FC<Props> = ({api, persistInFlight}) => { | ||
apiInstance = useApi({api, persistInFlight}); |
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.
imo it's important to test this hook, but it feels like an implementation detail
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.
Which part?
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.
the whole thing :)
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.
I disagree, I think this is a proper test that the api client is provided to the hook caller.
How else would you test a hook like this?
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.
I understand and agree with you... I was just thinking that api is an implementation detail that we have to test anyway, but it would be an exception on purpose of the RTL
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.
Oh sure, I see what you're saying. Yeah
4c1aa61
to
013fdfb
Compare
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.
left 1 comment, but it looks good to me
} | ||
|
||
// Use the provided client if available | ||
const api = providedApi ?? localApi.current!; |
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.
do we need!
here?
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.
Yes unfortunately typescript isn't smart enough to understand that we set it above
Introduces a useApi hook that can be used in place of wrapping components with
withApi