lwc-recipes icon indicating copy to clipboard operation
lwc-recipes copied to clipboard

Make the lightning/navigation mock resettable

Open AdamTReineke opened this issue 2 years ago • 1 comments

What does this PR do?

  • Uses jest.fn() functions to receive the lightning/navigation mock calls so the mock can be reset.
  • Updates any existing test that used the navigation helper to reset all mocks.

Note: This repository did not contain any existing failures. Please see the first commit (da77ad518aeda26a7be6856c8a13fa3a695b15ea) for a demonstration of how a user might encounter this issue. I'm proposing this fix to minimize overall test changes so that users could easily consume this mock change without a refactor of their existing tests.

A more thorough fix would be to export the mockNavigate and mockGenerate constants (with better names) and remove the getNavigateCalledWith and getGenerateUrlCalledWith helper functions but this would be a more extensive refactor and a breaking change for anyone already following the pattern shown in this repository.

What issues does this PR fix or reference?

#720

The PR fulfills these requirements:

[x] Tests for the proposed changes have been added/updated. [x] Code linting and formatting was performed.

Functionality Before

Tests using this mock may incorrectly read state from previous tests, even if jest.clearAllMocks() was called.

Functionality After

This mock is can now be reset.

AdamTReineke avatar Sep 14 '22 20:09 AdamTReineke

FYI, I hit this issue on IdeaExchange and this was the solution we adopted.

AdamTReineke avatar Sep 14 '22 20:09 AdamTReineke

Hi @AdamTReineke, Thank you so much for this PR. Apologies for the delay in the review due to Dreamforce. I will review it today and merge it.

satyasekharcvb avatar Oct 10 '22 04:10 satyasekharcvb