Skip to content

Commit

Permalink
feat(app-state): improve error message when trying to .assign() to …
Browse files Browse the repository at this point in the history
…undefined state.

Closes #53
  • Loading branch information
ersimont committed Oct 2, 2021
1 parent 115b757 commit ad11531
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions projects/app-state/src/lib/store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,12 @@ describe('Store', () => {
expect(store.state()).toBe(startingState);
expect(cloneDeep(store.state())).toEqual(stateClone);
});

it('throws with a useful message when the state is missing', () => {
expect(() => {
store<'optional', InnerState>('optional').assign({ state: 3 });
}).toThrowError('cannot assign to undefined state');
});
});

describe('.setUsing()', () => {
Expand Down
4 changes: 4 additions & 0 deletions projects/app-state/src/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export abstract class Store<T> extends CallableObject<GetSlice<T>> {
*/
assign(value: Partial<T>): void {
this.setUsing((state: any) => {
if (!state) {
throw new Error('cannot assign to undefined state');
}

if (every(value, (innerValue, key) => state[key] === innerValue)) {
return state;
} else {
Expand Down

0 comments on commit ad11531

Please sign in to comment.