Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Support EnvironmentProviders in provide and HttpInterceptorFn exemple #7011

Closed
kekel87 opened this issue Oct 18, 2023 · 2 comments · Fixed by #7441
Closed
Assignees
Labels

Comments

@kekel87
Copy link

kekel87 commented Oct 18, 2023

Hi, it's been a while! 😄 How are you?

Describe the feature or problem you'd like to solve

I'm trying to test an HttpInterceptorFn. There isn't much official Angular documentation on this, and there are no examples on your site.

I found something interesting thanks to this article:

MockBuilder()
  .provide(provideHttpClient(withInterceptors([myInterceptor])))
  .provide(provideHttpClientTesting())

The issue is that provideHttpClient returns an EnvironmentProviders, and provide only accepts Provider.

Proposed solution

It would be helpful to check if provide can accept EnvironmentProviders and update the typing.

In the meantime, this works:

MockBuilder()
  .provide(provideHttpClient(withInterceptors([myInterceptor])) as unknown as Provider)
  .provide(provideHttpClientTesting())

Additional context

Here's a complete example in case you want to add it to your documentation:

// my.interceptor.ts
export const myInterceptor: HttpInterceptorFn = <D = unknown>(request: HttpRequest<Response<D>>, next: HttpHandlerFn) => {
  return next(request);
};


// my.interceptor.spec.ts
describe('myInterceptor', () => {
  let client: HttpClient;
  let httpMock: HttpTestingController;

  beforeEach(() =>
    MockBuilder()
      .provide(provideHttpClient(withInterceptors([myInterceptor])) as unknown as Provider)
      .provide(provideHttpClientTesting())
  );

  beforeEach(() => {
    client = ngMocks.findInstance(HttpClient);
    httpMock = ngMocks.findInstance(HttpTestingController);
  });

  it('should blablabla', () => {
    client
      .get('/test')
      .pipe(first())
      .subscribe((response) => {
        expect(response).toEqual('data');
      });

    const req = httpMock.expectOne('/test');

    req.flush('data');
    httpMock.verify();
  });
});

Have a great day. ❤️

@satanTime
Copy link
Member

Hi @kekel87,

thanks for noting about that. The support will be released with the next version.

satanTime added a commit that referenced this issue Nov 15, 2023
feat(MockBuilder): supports EnvironmentProviders #7011
@satanTime
Copy link
Member

v14.12.0 has been released and contains a fix for the issue. Feel free to reopen the issue or to submit a new one if you meet any problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants