Skip to content

Commit

Permalink
chore: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts committed Apr 27, 2022
1 parent 63ac608 commit 47ea64f
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
86 changes: 85 additions & 1 deletion modules/component-store/spec/component-store.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { ComponentStore } from '@ngrx/component-store';
import {
ComponentStore,
OnStateInit,
OnStoreInit,
} from '@ngrx/component-store';
import { fakeSchedulers, marbles } from 'rxjs-marbles/jest';
import {
of,
Expand Down Expand Up @@ -1447,4 +1451,84 @@ describe('Component Store', () => {
expect(componentStore.get()).toEqual({ value: 'updated' });
});
});

describe('lifecycle hooks', () => {
interface LifeCycle {
init: boolean;
}

const onStoreInitMessage = 'on store init called';
const onStateInitMessage = 'on state init called';
let logs: string[] = [];
class LifecycleStore
extends ComponentStore<LifeCycle>
implements OnStoreInit, OnStateInit
{
constructor(state?: LifeCycle) {
super(state);
}

logEffect = this.effect(
tap<void>(() => {
logs.push('effect');
})
);

ngrxOnStoreInit() {
logs.push(onStoreInitMessage);
}

ngrxOnStateInit() {
logs.push(onStateInitMessage);
}
}

let componentStore: LifecycleStore;

beforeEach(() => {
logs = [];
});

it('should call the OnInitStore lifecycle hook if defined', async () => {
componentStore = new LifecycleStore({ init: true });

expect(logs[0]).toBe(onStoreInitMessage);
});

it('should only call the OnInitStore lifecycle hook once', async () => {
componentStore = new LifecycleStore({ init: true });
expect(logs[0]).toBe(onStoreInitMessage);

logs = [];
componentStore.setState({ init: false });

expect(logs.length).toBe(0);
});

it('should call the OnInitState lifecycle hook if defined and state is set eagerly', async () => {
componentStore = new LifecycleStore({ init: true });

expect(logs[1]).toBe(onStateInitMessage);
});

it('should call the OnInitState lifecycle hook if defined and after state is set lazily', async () => {
componentStore = new LifecycleStore();

expect(logs.length).toBe(1);

componentStore.setState({ init: true });

expect(logs[1]).toBe(onStateInitMessage);
});

it('should only call the OnInitStore lifecycle hook once', async () => {
componentStore = new LifecycleStore({ init: true });

expect(logs[1]).toBe(onStateInitMessage);
logs = [];
componentStore.setState({ init: false });

expect(logs.length).toBe(0);
});
});
});
4 changes: 2 additions & 2 deletions modules/component-store/src/component-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ export class ComponentStore<T extends object> implements OnDestroy {

constructor(@Optional() @Inject(INITIAL_STATE_TOKEN) defaultState?: T) {
// check/call store init hook
this.callInitStoreHook();
this.callInitStoreHook(defaultState);

// State can be initialized either through constructor or setState.
if (defaultState) {
this.initState(defaultState);
}
}

private callInitStoreHook() {
private callInitStoreHook(ds?: T) {
const onStoreInit: Function | undefined = (
this as unknown as ComponentStore<T> & OnStoreInit
)['ngrxOnStoreInit'];
Expand Down

0 comments on commit 47ea64f

Please sign in to comment.