Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 56d8c34

Browse files
committed
chore(state): add test cases for rxState options
1 parent fd0b067 commit 56d8c34

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

libs/state/spec/rx-state.spec.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { Component, isSignal, signal } from '@angular/core';
1+
import {
2+
Component,
3+
EnvironmentInjector,
4+
isSignal,
5+
signal,
6+
} from '@angular/core';
27
import { fakeAsync, TestBed, tick } from '@angular/core/testing';
38
import { selectSlice } from '@rx-angular/state/selections';
49
import {
@@ -395,6 +400,24 @@ describe(rxState, () => {
395400
}).toThrowError('readOnlyState.connect is not a function');
396401
});
397402
});
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+
});
398421
});
399422

400423
type ITestComponent<State extends object> = {
@@ -426,3 +449,12 @@ function setupComponent<State extends { count: number }>(
426449
component: fixture.componentInstance,
427450
};
428451
}
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

Comments
 (0)