Skip to content

Commit

Permalink
feat: token to exclude all interceptors
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Oct 26, 2020
1 parent 7b48127 commit 660f4c4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ I'm open to contributions.
- [a routing guard](#how-to-test-a-routing-guard)
- [a routing resolver](#how-to-test-a-routing-resolver)
- [a http request](#how-to-test-a-http-request)
- [a http interceptor](#how-to-test-a-http-interceptor)

---

Expand Down Expand Up @@ -1380,6 +1381,13 @@ beforeEach(() =>
// at all? The answer is to exclude `NG_GUARDS` token, it will removal
// all the guards from routes except the explicitly configured ones.
beforeEach(() => MockBuilder(MyGuard, MyModule).exclude(NG_GUARDS));
// The same thing if we want to test interceptors. If we exclude
// `NG_INTERCEPTORS` token, then all interceptors with `useValue` or
// `useFactory` will be excluded together with other interceptors
// except the explicitly configured ones.
beforeEach(() =>
MockBuilder(MyInterceptor, MyModule).exclude(NG_INTERCEPTORS)
);

// If we want to replace something with something,
// we should use .replace.
Expand Down
27 changes: 25 additions & 2 deletions examples/TestHttpInterceptor/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { Injectable, NgModule } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { MockBuilder } from 'ng-mocks';
import { MockBuilder, NG_INTERCEPTORS } from 'ng-mocks';
import { Observable } from 'rxjs';

// An interceptor we want to test.
Expand All @@ -29,15 +29,37 @@ class TargetInterceptor implements HttpInterceptor {
}
}

// An interceptor we want to ignore.
@Injectable()
class MockedInterceptor implements HttpInterceptor {
protected value = 'Ignore';

public intercept(request: HttpRequest<void>, next: HttpHandler): Observable<HttpEvent<void>> {
return next.handle(
request.clone({
setHeaders: {
'My-Mocked': this.value,
},
})
);
}
}

// A module with its definition.
@NgModule({
imports: [HttpClientModule],
providers: [
TargetInterceptor,
MockedInterceptor,
{
multi: true,
provide: HTTP_INTERCEPTORS,
useExisting: TargetInterceptor,
},
{
multi: true,
provide: HTTP_INTERCEPTORS,
useClass: TargetInterceptor,
useExisting: MockedInterceptor,
},
],
})
Expand All @@ -51,6 +73,7 @@ describe('TestHttpInterceptor', () => {
// with HttpClientTestingModule.
beforeEach(() =>
MockBuilder(TargetInterceptor, TargetModule)
.exclude(NG_INTERCEPTORS)
.keep(HTTP_INTERCEPTORS)
.replace(HttpClientModule, HttpClientTestingModule)
);
Expand Down
26 changes: 25 additions & 1 deletion lib/mock-service/mock-service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injector, Provider } from '@angular/core';

import { isNgInjectionToken, NG_GUARDS } from '../common';
import { isNgInjectionToken, NG_GUARDS, NG_INTERCEPTORS } from '../common';
import { ngMocksUniverse } from '../common/ng-mocks-universe';
import { MockProvider } from '../mock-module';

Expand Down Expand Up @@ -310,6 +310,30 @@ const mockServiceHelperPrototype = {
return;
}

if (
ngMocksUniverse.builder.has(NG_INTERCEPTORS) &&
ngMocksUniverse.builder.get(NG_INTERCEPTORS) === null &&
isNgInjectionToken(provider) &&
provider.toString() === 'InjectionToken HTTP_INTERCEPTORS' &&
provider !== def
) {
const interceptor = def.useExisting || def.useClass;
if (!ngMocksUniverse.builder.has(interceptor) || ngMocksUniverse.builder.get(interceptor) === null) {
/* istanbul ignore else */
if (changed) {
changed(true);
}
return;
}
if (def.useFactory || def.useValue) {
/* istanbul ignore else */
if (changed) {
changed(true);
}
return;
}
}

ngMocksUniverse.touches.add(provider);

// Then we check decisions whether we should keep or replace a def.
Expand Down

0 comments on commit 660f4c4

Please sign in to comment.