2

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 } from '@angular/core';

@Component({
  moduleId: module.id,
  selector: 'cl-header',
  templateUrl: 'cl-header.component.html',
  styleUrls: ['cl-header.component.css']
})
export class ClHeaderComponent implements OnInit {
  headerTitle: string = 'Some Title';
  constructor() {}

  ngOnInit() {
  }

}

The component html template

<nav class="navbar navbar-default" role="navigation">
  <ul class="nav navbar-nav">
    <li><a href="#" class="fa fa-bars fa-2x"></a></li>
    <li><a href="http://localhost:91/UserHome/" class="fa fa-home fa-2x no-padding"></a></li>
  </ul>
  <div class="nav navbar-text-center">{{headerTitle}}</div>
  <a href="#" class="navbar-brand pull-right">
    <img src="app/components/cl-header/shared/logo.png" height="28px" alt="logo" />
  </a>
  <i>testtext</i>
</nav>

My Spec File

import {
  beforeEach,
  beforeEachProviders,
  describe,
  expect,
  it,
  inject,
  injectAsync
} from '@angular/core/testing';

import { ComponentFixture, TestComponentBuilder } from '@angular/compiler/testing';
import { Component } from '@angular/core';
import { By } from '@angular/platform-browser';

import { ClHeaderComponent } from './cl-header.component';

describe('Component: ClHeader', () => {
  it('should display header title: "Some Title"', injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => {
    return tcb.createAsync(ClHeaderComponent).then((fixture) => {
      fixture.detectChanges();

      var compiled = fixture.debugElement.nativeElement;

      //WORKING
      //To check that a given piece of text exists in the html template
      //expect(compiled.innerHTML).toContain('ul');
      //To check whether a given element type contains text
      //expect(compiled.querySelector('div')).toHaveText('Some Title');
      //END WORKING

      //NOT WORKING - ALWAYS RETURNS SUCCESS NO MATTER WHAT THE TOEQUAL COUNT IS          
      //To check that a given element by type exists in the html template
      expect(compiled.querySelector('div').length).toEqual(5);
    });
  }));
});

No matter what I set the toEqual expression to below: expect(compiled.querySelector('div').length).toEqual(5);

The test will always return true.

Does anyone have any advice on how to accurately get a count of a given element type in a component html template?

Thanks

3
  • 2
    Not sure how this may be 'success', but it should be querySelectorAll('div').length. Commented Jun 17, 2016 at 16:18
  • First, make sure you follow @estus advice, because querySelector will just return you 1 result that doesn't have a length property. Next, did you disable caching in your browser? Open developer tools usually helps with this. Commented Jun 17, 2016 at 17:42
  • Definitely a caching issue. compiled.querySelector('div').length will thrown an exception. Commented Jun 19, 2016 at 7:37

1 Answer 1

1

Thank you for your advice.

Whilst compiled.querySelector('div') did not throw an answer (using the Angular Cli seed and vsCode) it was indeed a caching issue as I am now able to return the correct element count using compiled.querySelectorAll('div')

I think my confusion arose due to the caching as I had originally tried querySelectorAll.

Ps. I couldn't set any comments as an answer so I had to set it answered by myself.

Sign up to request clarification or add additional context in comments.

1 Comment

Did you find a solution how how to clear cache automatically in the test?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.