scottrippey / next-router-mock

Mock implementation of the Next.js Router
MIT License
401 stars 38 forks source link

Add history support #117

Closed ergofriend closed 1 month ago

ergofriend commented 9 months ago

Introduction

This PR proposes to support history in anticipation of router.back() support. A picture of future support is envisioned (like this code).

As described in the React Router testing guide, React Router previously allowed explicit tracking of navigation history by passing the history object.

Ideally, centralizing state in the history object would eliminate the need for manual history restoration.

However, this pull request only implements location history recording at this stage.

Breaking Changes

This pull request change allows us to test the following...

        it("replace", async () => {
          const history = createMemoryHistory({ initialEntries: ["/one"] });
          memoryRouter.setCurrentHistory(history);
          expect(memoryRouter.asPath).toEqual("/one");
          expect(memoryRouter.history.index).toEqual(0);

          await memoryRouter.push("/two");
          expect(memoryRouter.asPath).toEqual("/two");
          expect(memoryRouter.history.index).toEqual(1);

          await memoryRouter.replace("/three");
          expect(memoryRouter.asPath).toEqual("/three");
          expect(memoryRouter.history.index).toEqual(1); // replace does not add a new entry
        });

Concerns

I would appreciate your input on the above concerns, as I think they are concerns. Thanks in advance!

changeset-bot[bot] commented 9 months ago

⚠️ No Changeset found

Latest commit: 69735e4d41cb1edb054998634799a98015d7808f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

jmisasa commented 8 months ago

Hi! Since next-router-mock does not offer history support, we have ended up implementing something similar on our end. Just a quick ping that we would be interested in this feature being added to the library.

Thanks a lot!

ergofriend commented 1 month ago

Sorry, I lost the motivation to handle this.