Skip to content

[Enterprise Search] Fix/update MockRouter helper to return specific routes/paths #82682

Merged
cee-chen merged 3 commits intoelastic:masterfrom
cee-chen:mock-router-fix
Nov 5, 2020
Merged

[Enterprise Search] Fix/update MockRouter helper to return specific routes/paths #82682
cee-chen merged 3 commits intoelastic:masterfrom
cee-chen:mock-router-fix

Conversation

@cee-chen
Copy link
Copy Markdown
Contributor

@cee-chen cee-chen commented Nov 5, 2020

Summary

I realized while doing some server API refactoring/testing that our MockRouter helper would fail/produce a false negative in the following example scenario:

export function registerSomeRoutes(...) {
  router.get(
    {
      path: '/api/app_search/example_one',
      validate: false,
    },
    enterpriseSearchRequestHandler.createRequest({
      path: '/as/example_one',
    })
  };

  router.get(
    {
      path: '/api/app_search/example_two',
      validate: false,
    },
    enterpriseSearchRequestHandler.createRequest({
      path: '/as/example_two',
    })
  };
}

Specifically, calling MockRouter.callRoute() would never actually call the 2nd router.get handler because of this line in the code:

const [, handler] = this.router[this.method].mock.calls[0];

Each router.get registration creates another array item in mock.calls, so the static [0] index meant only the 1st of each type of router method in a single registerSomeRoute function was ever getting called 😬

My solution/approach to this was adding a path param to the MockRouter initialization, and searching the array for the correct/specific route path. This has the benefit of being more explicit about exactly which route we're testing in our test code - I'll admit there have been a few times when I've been writing tests where I'm like, "how does this mock router know what route I'm testing??" so hopefully this helps answer that.

Checklist

Delete any items that are not applicable to this PR.

@cee-chen cee-chen added release_note:skip Skip the PR/issue when compiling release notes v7.11.0 labels Nov 5, 2020
@cee-chen cee-chen requested review from a team, JasonStoltz and scottybollinger November 5, 2020 02:22
@cee-chen
Copy link
Copy Markdown
Contributor Author

cee-chen commented Nov 5, 2020

I'll add that it's also possible to get around this simply by never putting more than 1 router method type per register function, for example:

export function registerSomeRoute(...) {
  router.get(
    {
      path: '/api/app_search/example_one',
      validate: false,
    },
    enterpriseSearchRequestHandler.createRequest({
      path: '/as/example_one',
    })
  };
});

export function registerAnotherRoute(...) {
  router.get(
    {
      path: '/api/app_search/example_two',
      validate: false,
    },
    enterpriseSearchRequestHandler.createRequest({
      path: '/as/example_two',
    })
  };
});

But I thought that fixing/modifying our helper would be more flexible & allow us to register multiple routes per function. Definitely open to more thoughts though, let me know what you think!

@cee-chen
Copy link
Copy Markdown
Contributor Author

cee-chen commented Nov 5, 2020

Also @scottybollinger I know this impacts your test PR a decent amount, I plan on merging this after your PR and rebasing against it

Copy link
Copy Markdown
Contributor

@scottybollinger scottybollinger left a comment

Choose a reason for hiding this comment

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

LGTM 🎉

@JasonStoltz
Copy link
Copy Markdown
Member

💯 For this change. I've definitely had the feeling you've described:

how does this mock router know what route I'm testing??

…rations of the same method

- This fix allows us to specify the route call we're testing via a path param
- e.g., in case a path gets typoed
@kibanamachine
Copy link
Copy Markdown
Contributor

💚 Build Succeeded

Metrics [docs]

✅ unchanged

History

  • 💔 Build #86026 failed 68352e26eb2868fb821d7039a75ce9c068717829

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

@cee-chen cee-chen merged commit 4a8f426 into elastic:master Nov 5, 2020
@cee-chen cee-chen deleted the mock-router-fix branch November 5, 2020 21:03
cee-chen pushed a commit that referenced this pull request Nov 5, 2020
…outes/paths (#82682) (#82781)

* Fix tests failing for route files that have more than 2 router registrations of the same method

- This fix allows us to specify the route call we're testing via a path param

* Update all existing uses of MockRouter to pass path param

* Add helpful error messaging

- e.g., in case a path gets typoed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release_note:skip Skip the PR/issue when compiling release notes v7.11.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants