Skip to content

Override imports of routed standalone component #396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Nosfistis opened this issue Jun 21, 2023 · 5 comments · Fixed by #403
Closed

Override imports of routed standalone component #396

Nosfistis opened this issue Jun 21, 2023 · 5 comments · Fixed by #403

Comments

@Nosfistis
Copy link

I am having the case of a routed component that requires some resolved data. The component itself has some imports that require stubbing. In the pre-standalone era this was done easily via imports. With standalone it should be done via componentImports. However, componentImports does not override routed component imports, only the rendered component's imports.

await render(MyComponent, {
  componentImports: [FontAwesomeTestingModule],
  declarations: [StubSelectComponent],
  initialRoute: '',
  routes: [
    { path: '', component: MyComponent, resolve: { page: () => requestPageData() } },
  ],
});

Now, I know that this is flawed, but I am trying to achieve a truly routed component whose imports are stubbed.

@timdeschryver
Copy link
Member

Hi, that's an interesting use case.
Could you share a working example (component + test) that isn't using the standalone API?

@Nosfistis
Copy link
Author

Hi, I guess you are talking about the pre-standalone form of our test. It would be something like this:

Component:

@Component({
  templateUrl: './main.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MainComponent {
  readonly faClock = faClock;
  readonly faHistory = faHistory;
  readonly faFileDownload = faFileDownload;
}

Test:

const renderResult = await render(MainComponent, {
      imports: [
        FontAwesomeTestingModule,
        FormsModule,
        // more
      ],
      declarations: [RangePickerStubComponent],
      providers: [
        { provide: AlertService, useValue: alertService },
      ],
    });

@timdeschryver
Copy link
Member

@Nosfistis Yea, I'm looking for a reproduction that can be used to investigate this.

@timdeschryver
Copy link
Member

timdeschryver commented Jul 26, 2023

@Nosfistis I don't see a way to do this in a generic way that covers many cases.
Instead, I propose a callback in which you can configure the TestBed (for example to override providers, components, ...) to your own customized needs #403.
What do you think about it?

@Nosfistis
Copy link
Author

I think it would work fine, and it would be easy to adopt, since it looks a lot like the angular way currently we can make standalone component mocks:

Testbed.configureTestingModule({
  imports: [MyComponent],
})
  .overrideComponent(MyComponent, {
    add: { imports: [FontAwesomeTestingModule] },
    remove: { imports: [FontAwesomeModule] }
  })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants