Skip to content

Commit

Permalink
Merge pull request #154 from vanmxpx/fix-store-service-cleanup
Browse files Browse the repository at this point in the history
fix: add store service cleanup
  • Loading branch information
markwhitfeld authored Sep 13, 2024
2 parents 7c81606 + 198cbfe commit fe48123
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function defineSelectSnapshotProperties(
);
// Don't use the `directiveInject` here as it works ONLY
// during view creation.
store = store || getStore();
store = getStore();
return store.selectSnapshot(selector);
},
enumerable: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class NgxsSelectSnapshotModuleIsNotImported extends Error {
}

let injector: Injector | null = null;
let store: Store | null = null;

function assertDefined<T>(actual: T | null | undefined): asserts actual is T {
if (actual == null) {
Expand All @@ -25,9 +26,11 @@ export function setInjector(parentInjector: Injector): void {
*/
export function clearInjector(): void {
injector = null;
store = null;
}

export function getStore(): never | Store {
assertDefined(injector);
return injector!.get(Store);
store = store || injector!.get(Store);
return store;
}
59 changes: 40 additions & 19 deletions libs/select-snapshot/src/lib/select-snapshot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,29 +217,29 @@ describe('SelectSnapshot', () => {
expect(message).toEqual("Cannot read properties of undefined (reading 'here')");
});

it('should get the correct snapshot after dispatching multiple actions', () => {
// Arrange
class Increment {
public static type = '[Counter] Increment';
}
// Arrange
class Increment {
public static type = '[Counter] Increment';
}

@State({
name: 'counter',
defaults: 0,
})
@Injectable()
class CounterState {
@Action(Increment)
increment({ setState, getState }: StateContext<number>): void {
setState(getState() + 1);
}
@State({
name: 'counter',
defaults: 0,
})
@Injectable()
class CounterState {
@Action(Increment)
increment({ setState, getState }: StateContext<number>): void {
setState(getState() + 1);
}
}

@Component({ template: '' })
class TestComponent {
@SelectSnapshot(CounterState) counter!: number;
}
@Component({ template: '' })
class TestComponent {
@SelectSnapshot(CounterState) counter!: number;
}

it('should get the correct snapshot after dispatching multiple actions', () => {
// Act
TestBed.configureTestingModule({
imports: [
Expand All @@ -262,6 +262,27 @@ describe('SelectSnapshot', () => {
expect(componentInstance.counter).toBe(4);
});

it('should get the correct state after destroying the module', () => {
// Act
TestBed.configureTestingModule({
imports: [
NgxsModule.forRoot([CounterState], { developmentMode: true }),
NgxsSelectSnapshotModule.forRoot(),
],
declarations: [TestComponent],
});

// Assert
const { componentInstance } = TestBed.createComponent(TestComponent);
const store: Store = TestBed.inject<Store>(Store);

expect(componentInstance.counter).toBe(0);

store.dispatch(new Increment());

expect(componentInstance.counter).toBe(1);
});

@State<any>({
name: 'nullselector',
defaults: {
Expand Down

0 comments on commit fe48123

Please sign in to comment.