-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(composables): add ngxs composables (#15)
- Loading branch information
Showing
12 changed files
with
209 additions
and
11 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 |
---|---|---|
@@ -1,10 +1,3 @@ | ||
# @bynary/composables/attribute | ||
|
||
Secondary entry point of `@bynary/composables`. It can be used by importing from `@bynary/composables/attribute`. | ||
|
||
## Composables: | ||
|
||
| Name | Purpose | | ||
|-------------------------------------------------------------|------------------------------------------------------| | ||
| [`Attribute`](docs/attribute.composable.md) | Get and set the value of an attribute of an element. | | ||
| [`Boolean Attribute`](docs/boolean-attribute.composable.md) | Get and set the value of a boolean attribute. | |
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,3 @@ | ||
# @bynary/composables/ngxs | ||
|
||
Secondary entry point of `@bynary/composables`. It can be used by importing from `@bynary/composables/ngxs`. |
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,5 @@ | ||
{ | ||
"lib": { | ||
"entryFile": "src/index.ts" | ||
} | ||
} |
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 @@ | ||
export * from './public-api'; |
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,29 @@ | ||
/** | ||
* @packageDocumentation | ||
* Composables for [NGXS](https://www.ngxs.io/). | ||
* | ||
* @example | ||
* ```ts | ||
* @Component({ | ||
* template: ` | ||
* My books: | ||
* <ul> | ||
* <li *ngFor="let book of books()>{{ book }}</li> | ||
* </ul> | ||
* ` | ||
* }) | ||
* class BooksComponent { | ||
* | ||
* // legacy way using an Observable | ||
* @Select(BooksState.books) | ||
* books$: Observable<string[]>; | ||
* | ||
* // new way using a signal | ||
* books = useSelect(BooksState.books); | ||
* } | ||
* ``` | ||
* | ||
* @module @bynary/composables/attribute | ||
*/ | ||
|
||
export * from './select.composable'; |
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,92 @@ | ||
import { inject, Injectable, isSignal } from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { Action, NgxsModule, Selector, State, StateContext, Store } from '@ngxs/store'; | ||
|
||
import { useSelect } from './select.composable'; | ||
|
||
interface IBookStateModel { | ||
books: string[]; | ||
} | ||
|
||
class AddBook { | ||
static readonly type = '[Books] Add'; | ||
|
||
constructor(public book: string) { | ||
} | ||
} | ||
|
||
@State<IBookStateModel>({ | ||
name: 'books', | ||
defaults: { | ||
books: [ 'The Hobbit' ] | ||
} | ||
}) | ||
@Injectable() | ||
class BooksState { | ||
|
||
@Selector() | ||
static books(state: IBookStateModel) { | ||
return state.books; | ||
} | ||
|
||
@Action(AddBook) | ||
addBook(ctx: StateContext<IBookStateModel>, action: AddBook) { | ||
ctx.patchState({ books: [ ...ctx.getState().books, action.book ] }); | ||
} | ||
} | ||
|
||
describe('select composable', () => { | ||
|
||
describe('useSelect', () => { | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ NgxsModule.forRoot([ BooksState ]) ] | ||
}); | ||
}); | ||
|
||
it('should be contained in state.utils', async () => { | ||
const module = await import('./select.composable'); | ||
|
||
expect(module).toHaveProperty('useSelect'); | ||
expect(typeof module.useSelect).toBe('function'); | ||
}); | ||
|
||
it('should return a Signal', async () => { | ||
TestBed.runInInjectionContext(() => { | ||
const result = useSelect(BooksState.books); | ||
|
||
expect(isSignal(result)).toBe(true); | ||
expect(result()).toEqual([ 'The Hobbit' ]); | ||
}); | ||
}); | ||
|
||
it('should pass the selector to Store.select', async () => { | ||
TestBed.runInInjectionContext(() => { | ||
const store = inject(Store); | ||
const selectSpy = jest.spyOn(store, 'select'); | ||
|
||
useSelect(BooksState.books); | ||
|
||
expect(selectSpy).toHaveBeenCalledTimes(1); | ||
expect(selectSpy).toHaveBeenCalledWith(BooksState.books); | ||
|
||
selectSpy.mockReset(); | ||
}); | ||
}); | ||
|
||
it('the returned Signal should update when the state changes', async () => { | ||
TestBed.runInInjectionContext(() => { | ||
const store = inject(Store); | ||
|
||
const result = useSelect(BooksState.books); | ||
|
||
expect(result()).toEqual([ 'The Hobbit' ]); | ||
|
||
store.dispatch(new AddBook('The Lord of the Rings')); | ||
|
||
expect(result()).toEqual([ 'The Hobbit', 'The Lord of the Rings' ]); | ||
}); | ||
}); | ||
}); | ||
}); |
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,43 @@ | ||
import { inject, Signal, Type } from '@angular/core'; | ||
import { toSignal } from '@angular/core/rxjs-interop'; | ||
import { Store } from '@ngxs/store'; | ||
import { StateToken } from '@ngxs/store/src/state-token/state-token'; | ||
|
||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
export function useSelect<T>(selector: (state: any, ...states: any[]) => T): Signal<T>; | ||
export function useSelect<T = any>(selector: string | Type<any>): Signal<T>; | ||
export function useSelect<T>(selector: StateToken<T>): Signal<T>; | ||
/** | ||
* Selects a slice of data from the store. | ||
* Uses {@link Store#select} internally and converts its observable to a signal. | ||
* | ||
* @example | ||
* ```ts | ||
* @Component({ | ||
* template: ` | ||
* My books: | ||
* <ul> | ||
* <li *ngFor="let book of books()>{{ book }}</li> | ||
* </ul> | ||
* ` | ||
* }) | ||
* class BooksComponent { | ||
* | ||
* // legacy way using an Observable | ||
* @Select(BooksState.books) | ||
* books$: Observable<string[]>; | ||
* | ||
* // new way using a signal | ||
* books = useSelect(BooksState.books); | ||
* } | ||
* ``` | ||
* | ||
* @param selector The selector function or key | ||
*/ | ||
export function useSelect<T>(selector: any) { | ||
const store = inject(Store); | ||
|
||
return toSignal(store.select<T>(selector)); | ||
} | ||
|
||
/* eslint-enable @typescript-eslint/no-explicit-any */ |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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