4Catalyzer / found

Extensible route-based routing for React applications
https://4catalyzer.github.io/found/
MIT License
794 stars 55 forks source link

Export RouterContext for testing #1052

Closed sushantdhiman closed 4 weeks ago

sushantdhiman commented 1 year ago

Sometimes deep links are used in a screen. I would like to wrap my test components in RouterContext so screen can be rendered for testing. Something like this

const appStore = /* get app redux store */

function Wrapper({ children }: React.PropsWithChildren<unknown>) {
  return (
    <RouterContext.Provider
      value={{
        match: {},
        router: {
          push: sinon.spy(),
          go: sinon.spy(),
          createLocation: sinon.spy(),
          replace: sinon.spy(),
          createHref: sinon.spy(),
          isActive: sinon.spy(),
          matcher: {
            match: sinon.spy(),
            getRoutes: sinon.spy(),
            isActive: sinon.spy(),
            format: sinon.spy(),
          },
          addNavigationListener: sinon.spy(),
        },
      }}
    >
      <Provider store={appStore}>{children}</Provider>
    </RouterContext.Provider>
  );
}
sushantdhiman commented 4 weeks ago

Closing, as I no longer need this.