Since this has come up a few times now. Context: #9866 (comment)
We could do this pretty simply by changing services.js to export an object/class instead of serviceForDoc functions. Could be nice for classes that need a lot of services stubbed.
Before
// services.js
actionServiceForDoc(nodeOrDoc) { ... }
// my-class.js
import {actionServiceForDoc} from '../src/services';
class MyClass {
foo() {
this.actionService().doSomething();
}
actionService() { // This is annoying to do for every service.
return actionForDoc(this.ampdoc);
}
}
// my-test.js
import {MyClass} from '../src/my-class.js';
sandbox.stub(MyClass, 'actionService').returns(mockActionService);
After
// services.js
export class Services {
static actionForDoc(nodeOrDoc) { ... }
}
// my-class.js
import {Services} from '../src/services';
class MyClass {
foo() {
Services.actionForDoc(this.ampdoc).doSomething();
}
}
// my-test.js
import {Services} from '../src/services';
sandbox.stub(Services, 'actionForDoc').returns(mockActionService);
It'd be a straightforward (if slightly annoying) refactor. I also checked that this would work with Babel's transpile.
/cc @lannka @alanorozco @keithwrightbos @taymonbeal @jridgewell
Since this has come up a few times now. Context: #9866 (comment)
We could do this pretty simply by changing
services.jsto export an object/class instead ofserviceForDocfunctions. Could be nice for classes that need a lot of services stubbed.Before
After
It'd be a straightforward (if slightly annoying) refactor. I also checked that this would work with Babel's transpile.
/cc @lannka @alanorozco @keithwrightbos @taymonbeal @jridgewell