-
-
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
0fd8dee
commit 513b8ea
Showing
16 changed files
with
269 additions
and
84 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 throw an error if used outside in the injection context', () => { | ||
let error; | ||
|
||
try { | ||
store.selectSignal((state) => state.todos); | ||
} catch (e) { | ||
error = `${e}`; | ||
} | ||
|
||
expect(error).toContain('Error: NG0203'); | ||
}); | ||
}); | ||
}); |
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
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 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 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
Oops, something went wrong.