5

我有一个具有组件级提供程序的 Angular 组件。

@Component({
  selector: 'storybook-di-component',
  templateUrl: './di.component.html',
  providers: [{ provide: TEST_TOKEN, useValue: 123 }],
})
export class DiComponent {
  @Input()
  title: string;

  constructor(
    protected injector: Injector,
    protected elRef: ElementRef,
    @Inject(TEST_TOKEN) protected testToken: number
  ) {}

在这种情况下,我如何让故事书注入不同的提供者,以便我可以提供替代/模拟服务?例如,在上面的 DiComponent 中,如果我想注入{ provide: TEST_TOKEN, useValue: 456 }呢?

真实世界的用例是我正在使用 ngrx/component-store 并且需要为组件提供一个虚拟的、预填充的存储。

(附加信息:)在模块级别(如下所示)注入它不起作用,因为组件仍在运行并创建它自己的提供者实例:

moduleMetadata: {
        providers: [
            { provide: StateStore, useValue: stateStore }
        ],
        imports: [...],
    },
4

2 回答 2

3

我最终使用低技术方法解决了这个问题。

  1. .stories.ts子类化文件中的组件
  2. 在子类中,声明构造函数以注入除需要模拟的服务之外的所有内容。
  3. super(...)使用注入的服务 + 模拟服务调用。

鉴于我的实际需要是注入ngrx/component-storemy的模拟,stories.ts现在有如下内容:

const stateStore = new RecordSuggestionsPageStore(mockSuggestionsService);
stateStore.setState(() => ({
    selectableRecords: [
        { name: "John Legend", recordType: "employee" },
        { name: "Allan Davies", recordType: "employee" },
    ]
}));

@Component({
    selector: "myapp-suggested-records-page",
    templateUrl: "./suggested-records-page.component.html",
    styleUrls: ["./suggested-records-page.component.scss"]
})
class SuggestedRecordsPageComponentStubbed extends SuggestedRecordsPageComponent {

    constructor(router: Router) {
        super(stateStore, router);
    }
}
于 2020-11-11T15:00:02.173 回答
0

TypeProvider为我工作对于您的示例,它类似于以下内容,其中 setState 的参数需要与被模拟的方法相匹配:

class MockRecordSuggestionsPageStore implements Partial<RecordSuggestionsPageStore> {
  setState() {
    return [
      { name: "John Legend", recordType: "employee" },
      { name: "Allan Davies", recordType: "employee" },
    ]
  }
}

对于 SuggestedRecordsPageComponent 故事中的提供者

    moduleMetadata: {
      declarations: [...],
      imports: [...],
      providers: [
        { provide: StateStore, useClass: MockRecordSuggestionsPageStore }
      ],
    },
于 2021-06-21T10:37:17.370 回答