question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Insert services that uses Angular classes as dependency in UpgradedServices

See original GitHub issue

Is your feature request related to a problem? Please describe. I’m trying to insert EmailDashboardDataService, which uses HttpClient as a dependency in UpgradedServices file:

upgradedServices['EmailDashboardDataService'] =
      new EmailDashboardDataService();

But it needs the HttpClient object instance. HttpClient uses dependencies as well and its dependencies use other dependencies and so on.

Describe the solution you’d like Insert services that use third-party classes as dependencies in UpgradedService using a clean way.

Describe alternatives you’ve considered Here’s the approach I’ve found but I think it is not right. I’ve searched non-abstract classes that implement the dependency of the previous dependency. For example, HttpXhrBackend implements HttpHandler, an abstract class. HttpBackend uses HttpHandler type as a dependency.

upgradedServices['EmailDashboardDataService'] =
      new EmailDashboardDataService(
        new HttpClient(new HttpXhrBackend(new ɵangular_packages_common_http_http_d())));

How can we find a better solution for this service and future ones?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
bansalnitishcommented, Mar 2, 2020

Hey @marianazangrossi,

I did tried the approach that you mentioned, though I used BrowserXrf rather than some angular_packages as you used.

upgradedServices['QuestionBackendApiService'] =
  new QuestionBackendApiService(new HttpClient(
    new HttpXhrBackend(new BrowserXhr())),

The problem with this approach is the frontend tests still fail and definitely if we are adding any service in upgradedService we are doing it for frontend tests. So, I’ll explain you whole case in detail.

Let me give you an example. I was migrating the service question-backend-api.service.ts. Now, this service uses HttpClient. The questions-list.service uses QuestionBackendApiService. Look at the questions-list.service.spec.ts and particularly the following part:

  it('should get question summaries twice with history reset', function() {
    var skillIds = ['1'];
    $httpBackend.expect(
      'GET', '/questions_list_handler/1?cursor=').respond(
      sampleResponse);
    qls.getQuestionSummariesAsync(skillIds, true, true);
    $httpBackend.flush();

qls.getQuestionSummariesAsync() this function uses QuestionBackendApiService to create the request. The request is created via HttpClient now since QuestionBackendApiService is migrated to Angular. Hence, there is no request to flush or test via $httpBackend and that gives a error Error: No request to flush!. The request created by HttpClient is tested or flushed via HttpClientTestingModule and not by $httpBackend (which is used when service handles request via $http). I need to modify such tests where ever they fail.

This is one solution that we have and yes we can go ahead with this because at last we are modifying all tests right so it’s not waste of effort to do it right now.

There is one other thing: createAngularJSTestingModule. I tried using this as well but I failed.

I think I explained you, please let me know if you any questions.

Now, question for you – Why do you want this service in UpgradedServices since I checked this service is not being used in any other service?

0reactions
srijanreddy98commented, Oct 9, 2020

This is not required anymore since the angular injector error is solved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Introduction to services and dependency injection - Angular
Service is a broad category encompassing any value, function, or feature that an application needs. A service is typically a class with a...
Read more >
Add services - Angular
This tutorial creates a HeroService that all application classes can use to ... Instead of creating that service with the new keyword, use...
Read more >
Providing dependencies in modules - Angular
Most of the time, these dependencies are services that you create and provide. For the final sample application using the provider that this...
Read more >
Configuring dependency providers - Angular
The Creating and injecting services topic describes how to use classes as dependencies. Besides classes, you can also use other values such as...
Read more >
Understanding dependency injection - Angular
Imagine there is a class called HeroService that needs to act as a dependency in a component. The first step is to add...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found