Angular: any disadvantage in putting store inside class? #127
-
Hi Thank you for this great library. In the provided Angular examples, the store is outside the class, e.g. const store = new Store({ name: 'todos', state, config });
@Injectable({ providedIn: 'root' })
export class TodosRepository {
todos$ = store.pipe(selectAll());
} Do you see any disadvantage in putting the store inside the class, e.g. @Injectable({ providedIn: 'root' })
export class TodosRepository {
store = new Store({ name: 'todos', state, config });
todos$ = this.store.pipe(selectAll());
} The reason why I would do this is to make sure that in unit tests that use the repository, I can be sure that each test has a new store, without needing to call |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
No. A matter of prefference |
Beta Was this translation helpful? Give feedback.
-
I'm trying to test with @ngneat/spectator a component that uses a repo. How to test the amount of const { config, state } = createState(withProps<{ names: string[] }>({ users: [] }));
const store = new Store({ name: 'user', config, state });
@Injectable()
export class UserRepo {
names$ = store.pipe(select(s => s.names));
loadNames$() {
return this.http.get('user-names').pipe(
tap(this.setNames),
);
}
setNames(names: string[]) {
store.update(s => ({ ...s, names }));
}
} @Component({
template: `<ul><li *ngFor="let name of repo.names$ | async">{{ name }}</li></ul>`,
})
export class UserComponent {
constructor(public repo: UserRepo) {
repo.loadNames$().subscribe();
}
} it('should display user names', () => {
// How can I populate the store with test user names?
const expected = spectator.queryAll('ul li').length;
expect(expected).toStrictEqual(2);
}); |
Beta Was this translation helpful? Give feedback.
No. A matter of prefference