-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(store): run plugins in injection context
In this commit, we enable plugin functions to run within the injection context. This allows plugin functions (but not classes) to call `inject` and access dependencies. Additionally, we've moved `compose` to the `Dispatcher` class since it was only used once. Keeping it in the `Dispatcher` ensures it isn't used elsewhere and can be safely inlined if needed.
- Loading branch information
Showing
4 changed files
with
75 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,72 @@ | ||
import { assertInInjectionContext } from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { NgxsModule, NGXS_PLUGINS, Store } from '@ngxs/store'; | ||
import { tap } from 'rxjs/operators'; | ||
import { Observable } from 'rxjs'; | ||
import { NgxsModule, NGXS_PLUGINS, Store, NgxsNextPluginFn, InitState } from '@ngxs/store'; | ||
import { debounceTime, firstValueFrom, tap } from 'rxjs'; | ||
|
||
describe('Plugins', () => { | ||
it('should run a function plugin', () => { | ||
let pluginInvoked = 0; | ||
it('should run a function plugin (within an injection context too)', async () => { | ||
// Arrange | ||
const recorder: any[] = []; | ||
|
||
class Foo { | ||
static readonly type = 'Foo'; | ||
} | ||
|
||
function logPlugin( | ||
state: any, | ||
action: any, | ||
next: (state: any, action: any) => Observable<any> | ||
) { | ||
if (action.constructor && action.constructor.type === 'Foo') { | ||
pluginInvoked++; | ||
function asyncLogPlugin(state: any, action: any, next: NgxsNextPluginFn) { | ||
assertInInjectionContext(asyncLogPlugin); | ||
|
||
if (action.constructor.type === 'Foo') { | ||
recorder.push(['asyncLogPlugin()', action, 'before next()']); | ||
} | ||
|
||
return next(state, action).pipe( | ||
debounceTime(0), | ||
tap(() => { | ||
if (action.constructor.type === 'Foo') { | ||
pluginInvoked++; | ||
recorder.push(['asyncLogPlugin()', action, 'after next()']); | ||
} | ||
}) | ||
); | ||
} | ||
|
||
function otherPlugin(state: any, action: any, next: NgxsNextPluginFn) { | ||
assertInInjectionContext(otherPlugin); | ||
recorder.push(['otherPlugin()', action]); | ||
return next(state, action); | ||
} | ||
|
||
TestBed.configureTestingModule({ | ||
imports: [NgxsModule.forRoot()], | ||
providers: [ | ||
{ | ||
provide: NGXS_PLUGINS, | ||
useValue: logPlugin, | ||
useValue: asyncLogPlugin, | ||
multi: true | ||
}, | ||
{ | ||
provide: NGXS_PLUGINS, | ||
useValue: otherPlugin, | ||
multi: true | ||
} | ||
] | ||
}); | ||
|
||
const store: Store = TestBed.inject(Store); | ||
store.dispatch(new Foo()); | ||
// Act | ||
const store = TestBed.inject(Store); | ||
|
||
// Assert | ||
expect(recorder).toEqual([['otherPlugin()', new InitState()]]); | ||
|
||
// Act | ||
const action = new Foo(); | ||
await firstValueFrom(store.dispatch(action)); | ||
|
||
expect(pluginInvoked).toEqual(2); | ||
// Assert | ||
expect(recorder).toEqual([ | ||
['otherPlugin()', new InitState()], | ||
['asyncLogPlugin()', action, 'before next()'], | ||
['otherPlugin()', action], | ||
['asyncLogPlugin()', action, 'after next()'] | ||
]); | ||
}); | ||
}); |