@@ -85,6 +85,7 @@ Become a bronze sponsor and get your logo on our README on GitHub.
85
85
- [ Additional Options] ( #additional-options )
86
86
- [ Testing Pipes] ( #testing-pipes )
87
87
- [ Using Custom Host Component] ( #using-custom-host-component )
88
+ - [ Testing DI Functions] ( #testing-di-functions )
88
89
- [ Mocking Providers] ( #mocking-providers )
89
90
- [ Mocking OnInit Dependencies] ( #mocking-oninit-dependencies )
90
91
- [ Mocking Constructor Dependencies] ( #mocking-constructor-dependencies )
@@ -238,6 +239,7 @@ The `createComponent()` method returns an instance of `Spectator` which exposes
238
239
- ` debugElement ` - The tested fixture's debug element
239
240
240
241
- ` flushEffects() ` - Provides a wrapper for ` TestBed.flushEffects() `
242
+ - ` runInInjectionContext() ` - Provides a wrapper for ` TestBed.runInInjectionContext() `
241
243
- ` inject() ` - Provides a wrapper for ` TestBed.inject() ` :
242
244
``` ts
243
245
const service = spectator .inject (QueryService );
@@ -926,6 +928,8 @@ describe('AuthService', () => {
926
928
The ` createService() ` function returns ` SpectatorService ` with the following properties:
927
929
- ` service ` - Get an instance of the service
928
930
- ` inject() ` - A proxy for Angular ` TestBed.inject() `
931
+ - ` flushEffects() ` - A proxy for Angular ` TestBed.flushEffects() `
932
+ - ` runInInjectionContext() ` - A proxy for Angular ` TestBed.runInInjectionContext() `
929
933
930
934
### Additional Options
931
935
@@ -1020,6 +1024,8 @@ The `createPipe()` function returns `SpectatorPipe` with the following propertie
1020
1024
- ` element ` - The native element of the host component
1021
1025
- ` detectChanges() ` - A proxy for Angular ` TestBed.fixture.detectChanges() `
1022
1026
- ` inject() ` - A proxy for Angular ` TestBed.inject() `
1027
+ - ` flushEffects() ` - A proxy for Angular ` TestBed.flushEffects() `
1028
+ - ` runInInjectionContext() ` - A proxy for Angular ` TestBed.runInInjectionContext() `
1023
1029
1024
1030
Setting inputs directly on a pipe using ` setInput ` or ` props ` is not possible.
1025
1031
Inputs should be set through ` hostProps ` or ` setHostInput ` instead, and passed through to your pipe in the template.
@@ -1075,6 +1081,52 @@ describe('AveragePipe', () => {
1075
1081
});
1076
1082
```
1077
1083
1084
+ ## Testing DI Functions
1085
+
1086
+ Every Spectator instance supports testing DI Function by passing them to ` runInInjectionContext() ` function. There is a dedicated test factory that simplifies such testing by eliminating the need to pass some arbitrary Angular class amongst other factory options. Let's say we have a following function that uses the http module to fetch users:
1087
+
1088
+ ``` ts
1089
+ import { HttpClient } from ' @angular/common/http' ;
1090
+ import { inject } from ' @angular/core' ;
1091
+
1092
+ function getUsers() {
1093
+ return inject (HttpClient ).get <User []>(' users' );
1094
+ }
1095
+ ```
1096
+
1097
+ Let's see how we can test DI Function easily with Spectator:
1098
+
1099
+ ``` ts
1100
+ import { HttpTestingController , provideHttpClientTesting } from ' @angular/common/http/testing' ;
1101
+ import { createInjectionContextFactory , SpectatorInjectionContext } from ' @ngneat/spectator' ;
1102
+
1103
+ function getUsers() {
1104
+ return inject (HttpClient ).get <User []>(' users' );
1105
+ }
1106
+
1107
+ describe (' Users' , () => {
1108
+ let spectator: SpectatorInjectionContext ;
1109
+ const createContext = createInjectionContextFactory ({ providers: [provideHttpClientTesting ()] });
1110
+
1111
+ it (' should fetch users' , () => {
1112
+ spectator = createContext ();
1113
+
1114
+ const controller = spectator .inject (HttpTestingController );
1115
+
1116
+ spectator .runInInjectionContext (getUsers ).subscribe ((users ) => {
1117
+ expect (users .length ).toBe (1 );
1118
+ });
1119
+
1120
+ controller .expectOne (' users' ).flush ([{ id: 1 }]);
1121
+ });
1122
+ });
1123
+ ```
1124
+
1125
+ The ` createContext() ` function returns ` SpectatorInjectionContext ` with the following properties:
1126
+ - ` inject() ` - A proxy for Angular ` TestBed.inject() `
1127
+ - ` flushEffects() ` - A proxy for Angular ` TestBed.flushEffects() `
1128
+ - ` runInInjectionContext() ` - A proxy for Angular ` TestBed.runInInjectionContext() `
1129
+
1078
1130
## Mocking Providers
1079
1131
1080
1132
For every Spectator factory, we can easily mock any provider.
@@ -1321,6 +1373,8 @@ We need to create an HTTP factory by using the `createHttpFactory()` function, p
1321
1373
- ` httpClient ` - A proxy for Angular ` HttpClient `
1322
1374
- ` service ` - The service instance
1323
1375
- ` inject() ` - A proxy for Angular ` TestBed.inject() `
1376
+ - ` flushEffects() ` - A proxy for Angular ` TestBed.flushEffects() `
1377
+ - ` runInInjectionContext() ` - A proxy for Angular ` TestBed.runInInjectionContext() `
1324
1378
- ` expectOne() ` - Expect that a single request was made which matches the given URL and it's method, and return its mock request
1325
1379
1326
1380
0 commit comments