How to unit test if an angular 2 component contains another component
NickName:Chan15 Ask DateTime:2016-11-11T12:29:43

How to unit test if an angular 2 component contains another component

I'm quite new to angular 2.

I have a component which in turn has some other components in its template.

How do I write unit tests to check if my parent component consists of other components.

Mentioning a sample or directing me to a resource would be really helpful.

MyComponent.ts:

import { Component } from '@angular/core';
@Component({
selector: 'my-component',
templateUrl: `<div>
<other-component></other-component>
</div>`
})
export class MyComponent{

}

OtherComponent.ts:

import { Component } from '@angular/core';
@Component({
selector: 'other-component',
templateUrl: `<div>
<h1>Other Component</h1>
</div>`
})
export class OtherComponent{

}

Copyright Notice:Content Author:「Chan15」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40541123/how-to-unit-test-if-an-angular-2-component-contains-another-component

Answers
disappointed in SO leadership 2017-06-24T04:29:23

To test if a component, when compiled, contains other components:\n\n\nInject the component you're testing\nInject the child components \nCreate the parent component\nDetect changes\nUse querySelector or querySelectorAll to find the child components\n\n\nI typically only check that the element exists and then do further testing in the spec for the individual child component.\n\nimport { TestBed, async } from '@angular/core/testing';\n\nimport { AppComponent } from './app.component';\nimport { OtherComponent } from './other/other.component';\n\ndescribe('AppComponent', () => {\n beforeEach(async(() => {\n TestBed.configureTestingModule({\n declarations: [\n AppComponent,\n OtherComponent\n ],\n }).compileComponents();\n }));\n\n it('should create the app', async(() => {\n const fixture = TestBed.createComponent(AppComponent);\n const app = fixture.debugElement.componentInstance;\n expect(app).toBeTruthy();\n }));\n\n it('should have the other component', async(() => {\n const fixture = TestBed.createComponent(AppComponent);\n fixture.detectChanges();\n const compiled = fixture.debugElement.nativeElement;\n expect(compiled.querySelector('app-other')).not.toBe(null);\n }));\n});\n\n\nChecking for null with querySelector will determine if your component exists. From the querySelector MDN:\n\n\n Returns null if no matches are found; otherwise, it returns the first\n matching element.\n\n\n\n\nIf you'd like to check that there are multiple instances of the same child component, you can use querySelectorAll and check the length property:\n\nexpect(compiled.querySelectorAll('app-other').length).toBeGreaterThan(4);\n",


More about “How to unit test if an angular 2 component contains another component” related questions

How to unit test if an angular 2 component contains another component

I'm quite new to angular 2. I have a component which in turn has some other components in its template. How do I write unit tests to check if my parent component consists of other components.

Show Detail

Unit test for Component within component Angular 4

I am using Tab control using the thoughtram blog post. Here is the plnkr for the same. I was trying to create a unit test cases for the Tabs component which internally using Tab Component. And not...

Show Detail

Asserting that a parent component exists in angular unit test

I have a top level component AppComponent and in it's template property it renders the &lt;child&gt; element on the page. (&lt;child&gt; is another Angular component). Do I need to tell the unit t...

Show Detail

Is unit test required for each and every component in angular 4?

Angular 4 comes with cli tool. When you create a new component, it generates a component file, template file, stylesheet and spec file (unit test). MainComponent does nothing but is just a placeho...

Show Detail

What should we unit test for Angular2 Component

I am newbie to Angular2. I wrote a small application in Angular2 which has few components. I want to write unit tests for my client application. Someone suggested me when we write unit test for a

Show Detail

Angular 2 unit test for component

I am using ng2 with webpack 2. I cant figure out how to test component functions Here is my component import { Component, OnInit } from '@angular/core'; import { GlobalDataService } from '.

Show Detail

Writing Unit Test framework for an Angular component library

I have an Angular component library already written without unit test cases. The components follow certain level of hierarchy. Each component (say TextComponent) inherits a common set of properties...

Show Detail

How to write unit test on a angular component that use material stepper

Angular app use material stepper that is defined in template. The component access the stepper by using the @ViewChild('stepper') stepper: MatStepper; In the component, many method set the selected...

Show Detail

Angular2 component unit test

I am trying to understand more about Angular2 unit testing. Currently I am unable to get a count of a particular element in my component html template. My component import { Component, OnInit } f...

Show Detail

Component Unit Tests Not Testing Just the Component in My Angular 2 App?

I am trying to set up some unit tests in my Angular 2/AngularCLI app. While the app is working as expected, the testing shows failures. So, to start simply (or so I thought), I set up a test comp...

Show Detail