Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to shallow test a component with an `entryComponents`?

Tags:

angular

For example, say you have the following component:

import { Another } from "./Another";
@Component({
    entryComponents: [
        Another
    ]
})
export class App {}

Even when using NO_ERRORS_SCHEMA I still have to include Another as part of the test declarations:

import { ComponentFixture, TestBed } from "@angular/core/testing";
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { App } from "./App";
import { Another } from "./Another";

describe("App", () => {
  let comp: App;
  let fixture: ComponentFixture<App>;

  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [ App, Another ],
      schemas: [ NO_ERRORS_SCHEMA ]
    });
    fixture = TestBed.createComponent(App);
    comp = fixture.componentInstance;
  });

  it("can load instance", () => {
    expect(comp).toBeTruthy();
  });

});
like image 972
Manuel Avatar asked Jan 17 '17 05:01

Manuel


People also ask

What is the ideal outcome of using shallow tests?

Shallow tests might be good quick win practice optimizing tests. Isolation. It's quite common to add a new dependency to a resuable directive or component. It tends to break other tests because something is missing in dependency injection tree.

Is entryComponents deprecated?

entryComponents has been deprecated since Angular 9 (check our blog post for more details). When updating to v13. 1, a schematics will automatically remove entryComponents from your @Component and @NgModule if you still have some.

What is the difference between declarations and entryComponents?

Difference between entry Component and Declaration: entryComponents are used to register components for offline computation in a module. These components are referenced here as they not referenced anywhere else in HTML template. Declarations are used to make Directives(components, pipes etc.) in a specific module.

What is the use of entryComponents?

The entryComponent is the component which loads angular by force, that means these components are not referenced in the HTML template. In most of the cases, Angular loads a component when it is explicitly declared in the component template.


1 Answers

They plan on adding EntryComponents to the testing module interface. See issue: https://github.com/angular/angular/issues/10760

For a current workaround see the Angular Material library see https://github.com/angular/components/blob/master/src/material/dialog/dialog.spec.ts#L1660.

Basically, they create a real module on the fly and then import it for the tests.

// Create a real (non-test) NgModule as a workaround for
// https://github.com/angular/angular/issues/10760
const TEST_DIRECTIVES = [
  ComponentWithChildViewContainer,
  PizzaMsg,
  DirectiveWithViewContainer,
  ContentElementDialog
];

@NgModule({
  imports: [MdDialogModule],
  exports: TEST_DIRECTIVES,
  declarations: TEST_DIRECTIVES,
  entryComponents: [ComponentWithChildViewContainer, PizzaMsg, ContentElementDialog],
})
class DialogTestModule { }

Now you can use DialogTestModule

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [MdDialogModule.forRoot(), DialogTestModule]
      ...
like image 140
kampsj Avatar answered Sep 16 '22 11:09

kampsj