Skip to content

Instantly share code, notes, and snippets.

@benjamincharity
Created April 12, 2017 16:23
Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save benjamincharity/3d25cd2c95b6ecffadb18c3d4dbbd80b to your computer and use it in GitHub Desktop.
Save benjamincharity/3d25cd2c95b6ecffadb18c3d4dbbd80b to your computer and use it in GitHub Desktop.
Mock ActivatedRoute with params, data and snapshot.
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
MdToolbarModule,
],
providers: [
{
provide: Router,
useClass: MockRouter,
},
{
provide: ActivatedRoute,
useValue: {
data: {
subscribe: (fn: (value: Data) => void) => fn({
company: COMPANY,
}),
},
params: {
subscribe: (fn: (value: Params) => void) => fn({
tab: 0,
}),
},
snapshot: {
url: [
{
path: 'foo',
},
{
path: 'bar',
},
{
path: 'baz',
},
],
},
},
},
],
})
.overrideComponent(ConversationsComponent, {
set: {
template: '',
}
});
}));
@arozwalak
Copy link

arozwalak commented Jun 18, 2020

that works great, just in my case I had to change provider a little bit as I'm using pipe, before subscribing to route data. So in my case it looks like this

            provide: ActivatedRoute, useValue: {
              params: of(routeParams),
              data: {
                pipe: () => {
                  return {
                    subscribe: (fn: (value) => void) => fn({
                      myDataKey: routeDataFactory.getValue()
                    })
                  };
                }
              },
              snapshot: {}
            }
          }

@benjamincharity
Copy link
Author

Awesome! Glad to know that works 💪

@hpmartini
Copy link

perfect!

Just in case someone plans to mock the paramMap -> params.get() function here you go:

{
  provide: ActivatedRoute,
  useValue: {
	paramMap: Observable.of({
	  get: () => {
		return 10;
	  }
	})
  }
}

Thanks! That's what I neded!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment