-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(store): add selectSignal method for interop with Angular Signals (…
- Loading branch information
1 parent
0df3419
commit 999dcb6
Showing
9 changed files
with
1,099 additions
and
234 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { ActionReducerMap, Store, provideStore } from '@ngrx/store'; | ||
|
||
import { State } from '../src/private_export'; | ||
import { | ||
ADD_TODO, | ||
COMPLETE_ALL_TODOS, | ||
COMPLETE_TODO, | ||
SET_VISIBILITY_FILTER, | ||
todos, | ||
visibilityFilter, | ||
VisibilityFilters, | ||
resetId, | ||
} from './fixtures/todos'; | ||
|
||
interface Todo { | ||
id: number; | ||
text: string; | ||
completed: boolean; | ||
} | ||
|
||
interface TodoAppSchema { | ||
visibilityFilter: string; | ||
todos: Todo[]; | ||
} | ||
|
||
describe('NgRx and Signals Integration spec', () => { | ||
let store: Store<TodoAppSchema>; | ||
let state: State<TodoAppSchema>; | ||
|
||
const initialState = { | ||
todos: [], | ||
visibilityFilter: VisibilityFilters.SHOW_ALL, | ||
}; | ||
const reducers: ActionReducerMap<TodoAppSchema, any> = { | ||
todos: todos, | ||
visibilityFilter: visibilityFilter, | ||
}; | ||
|
||
beforeEach(() => { | ||
resetId(); | ||
spyOn(reducers, 'todos').and.callThrough(); | ||
|
||
TestBed.configureTestingModule({ | ||
providers: [provideStore(reducers, { initialState })], | ||
}); | ||
|
||
store = TestBed.inject(Store); | ||
state = TestBed.inject(State); | ||
}); | ||
|
||
describe('todo integration spec', function () { | ||
describe('using the store.selectSignal', () => { | ||
it('should use visibilityFilter to filter todos', () => { | ||
store.dispatch({ type: ADD_TODO, payload: { text: 'first todo' } }); | ||
store.dispatch({ type: ADD_TODO, payload: { text: 'second todo' } }); | ||
store.dispatch({ | ||
type: COMPLETE_TODO, | ||
payload: { id: state.value.todos[0].id }, | ||
}); | ||
|
||
const filterVisibleTodos = ( | ||
visibilityFilter: string, | ||
todos: Todo[] | ||
) => { | ||
let predicate; | ||
if (visibilityFilter === VisibilityFilters.SHOW_ALL) { | ||
predicate = () => true; | ||
} else if (visibilityFilter === VisibilityFilters.SHOW_ACTIVE) { | ||
predicate = (todo: any) => !todo.completed; | ||
} else { | ||
predicate = (todo: any) => todo.completed; | ||
} | ||
return todos.filter(predicate); | ||
}; | ||
|
||
const filter = TestBed.runInInjectionContext(() => | ||
store.selectSignal((state) => state.visibilityFilter) | ||
); | ||
const todos = TestBed.runInInjectionContext(() => | ||
store.selectSignal((state) => state.todos) | ||
); | ||
const currentlyVisibleTodos = () => | ||
filterVisibleTodos(filter(), todos()); | ||
|
||
expect(currentlyVisibleTodos().length).toBe(2); | ||
|
||
store.dispatch({ | ||
type: SET_VISIBILITY_FILTER, | ||
payload: VisibilityFilters.SHOW_ACTIVE, | ||
}); | ||
|
||
expect(currentlyVisibleTodos().length).toBe(1); | ||
expect(currentlyVisibleTodos()[0].completed).toBe(false); | ||
|
||
store.dispatch({ | ||
type: SET_VISIBILITY_FILTER, | ||
payload: VisibilityFilters.SHOW_COMPLETED, | ||
}); | ||
|
||
expect(currentlyVisibleTodos().length).toBe(1); | ||
expect(currentlyVisibleTodos()[0].completed).toBe(true); | ||
|
||
store.dispatch({ type: COMPLETE_ALL_TODOS }); | ||
|
||
expect(currentlyVisibleTodos().length).toBe(2); | ||
expect(currentlyVisibleTodos()[0].completed).toBe(true); | ||
expect(currentlyVisibleTodos()[1].completed).toBe(true); | ||
|
||
store.dispatch({ | ||
type: SET_VISIBILITY_FILTER, | ||
payload: VisibilityFilters.SHOW_ACTIVE, | ||
}); | ||
|
||
expect(currentlyVisibleTodos().length).toBe(0); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('context integration spec', () => { | ||
it('Store.selectSignal should not throw an error if used outside in the injection context', () => { | ||
let error; | ||
|
||
try { | ||
store.selectSignal((state) => state.todos); | ||
} catch (e) { | ||
error = `${e}`; | ||
} | ||
|
||
expect(error).toBeUndefined(); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { expecter } from 'ts-snippet'; | ||
import { compilerOptions } from './utils'; | ||
|
||
describe('Store.selectSignal()', () => { | ||
const expectSnippet = expecter( | ||
(code) => ` | ||
import { Store, createSelector, createFeatureSelector } from '@ngrx/store'; | ||
interface State { foo: { bar: { baz: [] } } }; | ||
const store = {} as Store<State>; | ||
const fooSelector = createFeatureSelector<State, State['foo']>('foo') | ||
const barSelector = createSelector(fooSelector, s => s.bar) | ||
${code} | ||
`, | ||
compilerOptions() | ||
); | ||
|
||
describe('as property', () => { | ||
describe('with functions', () => { | ||
it('should enforce that properties exists on state (root)', () => { | ||
expectSnippet( | ||
`const selector = store.selectSignal(s => s.mia);` | ||
).toFail(/Property 'mia' does not exist on type 'State'/); | ||
}); | ||
|
||
it('should enforce that properties exists on state (nested)', () => { | ||
expectSnippet( | ||
`const selector = store.selectSignal(s => s.foo.bar.mia);` | ||
).toFail(/Property 'mia' does not exist on type '\{ baz: \[\]; \}'/); | ||
}); | ||
|
||
it('should infer correctly (root)', () => { | ||
expectSnippet( | ||
`const selector = store.selectSignal(s => s.foo);` | ||
).toInfer('selector', 'Signal<{ bar: { baz: []; }; }>'); | ||
}); | ||
|
||
it('should infer correctly (nested)', () => { | ||
expectSnippet( | ||
`const selector = store.selectSignal(s => s.foo.bar);` | ||
).toInfer('selector', 'Signal<{ baz: []; }>'); | ||
}); | ||
}); | ||
|
||
describe('with selectors', () => { | ||
it('should infer correctly', () => { | ||
expectSnippet( | ||
`const selector = store.selectSignal(fooSelector);` | ||
).toInfer('selector', 'Signal<{ bar: { baz: []; }; }>'); | ||
|
||
expectSnippet( | ||
`const selector = store.selectSignal(barSelector);` | ||
).toInfer('selector', 'Signal<{ baz: []; }>'); | ||
}); | ||
}); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { computed, signal, Signal } from '@angular/core'; | ||
|
||
import { StateObservable } from './state'; | ||
|
||
/** | ||
* Get the current value of an `StateObservable` as a reactive `Signal`. | ||
* | ||
* `toSignal` returns a `Signal` which provides synchronous reactive access to values produced | ||
* by the `StateObservable`, by subscribing to that `StateObservable`. The returned `Signal` will always | ||
* have the most recent value emitted by the subscription, and will throw an error if the | ||
* `StateObservable` errors. | ||
* | ||
* The subscription will last for the lifetime of the application itself. | ||
* | ||
* This function is for internal use only as it differs from the `toSignal` | ||
* provided by the `@angular/core/rxjs-interop` package with relying on | ||
* the injection context to unsubscribe from the provided observable. | ||
* | ||
*/ | ||
export function toSignal<T>(state$: StateObservable): Signal<T> { | ||
const state = signal<State<T>>({ kind: StateKind.NoValue }); | ||
|
||
state$.subscribe({ | ||
next: (value) => state.set({ kind: StateKind.Value, value }), | ||
error: (error) => state.set({ kind: StateKind.Error, error }), | ||
}); | ||
|
||
return computed(() => { | ||
const currentState = state(); | ||
|
||
switch (currentState.kind) { | ||
case StateKind.Value: | ||
return currentState.value; | ||
case StateKind.Error: | ||
throw currentState.error; | ||
case StateKind.NoValue: | ||
throw new Error( | ||
'@ngrx/store: The state observable must emit the initial value synchronously' | ||
); | ||
} | ||
}); | ||
} | ||
|
||
enum StateKind { | ||
NoValue, | ||
Value, | ||
Error, | ||
} | ||
|
||
type NoValueState = { kind: StateKind.NoValue }; | ||
type ValueState<T> = { kind: StateKind.Value; value: T }; | ||
type ErrorState = { kind: StateKind.Error; error: unknown }; | ||
|
||
type State<T> = NoValueState | ValueState<T> | ErrorState; |
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
Oops, something went wrong.