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

[http] Enhance versioned router mock for easier introspection #159669

Merged
merged 4 commits into from
Jun 14, 2023

Conversation

jloleysens
Copy link
Contributor

Summary

Adds a helper/util to the Versioned router mock so that tests can more easily be expressed against registered versioned routes.

Usage

See included test. Thanks @paul-tavares for providing some prior art, I adapted slightly to rather return all the versions. Let me know what you think!

CC @pgayvallet @paul-tavares

@jloleysens jloleysens added Feature:http Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc release_note:skip Skip the PR/issue when compiling release notes labels Jun 14, 2023
@jloleysens jloleysens self-assigned this Jun 14, 2023
@jloleysens jloleysens requested a review from a team as a code owner June 14, 2023 10:14
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-core (Team:Core)

@jloleysens jloleysens changed the title updated the mock to reduce into an object, slightly nicer API? [http] Enhance versioned mock for easier introspection Jun 14, 2023
@jloleysens jloleysens changed the title [http] Enhance versioned mock for easier introspection [http] Enhance versioned router mock for easier introspection Jun 14, 2023
Copy link
Contributor

@pgayvallet pgayvallet left a comment

Choose a reason for hiding this comment

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

LGTM. API looks fine, some minor nits regarding the implementation

Comment on lines +58 to +73
let route: undefined | RegisteredVersionedRoute;
for (let x = 0; x < router[method].mock.calls.length; x++) {
const [routeConfig] = router[method].mock.calls[x];
if (routeConfig.path === path) {
const mockedAddVersion = router[method].mock.results[x].value as MockedVersionedRoute;
route = {
config: routeConfig,
versions: mockedAddVersion.addVersion.mock.calls.reduce(
(acc, [config, handler]) => ({
...acc,
[config.version]: { config, handler },
}),
{}
),
};
break;
Copy link
Contributor

Choose a reason for hiding this comment

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

NIT: I would have done that within the MockedVersionedRouter implementation (basically constructing this map / info at registration time) instead of going though the mock's calls in a second time. But as long as it works, it's fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, that was an alternative I also considered, but my guess is that this function won't be used a lot so I sacrificed time over space. Might be the wrong assumption, but we should be able to change the implementation without breaking stuff down the line.

Copy link
Contributor

@paul-tavares paul-tavares left a comment

Choose a reason for hiding this comment

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

Thank you @jloleysens for these changes. This looks awesome 👍

@jloleysens jloleysens enabled auto-merge (squash) June 14, 2023 12:59

export interface RegisteredVersionedRoute {
config: VersionedRouteConfig<any>;
versions: { [version: string]: { config: AddVersionOpts<any, any, any>; handler: any } };
Copy link
Contributor

Choose a reason for hiding this comment

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

should handler here be:

RequestHandler<any, any, any, any, any, KibanaResponseFactory>

This will ensure that what we get back matches what was registered - with the exception that Context is being set to any.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes! Good catch, I left any there while I was concentrating on something else and did not return to it! I'll update per your suggestion.

@jloleysens jloleysens disabled auto-merge June 14, 2023 13:55
@jloleysens jloleysens enabled auto-merge (squash) June 14, 2023 13:59
@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Public APIs missing comments

Total count of every public API that lacks a comment. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats comments for more detailed information.

id before after diff
@kbn/core-http-router-server-mocks 8 13 +5

Public APIs missing exports

Total count of every type that is part of your API that should be exported but is not. This will cause broken links in the API documentation system. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats exports for more detailed information.

id before after diff
@kbn/core-http-router-server-mocks 2 1 -1
Unknown metric groups

API count

id before after diff
@kbn/core-http-router-server-mocks 8 13 +5

ESLint disabled line counts

id before after diff
enterpriseSearch 13 15 +2
securitySolution 409 413 +4
total +6

Total ESLint disabled count

id before after diff
enterpriseSearch 14 16 +2
securitySolution 492 496 +4
total +6

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

cc @jloleysens

@jloleysens jloleysens merged commit a1c5603 into elastic:main Jun 14, 2023
20 checks passed
@kibanamachine kibanamachine added v8.9.0 backport:skip This commit does not require backporting labels Jun 14, 2023
@jloleysens jloleysens deleted the enhance-versioned-router-mock branch June 15, 2023 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting Feature:http release_note:skip Skip the PR/issue when compiling release notes Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc v8.9.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants