Skip to content
This repository was archived by the owner on May 24, 2022. It is now read-only.

Commit ebf4ff8

Browse files
authored
EntityCollectionsRepository computed entitiesArray (#623)
* feat: entities array observable in data entity collections * feat: entities array observable test * feat: entities array observable doc * feat: entities array snapshot * feat: entities array docs * feat: entities array tests
1 parent d99991c commit ebf4ff8

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

docs/pages/state-repository.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ class ArticleEntitiesState extends NgxsDataEntityCollectionsRepository<Article>
8282
| state.snapshot | NgxsEntityCollections&lt;V, K&gt; | Memoized state value (getter) |
8383
| state.reset() | void | Reset state with default state value |
8484
| state.state\$ | Observable&lt;NgxsEntityCollections&lt;V, K&gt;&gt; | State data stream that you can subscribe to changes |
85-
| state.entityArray\$ | Observable&lt;V[]&gt; | Observable of entities array |
85+
| state.entities() | EntityDictionary<K, V> | Entities dictionary |
86+
| state.entitiesArray() | V[] | Entities array |
87+
| state.entities$ | Observable<EntityDictionary<K, V>> | Observable of entities dictionary |
88+
| state.entitiesArray$ | Observable<V[]> | Observable of entities array |
8689
| dispatch(actions: ActionType &verbar; ActionType[]) | void | Standard dispatch method |
8790
| state.addOne(entity: V) | void | Add one entity to the collection |
8891
| state.addMany(entities: V[]) | void | Add multiple entities to the collection |

integration/tests/entity/entity.spec.ts

+45
Original file line numberDiff line numberDiff line change
@@ -691,5 +691,50 @@ describe('[TEST]: Entity', () => {
691691
expect(article.getState()).toEqual({ ids: [], entities: {} });
692692
})
693693
);
694+
695+
it(
696+
'@article.entities',
697+
ngxsTestingPlatform([ArticleEntitiesState], (store, article) => {
698+
expect(store.snapshot()).toEqual({ article: { ids: [], entities: {} } });
699+
expect(article.entities).toEqual({});
700+
701+
article.setAll([
702+
{ id: 1, title: 'A_W', category: 'A_O' },
703+
{ id: 2, title: 'B_W', category: 'B_O' },
704+
{ id: 3, title: 'C_W', category: 'C_O' },
705+
{ id: 4, title: 'D_W', category: 'D_O' }
706+
]);
707+
708+
expect(article.entities).toEqual({
709+
'1': { id: 1, title: 'A_W', category: 'A_O' },
710+
'2': { id: 2, title: 'B_W', category: 'B_O' },
711+
'3': { id: 3, title: 'C_W', category: 'C_O' },
712+
'4': { id: 4, title: 'D_W', category: 'D_O' }
713+
});
714+
})
715+
);
716+
717+
it(
718+
'@article.entitiesArray',
719+
ngxsTestingPlatform([ArticleEntitiesState], (store, article) => {
720+
expect(store.snapshot()).toEqual({ article: { ids: [], entities: {} } });
721+
expect(article.entitiesArray).toEqual([]);
722+
723+
article.setAll([
724+
{ id: 1, title: 'A_W', category: 'A_O' },
725+
{ id: 2, title: 'B_W', category: 'B_O' },
726+
{ id: 3, title: 'C_W', category: 'C_O' },
727+
{ id: 4, title: 'D_W', category: 'D_O' }
728+
]);
729+
730+
expect(article.entitiesArray).toEqual([
731+
{ id: 1, title: 'A_W', category: 'A_O' },
732+
{ id: 2, title: 'B_W', category: 'B_O' },
733+
{ id: 3, title: 'C_W', category: 'C_O' },
734+
{ id: 4, title: 'D_W', category: 'D_O' }
735+
]);
736+
})
737+
);
738+
694739
});
695740
});

lib/repositories/src/ngxs-data-entity-collections/ngxs-data-entity-collections.repository.ts

+6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ export abstract class AbstractNgxsDataEntityCollectionsRepository<
4949
return this.snapshot.entities;
5050
}
5151

52+
@Computed()
53+
public get entitiesArray(): V[] {
54+
const snapshot = this.snapshot;
55+
return snapshot.ids.map(id => snapshot.entities[id]);
56+
}
57+
5258
@Computed()
5359
public get ids$(): Observable<K[]> {
5460
return this.state$.pipe(map((value: EntityCollections<V, K, C>): K[] => value.ids));

0 commit comments

Comments
 (0)