|
1 |
| -import { Component, isSignal, signal } from '@angular/core'; |
| 1 | +import { |
| 2 | + Component, |
| 3 | + EnvironmentInjector, |
| 4 | + isSignal, |
| 5 | + signal, |
| 6 | +} from '@angular/core'; |
2 | 7 | import { fakeAsync, TestBed, tick } from '@angular/core/testing';
|
3 | 8 | import { selectSlice } from '@rx-angular/state/selections';
|
4 | 9 | import {
|
@@ -395,6 +400,24 @@ describe(rxState, () => {
|
395 | 400 | }).toThrowError('readOnlyState.connect is not a function');
|
396 | 401 | });
|
397 | 402 | });
|
| 403 | + |
| 404 | + describe('without injection context', () => { |
| 405 | + it('should throw when invoked without injection context', () => { |
| 406 | + const { rxState } = setUpWithoutInjectionContext(); |
| 407 | + expect(() => rxState(() => {}, {})).toThrow( |
| 408 | + /rxState\(\) can only be used within an injection context/, |
| 409 | + ); |
| 410 | + }); |
| 411 | + |
| 412 | + it('should be able to use a custom Injector', () => { |
| 413 | + const { rxState } = setUpWithoutInjectionContext(); |
| 414 | + |
| 415 | + const envInjector = TestBed.inject(EnvironmentInjector); |
| 416 | + |
| 417 | + const state = rxState(() => {}, { injector: envInjector }); |
| 418 | + expect(state).toBeDefined(); |
| 419 | + }); |
| 420 | + }); |
398 | 421 | });
|
399 | 422 |
|
400 | 423 | type ITestComponent<State extends object> = {
|
@@ -426,3 +449,12 @@ function setupComponent<State extends { count: number }>(
|
426 | 449 | component: fixture.componentInstance,
|
427 | 450 | };
|
428 | 451 | }
|
| 452 | + |
| 453 | +function setUpWithoutInjectionContext() { |
| 454 | + return { |
| 455 | + rxState<T extends object>(...args: Parameters<typeof rxState<T>>) { |
| 456 | + const state = rxState(...args); |
| 457 | + return state; |
| 458 | + }, |
| 459 | + }; |
| 460 | +} |
0 commit comments