DockYard / ember-router-scroll

🗔 Scroll to top with preserved browser history scroll position.
https://dollarshaveclub.github.io/router-scroll-demo/
MIT License
204 stars 56 forks source link

Prevents apps from stubbing router service in unit tests #304

Open ef4 opened 2 years ago

ef4 commented 2 years ago

This addon eagerly instantiates its service in an instance-initializer:

https://github.com/DockYard/ember-router-scroll/blob/2f17728fd82d7d486888df9a09fdc4aae233f294/addon/instance-initializers/ember-router-scroll.js#L3

Which also eagerly instantiates the router service:

https://github.com/DockYard/ember-router-scroll/blob/2f17728fd82d7d486888df9a09fdc4aae233f294/addon/services/router-scroll.js#L72

But that means it's impossible to write a unit test that stubs the router service, because setupContext from @ember/test-hepers (which is what backs setupTest from 'ember-qunit') will run this initializer before users have a chance to register any overrides.

So if people have unit tests against a stubbed router service, and then they add this addon, those tests break.

mupkoo commented 1 year ago

One thing that you can do to go around this problem is to unregister the router service, before registering your mock

this.owner.unregister('service:router');
this.owner.register('service:router', class extends Service {});