Skip to content

Testing: Easier stubbing of services #10128

@dreamofabear

Description

@dreamofabear

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions