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: setParams #43

Merged
merged 1 commit into from
Jan 11, 2021
Merged

feat: setParams #43

merged 1 commit into from
Jan 11, 2021

Conversation

cexbrayat
Copy link
Contributor

@cexbrayat cexbrayat commented Jan 7, 2021

Introduces a new function setParams, allowing to set the parameters of the current route:

it('should display the user details', async () => {
  setParams({ userId: '12' })
  const wrapper = mount(UserDetails)

  // test...
})

Refs #42

What kind of change does this PR introduce? (check at least one)

  • Bugfix
  • Feature
  • Code style update
  • Refactor
  • Build-related changes
  • Other, please describe:

Does this PR introduce a breaking change? (check one)

  • Yes
  • No

If yes, please describe the impact and migration path for existing applications:

The PR fulfills these requirements:

  • When resolving a specific issue, it's referenced in the PR's title (e.g. fix #xxx[,#xxx], where "xxx" is the issue number)
  • All tests are passing
  • New/updated tests are included

If adding a new feature, the PR's description includes:

  • A convincing reason for adding this feature (to avoid wasting your time, it's best to open a suggestion issue first and wait for approval before working on it)

Other information:

@posva If that looks good, I'll do the same for setQuery and setHash

@codecov-io
Copy link

codecov-io commented Jan 7, 2021

Codecov Report

Merging #43 (0dddc3b) into v2 (e9a4383) will increase coverage by 0.02%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##               v2      #43      +/-   ##
==========================================
+ Coverage   97.91%   97.93%   +0.02%     
==========================================
  Files           5        5              
  Lines          96       97       +1     
  Branches       10       11       +1     
==========================================
+ Hits           94       95       +1     
  Misses          2        2              
Impacted Files Coverage Δ
src/router.ts 96.29% <100.00%> (+0.06%) ⬆️

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 e9a4383...0dddc3b. Read the comment docs.

@cexbrayat
Copy link
Contributor Author

@posva I amended the commit with your feedback:

  • setParams is now a function of RouterMock
  • we set the whole currentRoute.value

src/router.ts Outdated
@@ -174,12 +181,17 @@ export function createRouterMock(options: RouterMockOptions = {}): RouterMock {
return pendingNavigation || Promise.resolve()
}

function setParams(params: RouteParams) {
router.currentRoute.value = { ...router.currentRoute.value, params }
Copy link
Owner

Choose a reason for hiding this comment

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

shouldn't router.currentRoute.value = router.resolve({ params }) work too? It will also allow using RouteParamsRaw as params and allow numbers while still casting them to strings

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can definitely use router.resolve({ params }) 👍

But can I use RouteParamsRaw? It looks like it's not publicly exported

vue-router.d.ts:

export declare type RouteParams = Record<string, RouteParamValue | RouteParamValue[]>;
declare type RouteParamsRaw = Record<string, RouteParamValueRaw | RouteParamValueRaw[]>;

Copy link
Owner

Choose a reason for hiding this comment

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

I will export it

src/router.ts Outdated
@@ -40,6 +41,12 @@ export interface RouterMock extends Router {
* isn't any.
*/
getPendingNavigation(): ReturnType<Router['push']>

/**
* Sets the params of the current route
Copy link
Owner

@posva posva Jan 7, 2021

Choose a reason for hiding this comment

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

Suggested change
* Sets the params of the current route
* Sets the params of the current route without triggering a navigation.
* Use `router.push()` if you want to trigger a navigation instead.
*

@posva
Copy link
Owner

posva commented Jan 11, 2021

@cexbrayat I released a new version of vue router that exports the missing types, it should be good to rebase on v2 and add the missing types 🙂

Introduces a new function `setParams`, allowing to set the parameters of the current route:

```js
it('should display the user details', async () => {
  getRouter().setParams({ userId: 12 })
  const wrapper = mount(UserDetails)

  // test...
})
```

Refs posva#42
@cexbrayat
Copy link
Contributor Author

@posva Done! It now uses RouteParamsRaw. I'll let you review and I'll open the following PRs for setQuery and setHash.

Copy link
Owner

@posva posva left a comment

Choose a reason for hiding this comment

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

Thanks! I will add a few local changes

@posva posva merged commit d07b2e2 into posva:v2 Jan 11, 2021
@posva
Copy link
Owner

posva commented Jan 11, 2021

I added a few changes that should help with usage at 6d2829d I think we should use them for setQuery and setHash too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants