concept dependency in category angular

appears as: dependencies, dependencies, dependency, dependency
Testing Angular Applications

This is an excerpt from Manning's book Testing Angular Applications.

  • Most unit tests you’ll write or see in production applications will cover a similar pattern. You usually have a section at the top for importing dependencies, a section to create the test suite, a section for setting up the tests, a section for the tests themselves, and a section to tear down the tests.
  • 3.2.1 Importing the dependencies

    Because ContactEditComponent is a fully functioning component, it requires a lot of dependencies. Your tests will reflect that in the number of import statements that you need. Let’s consider the imports in the following order:

    Listing 6.1 A service using Angular dependency injection
    import { Injectable } from '@angular/core';
    import { BrowserStorage } from './browser-storage.service';    #1   
    
    @Injectable()
    export class PreferencesService {
    
     constructor(private browserStorage: BrowserStorage ) { }    #2   
    
      public saveProperty(preference: IContactPreference) {
     this.browserStorage.setItem(preference.key, preference.value);    #3   
      }
    
      public getProperty(key: string) : any {
     return this.browserStorage.getItem(key);    #3  
      }
    }
    
    #1   Imports the class so that you can use the token to define the dependency
    #2   Angular dependency injection uses the service constructor to look up and supply dependencies.
    #3   PreferencesService uses injected services, not BrowserStorage directly.
    Angular in Action

    This is an excerpt from Manning's book Angular in Action.

    As usual, we start by importing dependencies for the component. Then the component metadata is declared using the @Component annotation. The class object is then declared, which contains two properties: the first is the array of symbols that’s retrieved from the Stocks service, and the second is a property to hold the value of the input. We’ll see how the stock property is linked to the input field in the template, but this is where it’s first defined.

  • Modules —Objects that help you to organize dependencies into discrete units
  • You may be wondering how this is different from using JavaScript module imports and exports. Why do we need another method to pass code around when JavaScript now has modules? Dependency injection shouldn’t be confused with JavaScript module imports. There is a need for Angular to be able to keep track of what parts of the application need a particular service. JavaScript has no awareness of how dependencies are linked together, which can be useful information in understanding how to best assemble dependencies. Also, injecting a dependency with Angular will resolve any additional dependencies.

    There are a few key pieces to the DI system. The first is the injector. This is the service that Angular provides for requesting and registering dependencies. The injector is often at work behind the scenes, but occasionally is used directly. Most of the time, you’ll invoke the injector by declaring a type annotation on the property. You may recall from chapter 2 how we injected the HttpClient service like this:

    constructor(private http: HttpClient) {}
    

    Because we declare the type as HttpClient (which is a known service in Angular), the application will use the injector to ensure that the http property contains an instance of the HttpClient service. This seems like magic, but it’s merely a way to alias the dependency you would like to request without directly calling the injector API.