Angular
  • Angular learning
  • Angular
    • Change Detection
      • Angular Change Detection Strategies
      • Understanding Change Detection Strategy in Angular
    • Angular Components Overview
      • Lifecycle hooks
      • View encapsulation
    • Text interpolation
    • Pipes
    • ARIA
    • Event binding
    • Directives
    • Dependency injection in Angular
    • Difference between Template-Driven and Reactive Forms
    • Guards
    • Resolvers
      • Resolver example
  • Memory management in Angular applications
  • Renderer2
  • Angular test
    • Testing
      • The different types of tests
      • Some testing best practices
      • Angular Service Testing in Depth
        • About Jasmine
        • Jasmine - Test suites
        • Implementation of First Jasmine Specfication
        • spyOn() & jasmine.createSpyObj()
        • beforeEach()
        • Testing services
        • Disabled and Focused Tests
        • flush
        • HttpTestingController
        • Sample code
      • Angular Component Testing in Depth
        • Intro to Angular Component testing
        • DOM interaction
        • Trigger Change Detection
        • Test Card List Test Suite Conclusion
        • Window.requestAnimationFrame()
        • Asynchronous Work (Jasmine)
        • Cooperative asynchronous JavaScript: Timeouts and intervals
        • FakeAsync - Asynchronous Work (Jasmine) part 2
        • tick()
        • Sample codes
      • Testing Promised-based code-intro Microtasks
        • Microtasks
        • Micro-tasks within an event loop (Summary)
        • Macro-tasks within an event loop (Summary)
        • Test promised Microtasks (code)
      • Using fakeAsync to test Async Observables
      • Cypress.io
        • Create our first e2e test
      • Angular CLI code coverage and deployment in prod mode.
      • Travis CI
  • Angular best practices
    • Angular best practices
      • Security
      • Accessibility in Angular
      • Keeping your Angular projects up-to-date
    • Bootstrapping an Angular Application
      • Understanding the File Structure
      • Bootstrapping Providers
    • Components in Angular
      • Creating Components
      • Application Structure with Components
        • Accessing Child Components from Template
        • Using Two-Way Data Binding
        • Responding to Component Events
        • Passing Data into a Component
      • Projection
      • Structuring Applications with Components
      • Using Other Components
  • Reactive extensions
    • RxJS
      • RxJS Operators
      • of
      • Observable
      • async pipe (Angular)
      • Interval
      • fromEvent
      • Pipe
      • Map
      • Tap
      • ShareReplay
      • Concat
      • ConcatMap
      • Merge
      • MergeMap
      • ExhaustMap
      • fromEvent
      • DebounceTime
        • Type Ahead
      • Distinct Until Changed
      • SwitchMap
      • CatchError
      • Finalize
      • RetryWhen
      • DelayWhen
      • ForkJoin
      • First
      • Interview Questions
      • Zip
  • NgRx
    • What's NgRx
      • Actions
      • Reducers
      • Selectors
      • 🙅‍♂️Authentication guard with NgRX
      • @ngrx/effects
        • Side-Effect refresh survivor
  • Interview Q&A
    • Angular Unit Testing Interview Questions
    • Angular Questions And Answers
  • Angular Advanced
    • Setting up our environment
      • Understanding Accessors (TS)
      • The host & ::ng-deep Pseudo Selector
Powered by GitBook
On this page
  • CSS :first-child Selector
  • HTML DOM textContent Property

Was this helpful?

  1. Angular test
  2. Testing
  3. Angular Component Testing in Depth

Test Card List Test Suite Conclusion

In the following section, we will test some information displayed in the DOM. First, we will cover the following statements:

CSS :first-child Selector

Select and style every element that is the first child of its parent:

p:first-child {
  background-color: yellow;
}

HTML DOM textContent Property

Get the text content of an element:

var x = document.getElementById("myBtn").textContent;

Once we have understood these two statements one for CSS and the other for DOM we can now test the results we should expect in our test case.

it("should display the first course", () => {
  component.courses = setupCourses();
  fixture.detectChanges();
  
  const course = component.courses[0];
  
  const card = el.query(By.css('.course-card:first-child')),
    title = card.query(By.css('mat-card-title')),
    img = card.query(By.css('img'));
  
  expect(card).toBeTruthy('Could not find course card');
  expect(title.nativeElement.textContent).toBe(course.titles.description);
  expect(img.nativeElement.src).toBe(course.iconUrl);
});

In our HTML file we can check the following:

<mat-card *ngFor="let course of courses" class="course-card mat-elevation-z10">
    <mat-card-header>
        <mat-card-title>{{course.titles.description}}</mat-card-title>
    </mat-card-header>
    <img mat-card-image [src]="course.iconUrl">
</mat-card>

Let's cover a couple of things as well, we should know that if we are don't have a class we can write the proper HTML element like follows:

  const card = el.query(By.css('.course-card:first-child')), // CLASS
    title = card.query(By.css('mat-card-title')), // ELEMENT
    img = card.query(By.css('img')); // ELEMENT

PreviousTrigger Change DetectionNextWindow.requestAnimationFrame()

Last updated 4 years ago

Was this helpful?