Skip to content
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

Conversation

evanpurkhiser
Copy link
Member

Introduces a useApi hook that can be used in place of wrapping components with withApi

@evanpurkhiser evanpurkhiser requested a review from a team August 28, 2021 00:05
}
};
return WithApi;
};
Copy link
Member Author

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();
});
Copy link
Member Author

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

* 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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

static/app/utils/useApi.tsx Outdated Show resolved Hide resolved
static/app/utils/useApi.tsx Outdated Show resolved Hide resolved
Comment on lines +9 to +23
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 />;
};
Copy link
Member

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?

Copy link
Member Author

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

tests/js/spec/utils/withApi.spec.tsx Outdated Show resolved Hide resolved
};

const MyComponent: React.FC<Props> = ({api, persistInFlight}) => {
apiInstance = useApi({api, persistInFlight});
Copy link
Member

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which part?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the whole thing :)

Copy link
Member Author

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?

Copy link
Member

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

Copy link
Member Author

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

Copy link
Member

@priscilawebdev priscilawebdev left a 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!;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need!here?

Copy link
Member Author

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

@evanpurkhiser evanpurkhiser merged commit 0750603 into master Aug 31, 2021
@evanpurkhiser evanpurkhiser deleted the evanpurkhiser/feat-js-add-useapi-hook-and-implement-in-withapi branch August 31, 2021 08:01
@github-actions github-actions bot locked and limited conversation to collaborators Sep 15, 2021
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